From ec04e57c338b282c9b85127675d4a4e790842d98 Mon Sep 17 00:00:00 2001 From: lwad Date: Sat, 25 May 2024 19:12:23 +0100 Subject: [PATCH] feat: Add optional focus action to tell function Also, move it to general-graphical config. --- lwad/general-graphical.nix | 35 ++++++++++++++++++++++++++++++++++- lwad/general.nix | 25 ------------------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lwad/general-graphical.nix b/lwad/general-graphical.nix index 37009f3..9ea3e89 100644 --- a/lwad/general-graphical.nix +++ b/lwad/general-graphical.nix @@ -450,7 +450,40 @@ in { }; }; programs = { - bash.shellAliases.xclip = "${pkgs.xclip}/bin/xclip -selection clipboard"; + bash = { + shellAliases.xclip = "${pkgs.xclip}/bin/xclip -selection clipboard"; + interactiveShellInit = '' + join_by() { + local d=''${1-} f=''${2-} + if shift 2; then + ${pkgs.coreutils}/bin/printf %s "$f" "''${@/#/$d}" + fi + } + + tell() { + session_name="$(${pkgs.tmux}/bin/tmux list-clients -t "$TMUX_PANE" -F "#{session_name}" | ${pkgs.coreutils}/bin/head -n 1)" + + tree="$(i3-msg -t get_tree)" + window_id="$(jq -e '.nodes[].nodes[].nodes[].nodes | map(select(.focused)) | .[].window' <<< "$tree")" + if test -z "$window_id"; then + window_id="$(jq -e '.nodes[].nodes[].nodes[].floating_nodes[].nodes | map(select(.focused)) | .[].window' <<< "$tree")" + fi + + if eval "$@"; then + icon="data-information" + else + icon="data-error" + fi + + result="$(${pkgs.dunst}/bin/dunstify -a "longCommand" -u normal -i "$icon" -A 1,focus "$session_name" "$(join_by " " $@)")" + if test "$result" -eq 1; then + i3-msg "[id=$window_id] focus" + fi + } + + complete -F _command tell + ''; + }; wireshark.enable = true; }; services.unclutter-xfixes = { diff --git a/lwad/general.nix b/lwad/general.nix index cf82a5b..2897d1a 100644 --- a/lwad/general.nix +++ b/lwad/general.nix @@ -205,31 +205,6 @@ ./.comma $@ fi } - - tell() { - join_by() { - local d=''${1-} f=''${2-} - if shift 2; then - ${pkgs.coreutils}/bin/printf %s "$f" "''${@/#/$d}" - fi - } - - session_name="$(${pkgs.tmux}/bin/tmux list-clients -t "$TMUX_PANE" -F "#{session_name}" | ${pkgs.coreutils}/bin/head -n 1)" - - if eval "$@"; then - icon="data-information" - else - icon="data-error" - fi - - ${pkgs.dunst}/bin/dunstify -a "longCommand" \ - -u normal \ - -i "$icon" \ - "$session_name" \ - "$(join_by " " $@)" - } - - complete -F _command tell '') ]); shellAliases = let eza = "${pkgs.eza}/bin/eza";