From b30b88d2614cecb7826aadbf8c0aab6bd4215e82 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Wed, 22 Apr 2026 19:15:58 +0530 Subject: added scripts to show keybindings for i3 --- flake.nix | 3 -- modules/desktop/i3/default.nix | 10 +++++- modules/desktop/i3/keybindings.nix | 2 +- modules/scripts/default.nix | 38 ++++++++++++++++----- modules/scripts/i3-keybindings.sh | 67 ++++++++++++++++++++++++++++++++++++++ utils/default.nix | 3 -- utils/scripts.nix | 33 ------------------- 7 files changed, 107 insertions(+), 49 deletions(-) create mode 100644 modules/scripts/i3-keybindings.sh delete mode 100644 utils/default.nix delete mode 100644 utils/scripts.nix diff --git a/flake.nix b/flake.nix index b715b61..2ea5d88 100644 --- a/flake.nix +++ b/flake.nix @@ -49,9 +49,6 @@ in home-manager.lib.homeManagerConfiguration { inherit pkgs; - extraSpecialArgs = { - utils = import ./utils; - }; modules = [ ./users/arpit.nix ./modules diff --git a/modules/desktop/i3/default.nix b/modules/desktop/i3/default.nix index 3f96aaf..caa8cc3 100644 --- a/modules/desktop/i3/default.nix +++ b/modules/desktop/i3/default.nix @@ -82,7 +82,15 @@ } ]; - keybindings = lib.mkOptionDefault (import ./keybindings.nix { inherit pkgs lib config; }); + keybindings = lib.mkOptionDefault ( + import ./keybindings.nix { + inherit + pkgs + lib + config + ; + } + ); }; extraConfig = '' exec_always --no-startup-id xrandr -q | grep -w 'connected' | cut -d' ' -f1 | xargs -I{} i3-msg "workspace 1 output {}" diff --git a/modules/desktop/i3/keybindings.nix b/modules/desktop/i3/keybindings.nix index b635161..122b7e3 100644 --- a/modules/desktop/i3/keybindings.nix +++ b/modules/desktop/i3/keybindings.nix @@ -18,7 +18,7 @@ in lib.mkIf (config.tools.kitty.enable && config.tools.lf.enable) "exec ${lib.getExe pkgs.kitty} --title 'File Manager' --class file-explorer -e ${lib.getExe config.programs.lf.package}"; "${mod}+s" = - lib.mkIf config.tools.kitty.enable "exec ${lib.getExe pkgs.kitty} --title 'Keybindings' --class floating-termial -e keybindings.sh"; + lib.mkIf config.tools.kitty.enable "exec ${lib.getExe pkgs.kitty} --title 'Keybindings' --class floating-termial -e ${lib.getExe config.scripts.i3-keybindings}"; "${mod}+q" = "kill"; # --- Media / Hardware Keys --- diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix index c70c3a0..a16cb8d 100644 --- a/modules/scripts/default.nix +++ b/modules/scripts/default.nix @@ -2,24 +2,46 @@ config, lib, pkgs, - utils, ... }: let - scriptUtils = utils.scripts { inherit pkgs lib config; }; + shell = + if config.tools.zsh.enable then (lib.getExe config.programs.zsh.package) else "/usr/bin/env sh"; + + mkScript = + name: path: env: + let + envVars = lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n}=\"${toString v}\"") env); + in + pkgs.writeTextFile { + name = name; + executable = true; + destination = "/bin/${name}"; + text = '' + #!${shell} + ${envVars} + ${builtins.readFile path} + ''; + }; scripts = { - deep-clean = scriptUtils.mkScript "deep-clean" ./deep-clean.sh { }; + deep-clean = mkScript "deep-clean" ./deep-clean.sh { }; fzf-launcher = lib.mkIf config.tools.fzf.enable ( - scriptUtils.mkScript "fzf-launcher" ./fzf-launcher.sh { + mkScript "fzf-launcher" ./fzf-launcher.sh { FZF = lib.getExe pkgs.fzf; SETSID = lib.getExe' pkgs.util-linux "setsid"; } ); + i3-keybindings = lib.mkIf config.desktop.enable ( + mkScript "i3-keybindings" ./i3-keybindings.sh { + + } + ); + screen-recording = lib.mkIf (config.tools.ffmpeg.enable && config.tools.slop.enable) ( - scriptUtils.mkScript "screen-recording" ./screen-recording.sh { + mkScript "screen-recording" ./screen-recording.sh { FFMPEG = lib.getExe pkgs.ffmpeg-full; SLOP = lib.getExe pkgs.slop; } @@ -34,7 +56,7 @@ let && config.tools.kitty.enable ) ( - scriptUtils.mkScript "system-monitor" ./system-monitor.sh { + mkScript "system-monitor" ./system-monitor.sh { BTM = lib.getExe pkgs.bottom; NVTOP = lib.getExe pkgs.nvtopPackages.full; TMUX = lib.getExe pkgs.tmux; @@ -43,7 +65,7 @@ let ); vpn-connect = lib.mkIf (config.tools.openvpn.enable && config.tools.fzf.enable) ( - scriptUtils.mkScript "vpn-connect" ./vpn-connect.sh { + mkScript "vpn-connect" ./vpn-connect.sh { FZF = lib.getExe pkgs.fzf; OPENVPN = lib.getExe pkgs.openvpn; SYSTEMD_RESOLVED = "${pkgs.openvpn}/libexec/update-systemd-resolved"; @@ -51,7 +73,7 @@ let ); vpn-disconnect = lib.mkIf (config.tools.openvpn.enable && config.tools.fzf.enable) ( - scriptUtils.mkScript "vpn-disconnect" ./vpn-disconnect.sh { } + mkScript "vpn-disconnect" ./vpn-disconnect.sh { } ); }; in diff --git a/modules/scripts/i3-keybindings.sh b/modules/scripts/i3-keybindings.sh new file mode 100644 index 0000000..22ddef4 --- /dev/null +++ b/modules/scripts/i3-keybindings.sh @@ -0,0 +1,67 @@ +# Path to your i3 config file +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 diff --git a/utils/default.nix b/utils/default.nix deleted file mode 100644 index b4dac37..0000000 --- a/utils/default.nix +++ /dev/null @@ -1,3 +0,0 @@ -{ - scripts = import ./scripts.nix; -} diff --git a/utils/scripts.nix b/utils/scripts.nix deleted file mode 100644 index 87639c6..0000000 --- a/utils/scripts.nix +++ /dev/null @@ -1,33 +0,0 @@ -{ - lib, - pkgs, - config, -}: - -{ - # We define mkScript as a function that takes the context it needs - mkScript = - name: path: env: - let - # Logic inside the helper - defaultShell = - if (config.tools.zsh.enable or false) then - (lib.getExe config.programs.zsh.package) - else - "/usr/bin/env sh"; - - envVars = lib.concatStringsSep "\n" ( - lib.mapAttrsToList (n: v: "export ${n}=\"${toString v}\"") env - ); - in - pkgs.writeTextFile { - inherit name; - executable = true; - destination = "/bin/${name}"; - text = '' - #!${defaultShell} - ${envVars} - ${builtins.readFile path} - ''; - }; -} -- cgit v1.2.3