Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lua scripting to configure lsr #36

Merged
merged 25 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8350620
hacky solution for exposing api to lua scripts
jmattaa Nov 21, 2024
43f04b8
add completions back
jmattaa Nov 21, 2024
2d37d62
change build system to CMake
jmattaa Nov 21, 2024
69e077b
remove Makefile
jmattaa Nov 21, 2024
6fd0eb3
feat: add loading custom scripts from config
jmattaa Nov 21, 2024
fe3bb36
chore: keep branch up-to-date with main
jmattaa Nov 21, 2024
a2fbcf9
Merge branch 'main' into feat/extensibility
jmattaa Nov 21, 2024
7b69552
feat: add default scripts instalalion to CMake
jmattaa Nov 21, 2024
874f788
feat(lua): move long list format function into lua
jmattaa Nov 23, 2024
6e11506
build: add uninstall target to cmake
jmattaa Nov 23, 2024
809e4b2
feat(lua): pass longest owner to long format func
jmattaa Nov 23, 2024
73a8ad9
remove unused diagnostics disable config
jmattaa Nov 23, 2024
30224e4
Merge branch 'main' into feat/extensibility
jmattaa Nov 23, 2024
62d9c99
refactor(main): load default functinos before user
jmattaa Nov 23, 2024
d40a05c
feat(lua): rewrite colors to lua instead of env
jmattaa Nov 24, 2024
af7f948
docs(README.md): update configuration docs
jmattaa Nov 24, 2024
ed30f12
refactor(cli.c): cli options static and add help
jmattaa Nov 24, 2024
13f1364
refactor: remove macos specific header
jmattaa Nov 24, 2024
6425a1d
fix: exit command help early
jmattaa Nov 24, 2024
8841ba5
docs(README): update installation instructions
jmattaa Nov 24, 2024
c5df184
docs(CONFIGURATION): add config instructions
jmattaa Nov 24, 2024
20a58e5
docs: add toc to all md files
jmattaa Nov 24, 2024
29e5dfa
docs: update toc in CONFIGURATION.md
jmattaa Nov 24, 2024
505596a
docs: update installation instructions
jmattaa Nov 24, 2024
5931970
build: update CMakeLists.txt
jmattaa Nov 24, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin/
build/
assets/demo-dir
.cache/

Expand Down
61 changes: 61 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.18)

project(lsr LANGUAGES C)

set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG -fsanitize=address -Wall -Wextra -pedantic")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")

find_package(Lua51 REQUIRED)

if (NOT LUA51_FOUND)
message(FATAL_ERROR "Lua5.1 libraries not found")
else()
message(STATUS "Lua include directory: ${LUA_INCLUDE_DIR}")
message(STATUS "Lua libraries: ${LUA_LIBRARIES}")
endif()

if(NOT LUA_INCLUDE_DIR)
message(FATAL_ERROR "Lua headers not found")
else()
message(STATUS "Lua include directory: ${LUA_INCLUDE_DIR}")
endif()

include_directories("${LUA_INCLUDE_DIR}")

file(GLOB_RECURSE SRCS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")

add_executable(lsr ${SRCS})

target_include_directories(lsr PRIVATE ${LUA_INCLUDE_DIR})
target_link_libraries(lsr PRIVATE ${LUA_LIBRARIES})

add_custom_target(format COMMAND clang-format -i ${SRCS})

set(LUA_SCRIPTS "${CMAKE_CURRENT_SOURCE_DIR}/lua/*.lua")

set(LUA_INSTALL_PATH "/usr/local/share/lsr")

add_custom_command(TARGET lsr POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${LUA_INSTALL_PATH}"
COMMAND ${CMAKE_COMMAND} -E copy ${LUA_SCRIPTS} "${LUA_INSTALL_PATH}"
COMMENT "Copying ${LUA_SCRIPTS} to ${LUA_INSTALL_PATH}"
)

install(TARGETS lsr DESTINATION /usr/local/bin)

if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)

add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake

COMMAND ${CMAKE_COMMAND} -E remove_directory "${LUA_INSTALL_PATH}"
COMMENT "Removing Lua scripts from ${LUA_INSTALL_PATH}"
)
endif()

