Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion completions/bash/lsr
Original file line number Diff line number Diff line change
Expand Up @@ -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}) )
Expand Down
1 change: 1 addition & 0 deletions completions/fish/lsr.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions completions/zsh/_lsr
Original file line number Diff line number Diff line change
Expand Up @@ -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]" \
Expand Down
2 changes: 1 addition & 1 deletion include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <git2/types.h>
#include <stdio.h>

#define LASER_VERSION "1.7.1"
#define LASER_VERSION "1.7.2"

typedef struct laser_opts
{
Expand Down
2 changes: 1 addition & 1 deletion include/laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lua/laser_default_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function M.getPerms(mode)
return perms
end

function M.formatSize(size)
if size == -1 then -- -1 size means it's a directory
function LASER_BUILTIN_formatSize(size)
if size == -1 then
return " ---"
end

Expand Down
2 changes: 1 addition & 1 deletion lua/lsr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
Expand Down
4 changes: 3 additions & 1 deletion src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)") \
Expand Down Expand Up @@ -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':
Expand Down
81 changes: 60 additions & 21 deletions src/laser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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)

{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down