From 03bd9b6ad5335aff65d9cb6fb1d117b5cb4b7919 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Sat, 18 Jul 2026 14:55:21 +0530 Subject: refactor(scripts): split monolithic module into per-script submodules Extract shared `mkScript` and `mkScriptModule` helpers into `lib.nix` Split each script into `modules/scripts//` with its own `default.nix`, `script.sh`, and `assertions.nix` Remove `conditions` parameter from `mkScriptModule`; move condition assertions into per-script `assertions.nix` files with inline checks and error messages Simplify `mkScriptModule` to return `{ options, moduleConfig }` instead of a module function, taking `config` as a direct parameter Add `description` field to `mkScript` that inserts the description as a `# ` comment between the shebang and env vars Thread `description` through `mkScriptModule` to each `default.nix` Move top comment lines from each `script.sh` into the `description` field and remove from sources to avoid duplication Remove old flat `.sh` files from `modules/scripts/` root --- modules/scripts/i3-keybindings/assertions.nix | 11 +++++ modules/scripts/i3-keybindings/default.nix | 30 ++++++++++++ modules/scripts/i3-keybindings/script.sh | 66 +++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 modules/scripts/i3-keybindings/assertions.nix create mode 100644 modules/scripts/i3-keybindings/default.nix create mode 100644 modules/scripts/i3-keybindings/script.sh (limited to 'modules/scripts/i3-keybindings') diff --git a/modules/scripts/i3-keybindings/assertions.nix b/modules/scripts/i3-keybindings/assertions.nix new file mode 100644 index 0000000..ce1dd6b --- /dev/null +++ b/modules/scripts/i3-keybindings/assertions.nix @@ -0,0 +1,11 @@ +{ config, ... }: +[ + { + assertion = !config.scripts.i3-keybindings.enable || config.desktop.enable; + message = "scripts.i3-keybindings is enabled but requires `desktop.enable`."; + } + { + assertion = !config.scripts.i3-keybindings.enable || config.terminal.less.enable; + message = "scripts.i3-keybindings is enabled but requires `terminal.less.enable`."; + } +] diff --git a/modules/scripts/i3-keybindings/default.nix b/modules/scripts/i3-keybindings/default.nix new file mode 100644 index 0000000..e1be7fe --- /dev/null +++ b/modules/scripts/i3-keybindings/default.nix @@ -0,0 +1,30 @@ +{ + config, + lib, + pkgs, + ... +}: +let + inherit ((import ../lib.nix { inherit lib pkgs; })) mkScriptModule; + base = mkScriptModule { + name = "i3-keybindings"; + path = ./script.sh; + description = "Path to your i3 config file"; + deps = [ + pkgs.coreutils + pkgs.gawk + pkgs.ncurses + ]; + desktop = { + enable = true; + displayName = "i3 Keybindings"; + }; + inherit config; + }; +in +{ + options = base.options; + config = base.moduleConfig // { + assertions = base.moduleConfig.assertions ++ (import ./assertions.nix { inherit config lib; }); + }; +} diff --git a/modules/scripts/i3-keybindings/script.sh b/modules/scripts/i3-keybindings/script.sh new file mode 100644 index 0000000..326e8bd --- /dev/null +++ b/modules/scripts/i3-keybindings/script.sh @@ -0,0 +1,66 @@ +I3_CONFIG="$HOME/.config/i3/config" + +# Function to wrap text +wrap_text() { + local text="$1" + local width="$2" + echo "$text" | fold -s -w "$width" +} + +# Get terminal width and calculate columns +terminal_width=$(tput cols) +table_width=$((terminal_width - 4)) +max_keybinding_length=$((table_width / 3)) # Keybindings are usually shorter in i3 +max_keybinding_content_length=$((max_keybinding_length - 2)) +description_width=$((table_width - max_keybinding_length - 1)) +description_content_width=$((description_width - 2)) + +# Border lines +top_border="┌$(printf '%.0s─' $(seq 1 $max_keybinding_length))┬$(printf '%.0s─' $(seq 1 $description_width))┐" +row_sep="├$(printf '%.0s─' $(seq 1 $max_keybinding_length))┼$(printf '%.0s─' $(seq 1 $description_width))┤" +bottom_border="└$(printf '%.0s─' $(seq 1 $max_keybinding_length))┴$(printf '%.0s─' $(seq 1 $description_width))┘" + +{ + echo "$top_border" + printf "│ %-*s │ %-*s │\n" "$max_keybinding_content_length" "KEYBINDING" "$description_content_width" "DESCRIPTION" + + last_comment="" + + # Read i3 config line by line + while IFS= read -r line; do + # If line is a comment, store it as a potential description + if [[ $line =~ ^#\ (.*) ]]; then + last_comment="${BASH_REMATCH[1]}" + + # If line contains a bindsym + elif [[ $line =~ ^bindsym ]]; then + # Extract the key combo (the parts between 'bindsym' and the start of the command/exec) + # This regex captures bindsym [options] + keybinding=$(echo "$line" | awk '{print $2}') + + # If the comment is empty, use the command itself as the description + if [[ -z "$last_comment" ]]; then + display_desc=$(echo "$line" | cut -d' ' -f3-) + else + display_desc="$last_comment" + fi + + wrapped_description=$(wrap_text "$display_desc" "$description_content_width") + + echo "$row_sep" + + # Print first line of binding and description + printf "│ %-*s │ %-*s │\n" "$max_keybinding_content_length" "$keybinding" "$description_content_width" "$(echo "$wrapped_description" | head -n 1)" + + # Print subsequent lines of wrapped description + echo "$wrapped_description" | sed '1d' | while read -r continuation; do + printf "│ %-*s │ %-*s │\n" "$max_keybinding_content_length" "" "$description_content_width" "$continuation" + done + + # Reset comment after use + last_comment="" + fi + done < "$I3_CONFIG" + + echo "$bottom_border" +} | less -R -- cgit v1.2.3