Skip to content

Commit

Permalink
Merge pull request #41 from jmattaa/feat/L_default_args
Browse files Browse the repository at this point in the history
feat: add `L_default_args`
  • Loading branch information
jmattaa authored Dec 11, 2024
2 parents 086c302 + ccf968d commit 9fa69f2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 10 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ cmake_minimum_required(VERSION 3.18)

project(lsr LANGUAGES C)

set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG -fsanitize=address -Wall -Wextra -pedantic")
option(ENABLE_SANITIZER "Enable AddressSanitizer for debug builds" ON)

if(ENABLE_SANITIZER)
set(SANITIZER_FLAGS "-fsanitize=address")
else()
set(SANITIZER_FLAGS "")
endif()

set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG ${SANITIZER_FLAGS} -Wall -Wextra -pedantic")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")

find_package(Lua 5.1 REQUIRED)
Expand Down
24 changes: 23 additions & 1 deletion CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
1. [Constants](#constants)
1. [`L_colors`](#l_colors)
2. [`L_recursive_max_depth`](#l_recursive_max_depth)
3. [`L_filters`](#l_filters)
3. [`L_default_args`](#l_default_args)
4. [`L_filters`](#l_filters)
2. [Functions](#functions)
1. [`L_compare_entries()`](#l_compare_entries())
2. [`L_long_format()`](#l_long_format())
Expand Down Expand Up @@ -76,6 +77,27 @@ a `-rN` you can set the maximum depth to `N` and override the default value.
L_recursive_max_depth = -1
```

#### `L_default_args`

You can configure the default arguments that should be used when running `lsr`
by creating the global `L_default_args` table.

The default configuration of `L_default_args` is:
```lua
L_default_args = {
all = false, -- show hidden files
files = true, -- show files
directories = true, -- show directories
symlinks = true, -- show symlinks
git = false, -- show git status and entries tracked by git
long = false, -- show long format

filters = {}, -- apply filters defined in `L_filters`
-- (NOTE: the name should be a string)
}

```

#### `L_filters`

You can configure the default filters by modifying the global table named
Expand Down
1 change: 1 addition & 0 deletions include/init_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ extern lua_State *L;
lua_State *laser_init_lua(void);
void laser_lua_load_script(const char *script_path);
void laser_lua_set_package_path(const char *script_path);
void laser_lua_destroy(void);

#endif
10 changes: 10 additions & 0 deletions lua/lsr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ end

function L_pre_print_entries()
end

L_default_args = {
all = false,
files = true,
directories = true,
symlinks = true,
git = false,
long = false,
filters = {},
}
82 changes: 76 additions & 6 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,85 @@ static const char *descriptions[] = {
"Generate shell completions",
};

#define L_DEFAULT_SIMPLE_ARGS(_X) \
_X(all, boolean) \
_X(files, boolean) \
_X(directories, boolean) \
_X(symlinks, boolean) \
_X(git, boolean) \
_X(long, boolean)

#define L_DEFAULT_SIMPLE_ARGS_GET(arg, type) \
lua_pushstring(L, #arg); \
lua_gettable(L, -2); \
if (lua_is##type(L, -1)) \
*show_##arg = lua_toboolean(L, -1); \
lua_pop(L, 1);

static void lua_get_L_default_args(int *show_all, int *show_files,
int *show_directories, int *show_symlinks,
int *show_git, int *show_long,
const char ***filters, int *filter_count)
{
lua_getglobal(L, "L_default_args");
if (!lua_istable(L, -1))
return;

L_DEFAULT_SIMPLE_ARGS(L_DEFAULT_SIMPLE_ARGS_GET)

lua_pushstring(L, "filters");
lua_gettable(L, -2);
if (lua_istable(L, -1))
{
lua_pushnil(L);
while (lua_next(L, -2) != 0)
{
(*filters) =
realloc(*filters, sizeof(char *) * ((*filter_count) + 1));
(*filters)[*filter_count] = strdup(lua_tostring(L, -1));
(*filter_count)++;
lua_pop(L, 1);
}
}
lua_pop(L, 1);
}

#define L_DEFAULT_ARG_TYPES(_X) \
_X(show_files) \
_X(show_directories) \
_X(show_symlinks)

laser_opts laser_cli_parsecmd(int argc, char **argv)
{
int show_all = 0;
int show_files = -1;
int show_directories = -1;
int recursive_depth = -1;
int show_symlinks = -1;
int recursive_depth = -1;
int show_git = 0;
int show_tree = 0;
int show_long = 0;
const char *dir = ".";

// set default recursive_depth from lua
lua_getglobal(L, "L_recursive_max_depth");
if (lua_isnumber(L, -1))
recursive_depth = (int)lua_tointeger(L, -1);

int filter_count = 0;
const char **filters = NULL;

int opt;

#define _X(name) int default_##name;
L_DEFAULT_ARG_TYPES(_X)
#undef _X

#define _X(name) &default_##name,
lua_get_L_default_args(&show_all, L_DEFAULT_ARG_TYPES(_X)(&show_git),
&show_long, &filters, &filter_count);
#undef _X

// set default recursive_depth from lua
lua_getglobal(L, "L_recursive_max_depth");
if (lua_isnumber(L, -1))
recursive_depth = (int)lua_tointeger(L, -1);

while ((opt = getopt_long(argc, argv, "aFDSGr::lvhf::", long_args, NULL)) !=
-1)
{
Expand Down Expand Up @@ -129,7 +186,20 @@ laser_opts laser_cli_parsecmd(int argc, char **argv)
}

if (optind < argc)
{
dir = argv[optind];
}

// check if default lua values have been overriden by cli
// if not then use them if yes then use cli values
#define _X(name) name == -1 &&
if (L_DEFAULT_ARG_TYPES(_X) 1) // there is prolly a better way than to put 1
#undef _X
{
#define _X(name) name = default_##name;
L_DEFAULT_ARG_TYPES(_X)
#undef _X
}

return (laser_opts){
show_all, show_files, show_directories, show_symlinks, show_git,
Expand Down
6 changes: 6 additions & 0 deletions src/init_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ void laser_lua_set_package_path(const char *script_path)
lua_pop(L, 1);
}
}

void laser_lua_destroy(void)
{
lua_settop(L, 0);
lua_close(L);
}
4 changes: 2 additions & 2 deletions src/lua_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ int lua_filters_apply(laser_opts opts, struct laser_dirent *entry)
if (lua_toboolean(L, -1))
{
lua_pop(L, 1); // pop the boolean
continue; // check the next filter
continue; // check the next filter
}

lua_pop(L, 1); // pop the boolean
lua_pop(L, 1); // pop the table
return 0; // filtered out
return 0; // filtered out
}

lua_pop(L, 1); // pop the boolean
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ int main(int argc, char **argv)
laser_cli_destroy_opts(opts);
laser_colors_free();

laser_lua_destroy();

return 0;
}

0 comments on commit 9fa69f2

Please sign in to comment.