151 changes: 151 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<div align="center">

# Configure laser!

</div>

## Table of contents
1. [Creating Your Own Configuration File](#creating-your-own-configuration-file)
2. [Writing Your Own Configuration](#writing-your-own-configuration)
1. [Constants](#constants)
1. [`L_colors`](#`l_colors`)
2. [Functions](#functions)
1. [`L_compare_entries()`](#`l_compare_entries()`)
2. [`L_long_format()`](#`l_long_format()`)
3. [Using Defaults](#using-defaults)

Laser is highly customizable through Lua scripts. The default configuration
file is located at `/usr/local/share/lsr/lsr.lua`. This file is loaded by the
program and sets the default behavior for laser. You can modify this file or
create your own configuration file to change the behavior of laser.

## Creating Your Own Configuration File

To create your own configuration all that you need to do is to create a lua
file at `~/.config/lsr/lsr.lua`. This will be the default file where laser will
look for your configuration. You can of course add more files to this directory
(check the `lua` directory in the repo). Other files in this directory will not
be loaded by laser but you can do a `require` to them to load them into the
`lsr.lua` file.

## Writing Your Own Configuration

The default configuration file is located at `/usr/local/share/lsr/lsr.lua`.
You can copy these files and edit them to your liking. But here comes a small
guide on what you can do with a blank `lsr.lua` file.

> [!NOTE]
> All laser-specific functions and constants are prefixed with `L_`.

### Constants

#### `L_colors`

You can configure the colors of the different entry types by modifying the
global `L_colors` table. You can even add glyphs or text as it is just a normal
string. The definition of the table should be placed in the `lsr.lua` file
(check the [Writing Your Own Configuration](#writing-your-own-configuration)).
But if you check the `lua` directory in the repo you will find the default
colors are placed in a different file. You can do this by doing a
`require("colors")` in the `lsr.lua` (assuming that the `colors.lua` file is in
the same directory).

The default configuration of `L_colors` is:
```lua
L_colors = {
DIR = "\x1b[34m",
SYMLINK = "\x1b[36m",
FILE = "\x1b[0m",
HIDDEN = "\x1b[90m",
EXEC = "\x1b[32;4m",
ARCHIVE = "\x1b[31m",
MEDIA = "\x1b[33m",
DOCUMENT = "\x1b[35;3m",
}
```

### Functions

There are a few functions you can use to modify the behavior of the program.

#### `L_compare_entries()`

This function is used to sort the entries in the directory. This is by default
used to add "gravity" to files so that they fall at the bottom of the list. And
directories being placed at the top.

This fcuntion have the following signature:

```lua
L_compare_entries(entry1, entry2, entry1_is_dir, entry2_is_dir)
```

```lua
-- files should get "gravity" and fall to the bottom of the list
-- then the entries are sorted alphabetically
function L_compare_entries(entry1, entry2, entry1_is_dir, entry2_is_dir)
if entry1_is_dir and not entry2_is_dir then return -1 end
if not entry1_is_dir and entry2_is_dir then return 1 end

return entry1 < entry2 and -1 or (entry1 > entry2 and 1 or 0)
end
```

#### `L_long_format()`

This function is used to format the entries in long format. This is by default
used to show the permissions, the size, last modified and owner of the entry.

This fcuntion have the following signature:

```lua
L_long_format(entry, longest_name)
```

Where `entry` is a table with the following keys:

- `name`: The name of the entry
- `mode`: The c stat mode of the entry (this can be used to get the permissions
and do much more, there is a default util function called
`utils.getPerms(mode)`)
- `size`: The size of the entry (there is a default util function called
`utils.formatSize(size)` to make it human readable)
- `mtime`: The last modified time of the entry you can pass it through
`os.date` to format it
- `owner`: The name of the owner of the entry
- `type`: The type of the entry in one character
- `d`: Directory
- `l`: Symlink
- `c`: Character device
- `b`: Block device
- `p`: Named pipe (fifo)
- `s`: Socket
- `-`: File

The `longest_name` is the length of the longest name in the directory. This
could be used to align the output.

The default function is:

```lua
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 owner = string.format("%-" .. longest_name .. "s ", entry.owner)

return string.format("%s %s%s %s%s %s%s ",
perms,
L_colors.SYMLINK,
last_modified, L_colors.MEDIA, size,
L_colors.SYMLINK, owner)
end
```

### Using Defaults

If you do not define these functions in your configuration file, the default
functions from the default configuration file will be used automatically. If
there is a missing constant or function the default configuration file will be
used.
26 changes: 16 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ See the [Table of Contents](#table-of-contents) for different ways to help and
details about how this project handles them. Please make sure to read the
relevant section before making your contribution.

## Table of Contents

- [I Have a Question](#i-have-a-question)
- [I Want To Contribute](#i-want-to-contribute)
- [Reporting Bugs](#reporting-bugs)
- [Suggesting Enhancements](#suggesting-enhancements)
- [Your First Code Contribution](#your-first-code-contribution)
- [Improving The Documentation](#improving-the-documentation)
- [Styleguides](#styleguides)
- [Commit Messages](#commit-messages)
## Table of contents
1. [I Have a Question](#i-have-a-question)
2. [I Want To Contribute](#i-want-to-contribute)
1. [Reporting Bugs](#reporting-bugs)
1. [Before Submitting a Bug Report](#before-submitting-a-bug-report)
2. [Suggesting Enhancements](#suggesting-enhancements)
1. [Before Submitting an
Enhancement](#before-submitting-an-enhancement)
2. [How Do I Submit a Good Enhancement
Suggestion?](#how-do-i-submit-a-good-enhancement-suggestion?)
3. [Your First Code Contribution](#your-first-code-contribution)
1. [Development Environment Setup](#development-environment-setup)
2. [Code formatting](#code-formatting)
4. [Improving The Documentation](#improving-the-documentation)
3. [Styleguides](#styleguides)
1. [Commit Messages](#commit-messages)

## I Have a Question

Expand Down
67 changes: 0 additions & 67 deletions Makefile

This file was deleted.

44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@

</div>

## Table of contents
1. [Installation](#installation)
1. [From Homebrew](#from-homebrew)
2. [Building from source](#building-from-source)
2. [Usage](#usage)
1. [Configure](#configure)
2. [Command-line options](#command-line-options)
1. [Contributing](#contributing)
2. [Authors](#authors)

**lsr** basically `ls` but with colorization and sorting for better readability.
It offers filtering options, making it easy to
locate specific files and folders (with grep you'll find exactly what you need)
It can even display the directory contents in a tree-like structure! :fire:
If that wasn't enough you can even extend the program with lua!
(I'm too proud of that )

## Installation

Expand All @@ -38,13 +50,15 @@ cd laser
Install system-wide:

```sh
sudo make install
cmake -S . -B build
cmake --build build
sudo cmake --install build
```

To uninstall you can run
To uninstall you can run the following while being in the `laser` directory:

```sh
sudo make uninstall
cmake --build build --target uninstall
```

> [!NOTE]
Expand All @@ -63,25 +77,17 @@ lsr
lsr some-directory
```

### Change colors or add icons
### Configure

If you want to change the colors of the output you can set an environment
variable called `LSR_COLORS` for example if you want to add nerd font icons to
the existing colors you can use:
If you'd like to configure the program's behavior, or change the default colors,
add icons and more. You can configure the program with lua :fire:. A default
configuration will be installed at `/usr/local/share/lsr/`. But if you want to
write some own configuration you can place it in `~/.config/lsr/lsr.lua`.

```sh
export LSR_COLORS="DIR=\x1b[34m :\
SYMLINK=\x1b[36m :\
FILE=\x1b[0m :\
HIDDEN=\x1b[90m :\
EXEC=\x1b[32;4m :\
ARCHIVE=\x1b[31m :\
MEDIA=\x1b[33m :\
DOCUMENT=\x1b[35;3m :"
```
Copy the files from `/usr/local/share/lsr/` to `~/.config/lsr/` and you can edit
them now.

You don't have to change all of these if there is only one you want to change
you can do that, to leave the rest on default
Check out the [configuration guide](/CONFIGURATION.md)

### Command-line options

Expand Down
Loading
Loading