From ea8286356e456a029f9706e62a1d00e6774f7e47 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 17 Jun 2025 12:36:41 +0200 Subject: [PATCH 1/2] feat: print the total size of current dir when `-s` flag is passed --- include/cli.h | 2 +- include/laser.h | 2 +- lua/laser_default_utils.lua | 6 ++- src/laser.c | 81 +++++++++++++++++++++++++++---------- src/main.c | 2 +- 5 files changed, 68 insertions(+), 25 deletions(-) diff --git a/include/cli.h b/include/cli.h index 7e13a8f..e43028b 100644 --- a/include/cli.h +++ b/include/cli.h @@ -4,7 +4,7 @@ #include #include -#define LASER_VERSION "1.7.1" +#define LASER_VERSION "1.7.2" typedef struct laser_opts { diff --git a/include/laser.h b/include/laser.h index 86d684c..681d89e 100644 --- a/include/laser.h +++ b/include/laser.h @@ -23,7 +23,7 @@ struct laser_dirent char git_status; }; -void laser_list_directory(laser_opts opts, int depth); +void laser_start(laser_opts opts); void laser_process_single_file(laser_opts opts); #endif diff --git a/lua/laser_default_utils.lua b/lua/laser_default_utils.lua index 7134de3..1c579bd 100644 --- a/lua/laser_default_utils.lua +++ b/lua/laser_default_utils.lua @@ -16,7 +16,7 @@ function M.getPerms(mode) end function M.formatSize(size) - if size == -1 then -- -1 size means it's a directory + if size == -1 then return " ---" end @@ -31,4 +31,8 @@ function M.formatSize(size) end end +function LASER_BUILTIN_formatSize(size) + return M.formatSize(size) +end + return M diff --git a/src/laser.c b/src/laser.c index 69b228f..3ab7b8c 100644 --- a/src/laser.c +++ b/src/laser.c @@ -15,6 +15,7 @@ #define BRANCH_SIZE 8 static ssize_t longest_ownername = 0; +static size_t current_dir_total_size = 0; // ------------------------- helper function decl ----------------------------- static void laser_process_entries(laser_opts opts, int depth, char *indent); @@ -32,26 +33,39 @@ static laser_color_type laser_color_for_format(const char *filename); // returns size of directory if able. else returns -1 static off_t laser_git_dir_size(struct laser_dirent *entry, char *fp); +static void laser_list_directory(laser_opts opts, int depth); // ---------------------------------------------------------------------------- -void laser_list_directory(laser_opts opts, int depth) +void laser_start(laser_opts opts) { - if (opts.recursive_depth >= 0 && depth > opts.recursive_depth) - return; + laser_list_directory(opts, 0); - const char *pipe = "│ "; - size_t indent_len = depth > 0 ? depth * strlen(pipe) : 0; - char *indent = malloc(indent_len + 1); - if (indent == NULL) - laser_logger_fatal(1, "Malloc function failed: %s", strerror(errno)); + if (opts.show_directory_size) + { + const char *errtemp = + "error when trying to call LASER_BUILTIN_formatSize: %s\n" + "Please open a issue on github with this message as this is" + " an internal error!\n"; - indent[0] = '\0'; - if (depth > 0) - for (int i = 1; i < depth; i++) - strcat(indent, pipe); + lua_getglobal(L, "LASER_BUILTIN_formatSize"); + lua_pushinteger(L, current_dir_total_size); - laser_process_entries(opts, depth, indent); - free(indent); + if (lua_pcall(L, 1, 1, 0) != LUA_OK) + { + laser_logger_error(errtemp, lua_tostring(L, -1)); + lua_pop(L, 1); + return; + } + + const char *size = lua_tostring(L, -1); + if (size == NULL) + { + laser_logger_error(errtemp, ""); + lua_pop(L, 1); + return; + } + printf("\nTotal size: %s\n", size); + } } void laser_process_single_file(laser_opts opts) @@ -88,6 +102,26 @@ void laser_process_single_file(laser_opts opts) } // ------------------------- helper function impl ----------------------------- +static void laser_list_directory(laser_opts opts, int depth) +{ + if (opts.recursive_depth >= 0 && depth > opts.recursive_depth) + return; + + const char *pipe = "│ "; + size_t indent_len = depth > 0 ? depth * strlen(pipe) : 0; + char *indent = malloc(indent_len + 1); + if (indent == NULL) + laser_logger_fatal(1, "Malloc function failed: %s", strerror(errno)); + + indent[0] = '\0'; + if (depth > 0) + for (int i = 1; i < depth; i++) + strcat(indent, pipe); + + laser_process_entries(opts, depth, indent); + free(indent); +} + static void laser_process_entries(laser_opts opts, int depth, char *indent) { @@ -173,14 +207,19 @@ static void laser_process_entries(laser_opts opts, int depth, char *indent) strcmp(entry->d->d_name, "..") == 0)) continue; - if (S_ISDIR(entry->s.st_mode) && opts.show_directory_size) + if (opts.show_directory_size) { - int dirsize = laser_git_dir_size(entry, full_path); - if (dirsize == -1) - laser_logger_error("couldn't calculate the size of %s\n", - entry->d->d_name); - else - entry->s.st_size = dirsize; + if (S_ISDIR(entry->s.st_mode)) + { + off_t dirsize = laser_git_dir_size(entry, full_path); + if (dirsize == -1) + laser_logger_error( + "couldn't calculate the size of %s\n", + entry->d->d_name); + else + entry->s.st_size = dirsize; + } + current_dir_total_size += entry->s.st_size; } char *ownername = getpwuid(entry->s.st_uid)->pw_name; diff --git a/src/main.c b/src/main.c index a9cb3a3..4a4906e 100644 --- a/src/main.c +++ b/src/main.c @@ -72,7 +72,7 @@ int main(int argc, char **argv) laser_process_single_file(opts); goto clean; } - laser_list_directory(opts, 0); + laser_start(opts); clean: laser_cli_destroy_opts(opts); From 53b0aa5678b18c79c3dcea69a2fdf434d46916a2 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 17 Jun 2025 13:18:02 +0200 Subject: [PATCH 2/2] fix: force hidden when `-s` to calculate correct dir size update readme and completions too --- README.md | 2 +- completions/bash/lsr | 2 +- completions/fish/lsr.fish | 1 + completions/zsh/_lsr | 1 + lua/laser_default_utils.lua | 6 +----- lua/lsr.lua | 2 +- src/cli.c | 4 +++- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cc50550..0161c4d 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ you can directly pass in the flags. last modified date, size, and owner. - `-s` / `--directory-size` Displays the physical disk usage of the all the -subdirectories. (will force the `-l`/`--long` flag) +subdirectories. (will force the `-l`/`--long` flag and the `-a`/`--all` flag) - `-c` / `--ensure-colors` Ensures that the output is colorized even if output is redirected to another file than stdout (e.g. `lsr > file.txt` or `lsr | diff --git a/completions/bash/lsr b/completions/bash/lsr index 5ab5e87..cade391 100644 --- a/completions/bash/lsr +++ b/completions/bash/lsr @@ -2,7 +2,7 @@ _lsr() { local cur prev opts opts_with_args COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" - opts="-a --all -F --Files -D --Directories -S --Symlinks -G --Git -g --git-status -i --git-ignored -l --long -r --recursive -f --filter -c --ensure-colors -n --no-sort -! --no-lua -v --version -h --help --completions " + opts="-a --all -F --Files -D --Directories -S --Symlinks -G --Git -g --git-status -i --git-ignored -l --long -s --directory-size -r --recursive -f --filter -c --ensure-colors -n --no-sort -! --no-lua -v --version -h --help --completions " opts_with_args="--Git --git-status --git-ignored --recursive --filter --completions " if [[ ${cur} == --* ]]; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) diff --git a/completions/fish/lsr.fish b/completions/fish/lsr.fish index 566d5eb..53769b1 100644 --- a/completions/fish/lsr.fish +++ b/completions/fish/lsr.fish @@ -7,6 +7,7 @@ complete -c lsr -s G -l Git -d "Do not show ignored git files" -r complete -c lsr -s g -l git-status -d "Show git status for entries" -r complete -c lsr -s i -l git-ignored -d "Ignore git ignored files" -r complete -c lsr -s l -l long -d "Use long format" +complete -c lsr -s s -l directory-size -d "Show directory size (forces the --long flag and the --all flag)" complete -c lsr -s r -l recursive -d "Show in tree format" -r complete -c lsr -s f -l filter -d "Filter out files using lua filters (L_filters in lsr.lua)" -r complete -c lsr -s c -l ensure-colors -d "Force colored output" diff --git a/completions/zsh/_lsr b/completions/zsh/_lsr index b3058d4..d97af3f 100644 --- a/completions/zsh/_lsr +++ b/completions/zsh/_lsr @@ -10,6 +10,7 @@ _lsr() { "--git-status[Specify git-status]:Show git status for entries:" \ "--git-ignored[Specify git-ignored]:Ignore git ignored files:" \ "(-l --long)"{-l,--long}"[Use long format]" \ + "(-s --directory-size)"{-s,--directory-size}"[Show directory size (forces the --long flag and the --all flag)]" \ "--recursive[Specify recursive]:Show in tree format:" \ "--filter[Specify filter]:Filter out files using lua filters (L_filters in lsr.lua):" \ "(-c --ensure-colors)"{-c,--ensure-colors}"[Force colored output]" \ diff --git a/lua/laser_default_utils.lua b/lua/laser_default_utils.lua index 1c579bd..ce46773 100644 --- a/lua/laser_default_utils.lua +++ b/lua/laser_default_utils.lua @@ -15,7 +15,7 @@ function M.getPerms(mode) return perms end -function M.formatSize(size) +function LASER_BUILTIN_formatSize(size) if size == -1 then return " ---" end @@ -31,8 +31,4 @@ function M.formatSize(size) end end -function LASER_BUILTIN_formatSize(size) - return M.formatSize(size) -end - return M diff --git a/lua/lsr.lua b/lua/lsr.lua index ccac4e7..75c4452 100644 --- a/lua/lsr.lua +++ b/lua/lsr.lua @@ -15,7 +15,7 @@ function L_long_format(entry, longest_name) local perms = string.format("%s%s", entry.type, utils.getPerms(entry.mode)) local last_modified = os.date("%b %d %H:%M", entry.mtime) - local size = utils.formatSize(entry.size) + local size = LASER_BUILTIN_formatSize(entry.size) local owner = string.format("%-" .. longest_name .. "s ", entry.owner) return string.format("%s %s%s %s%s %s%s ", diff --git a/src/cli.c b/src/cli.c index 355b413..88898af 100644 --- a/src/cli.c +++ b/src/cli.c @@ -19,7 +19,7 @@ static int completionsset; _X("git-ignored", optional_argument, 0, 'i', "Ignore git ignored files") \ _X("long", 0, 0, 'l', "Use long format") \ _X("directory-size", 0, 0, 's', \ - "Show directory size (forces the --long flag)") \ + "Show directory size (forces the --long flag and the --all flag)") \ _X("recursive", optional_argument, 0, 'r', "Show in tree format") \ _X("filter", required_argument, 0, 'f', \ "Filter out files using lua filters (L_filters in lsr.lua)") \ @@ -160,6 +160,8 @@ laser_opts laser_cli_parsecmd(int argc, char **argv) case 's': // force the long flag cuz without it we aint seeing size show_long = 1; + // force showing hidden too cuz we wanna calc the size + show_all = 1; show_directory_size = 1; break; case 'c':