Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit c3d2597730e5a68d2b14d7f281ce722c3ce480a5 Author: rani <clagv.randomgames@gmail.com> Date: Thu Jan 01 18:08:13 2026 +0000 Add: human readable file sizes diff --git a/include/config.h b/include/config.h index 8c9102a..53790ab 100644 --- a/include/config.h +++ b/include/config.h @@ -256 +257 @@ typedef struct {   bool longmode;   bool longinline;   bool dots; + bool humansize;   enum dir_sort_dirs dir_sort_dirs;   enum dir_sortby dir_sortby;   enum dir_sort dir_sort; diff --git a/include/dir.h b/include/dir.h index 3d46d07..775a399 100644 --- a/include/dir.h +++ b/include/dir.h @@ -246 +2415 @@  #define REGSEARCH_BAD_REGEX 1  #define REGSEARCH_NOT_FOUND 2   +enum size_unit { + SIZE_B, + SIZE_KB, + SIZE_MB, + SIZE_GB, + SIZE_TB, + SIZE_EB, +}; +  enum de_type {   DE_FILE,   DE_DIR, @@ -386 +478 @@ enum de_type {  typedef struct {   char * name;   enum de_type type; + unsigned size_small; + enum size_unit size_unit;     size_t size;   uint16_t mode; @@ -586 +698 @@ typedef struct {   size_t longest_uname;   size_t longest_gname;   size_t longest_size; + size_t longest_size_small; + size_t longest_size_unit;  } dir_t;    int dir_list(const char * path, dir_t * dir); @@ -775 +906 @@ char dirent_creprl(const dirent_t * de); // like above, but for the link  char dirent_longcrepr(const dirent_t * de); // like above, but for long mode  bool dirent_isexec(const dirent_t * de);  const char * dirent_prettymode(const dirent_t * de); +const char * dirent_size_unit(const dirent_t * de);    #endif /* DIR_H */ diff --git a/src/config.c b/src/config.c index 8353340..f4e35f2 100644 --- a/src/config.c +++ b/src/config.c @@ -66 +67 @@ config_t config = {   .longmode = true,   .longinline = true,   .dots = true, + .humansize = true,     .dir_sortby = DIR_SORTBY_NAME,   .dir_sort = DIR_SORT_INCREASING, diff --git a/src/dir.c b/src/dir.c index 1b7da66..9f3049c 100644 --- a/src/dir.c +++ b/src/dir.c @@ -496 +498 @@ int dir_list(const char * path, dir_t * dir) {   dir->longest_uname = 0;   dir->longest_gname = 0;   dir->longest_size = 0; + dir->longest_size_small = 0; + dir->longest_size_unit = 0;     struct dirent * de;   DIR * dp = opendir(path); @@ -7411 +7617 @@ int dir_list(const char * path, dir_t * dir) {   size_t uname_len = 0;   size_t gname_len = 0;   size_t size_len = ilen(dirent.size, 10); + size_t size_small_len = ilen(dirent.size_small, 10); + size_t size_unit_len = (dirent.size_unit == SIZE_B) ? 1 : 2;   if (dirent.uname) uname_len = strlen(dirent.uname);   if (dirent.gname) gname_len = strlen(dirent.gname);   if (uname_len > dir->longest_uname) dir->longest_uname = uname_len;   if (gname_len > dir->longest_gname) dir->longest_gname = gname_len;   if (size_len > dir->longest_size) dir->longest_size = size_len; + if (size_small_len > dir->longest_size_small) + dir->longest_size_small = size_small_len; + if (size_unit_len > dir->longest_size_unit) + dir->longest_size_unit = size_unit_len;   }     closedir(dp); @@ -1427 +15018 @@ void dir_entry(int dirfd, const char * name, dirent_t * dirent) {   }   }   + size_t size = statbuf.st_size; + enum size_unit size_unit = SIZE_B; + for (;size >= 1024;) { + size /= 1024; + size_unit++; + if (size_unit == SIZE_EB) break; + } +   dirent->size = statbuf.st_size; + dirent->size_small = size; + dirent->size_unit = size_unit; +   dirent->mode = statbuf.st_mode & 07777;   dirent->mtime = statbuf.st_mtime;   @@ -3703 +38915 @@ void dir_sort(const dir_t * dir) {   qsort(dir->entries, dir->len, sizeof(dir->entries[0]), dir_sort_cmp);   qsort(dir->nodots, dir->nodots_len, sizeof(dir->entries[0]), dir_sort_cmp);  } + +const char * dirent_size_unit(const dirent_t * de) { + static const char * units[] = { + [SIZE_B] = "B", [SIZE_KB] = "kB", [SIZE_MB] = "MB", + [SIZE_GB] = "GB", [SIZE_TB] = "TB", [SIZE_EB] = "EB", + }; + static const char * nounit = ""; + + const char * ret = units[de->size_unit]; + if (!ret) return nounit; + return ret; +} diff --git a/src/ui.c b/src/ui.c index ae49f45..ac2a79c 100644 --- a/src/ui.c +++ b/src/ui.c @@ -3912 +3914 @@ static int ui_dirent_color(const dirent_t * de);  static int ui_link_color(const dirent_t * de);  static void ui_print_dirent(const dirent_t * de, size_t pos, bool selected, const dir_t * dir);  static void ui_wlpadstr(WINDOW * win, const char * s, size_t len); +static void ui_wrpadstr(WINDOW * win, const char * s, size_t len);  static void ui_get_first_last(size_t n_dirents, size_t cursor, size_t * first, size_t * last);    void ui_init(void) {   setlocale(LC_CTYPE, "");     initscr(); + cbreak();   keypad(stdscr, TRUE);   curs_set(0);   noecho(); @@ -1226 +12416 @@ static void ui_wlpadstr(WINDOW * win, const char * s, size_t len) {   waddstr(win, s);  }   +static void ui_wrpadstr(WINDOW * win, const char * s, size_t len) { + size_t slen = strlen(s); + + waddstr(win, s); + + for (size_t i = slen; i < len; i++) { + waddch(win, ' '); + } +} +  void ui_print_dirent(const dirent_t * de, size_t pos, bool selected, const dir_t * dir) {   static const enum ui_color mode_colors[] = {   ['r'] = RED, ['w'] = MAGENTA, ['x'] = COLOR_EXEC, @@ -1648 +17615 @@ void ui_print_dirent(const dirent_t * de, size_t pos, bool selected, const dir_t   if (de->gname) ui_wlpadstr(filewin, de->gname, dir->longest_gname + 1);   else waddstr(filewin, " ?");   - snprintf(isbuf, sizeof(isbuf), "%lu", de->size); - ui_wlpadstr(filewin, isbuf, dir->longest_size + 1); + if (config.humansize) { + const char * unit = dirent_size_unit(de); + snprintf(isbuf, sizeof(isbuf), "%u", de->size_small); + ui_wlpadstr(filewin, isbuf, dir->longest_size_small + 1); + ui_wrpadstr(filewin, unit, dir->longest_size_unit); + } else { + snprintf(isbuf, sizeof(isbuf), "%zu", de->size); + ui_wlpadstr(filewin, isbuf, dir->longest_size + 1); + }     strftime(stime, sizeof(stime), "%b %d %H:%M %Y", localtime(&de->mtime));   wprintw(filewin, " %s ", stime); @@ -1766 +1957 @@ void ui_print_dirent(const dirent_t * de, size_t pos, bool selected, const dir_t     int color = COLOR_PAIR(ui_dirent_color(de));   if (selected) color |= A_REVERSE; + waddch(filewin, ' ');   wattron(filewin, color);   waddstr(filewin, de->name);   wattroff(filewin, color);