fish: use zs over zmx -select funcs, add name gen

Amolith created

Change summary

dot_config/private_fish/config.fish.tmpl                     |   8 
dot_config/private_fish/functions/__zmx_choose_session.fish  |   8 
dot_config/private_fish/functions/__zmx_format_sessions.fish |   5 
dot_config/private_fish/functions/friendly-name.fish         | 120 ++++++
dot_config/private_fish/functions/zmx-select.fish            |  11 
dot_config/private_fish/functions/zs.fish                    |  62 +++
dot_config/private_fish/functions/zssh-select.fish           |  41 --
7 files changed, 199 insertions(+), 56 deletions(-)

Detailed changes

dot_config/private_fish/config.fish.tmpl 🔗

@@ -92,6 +92,10 @@ fundle plugin 'patrickf1/fzf.fish'
 fundle plugin 'franciscolourenco/done'
 fundle init
 
+{{- if or (eq .chezmoi.hostname "angmar") (eq .chezmoi.hostname "sidhe") (eq .chezmoi.username "exedev") }}
+set ZMX_AUTO_SELECT true
+{{- end }}
+
 if status is-interactive
 	if not functions -q fundle; eval (curl -sfL https://git.io/fundle-install); end
 
@@ -139,6 +143,10 @@ if status is-interactive
     sb completion fish | source
 
     enable_transience
+
+    if set -q ZMX_AUTO_SELECT; and not set -q ZMX_SESSION; and type -q zs
+        zs; and exit
+    end
 end
 
 if status is-login

dot_config/private_fish/functions/__zmx_choose_session.fish 🔗

@@ -37,8 +37,12 @@ function __zmx_choose_session --description "Choose or create a zmx session name
     set -l selected (sed -n '3p' $output_file)
     rm -f $output_file
 
-    if test "$key" = ctrl-n; and test -n "$query"
-        echo $query
+    if test "$key" = ctrl-n
+        if test -n "$query"
+            echo $query
+        else
+            printf "%s" $display | awk '{print $1}' | friendly-name --exclude-stdin
+        end
     else if test $fzf_status -eq 0; and test -n "$selected"
         string split -f1 ' ' -- $selected
     else if test -n "$query"

dot_config/private_fish/functions/__zmx_format_sessions.fish 🔗

@@ -1,15 +1,16 @@
 function __zmx_format_sessions --description "Format zmx list output for fzf"
     while read -l line
+        set line (string trim -- $line)
         set -l fields (string split \t -- $line)
 
         if test (count $fields) -lt 5
             continue
         end
 
-        set -l name (string replace -r '^session_name=' '' -- $fields[1])
+        set -l name (string replace -r '^(session_)?name=' '' -- $fields[1])
         set -l pid (string replace -r '^pid=' '' -- $fields[2])
         set -l clients (string replace -r '^clients=' '' -- $fields[3])
-        set -l dir (string replace -r '^started_in=' '' -- $fields[5])
+        set -l dir (string replace -r '^(start_dir|started_in)=' '' -- $fields[5])
 
         printf "%-20s  pid:%-8s  clients:%-2s  %s\n" $name $pid $clients $dir
     end

dot_config/private_fish/functions/friendly-name.fish 🔗

@@ -0,0 +1,120 @@
+function friendly-name --description "Emit random friendly adjective-noun names"
+    argparse 'h/help' 'n/count=' 'e/exclude=' 'exclude-file=' 'exclude-stdin' -- $argv
+    or return 1
+
+    if set -q _flag_help
+        echo "Usage: friendly-name [OPTIONS]"
+        echo
+        echo "Emit random friendly adjective-noun names."
+        echo
+        echo "Options:"
+        echo "  -n, --count COUNT       Number of names to emit (default: 1)"
+        echo "  -e, --exclude NAME      Exclude a specific name; may be repeated"
+        echo "      --exclude-file PATH Read newline-delimited excludes from file"
+        echo "      --exclude-stdin     Read newline-delimited excludes from stdin"
+        echo "  -h, --help              Show this help message"
+        return 0
+    end
+
+    set -l adjectives \
+        ancient autumn bitter billowing black blue bold brave broken calm \
+        clever cold cool crimson damp dark dawn delicate dry eager \
+        empty falling fragrant frosty fuzzy gentle golden green hidden holy \
+        icy keen late light lingering little lively long lucky merry \
+        misty muddy nameless noble old patient purple quiet red rough \
+        shy silent small sparkling spring steady still swift twilight wandering \
+        warm weathered white wild winter wispy withered young zippy \
+        bright brisk crystal dusty faint fierce floral free fresh \
+        great happy high huge kind loud majestic mild pale \
+        pleasant polished proud quick ripe rolling rose royal \
+        serene sharp silver sleek smooth snowy soft spiced \
+        stout strange strong summer sunny sweet tall tender \
+        tiny vast velvet vivid wide wondrous
+
+    set -l nouns \
+        aurora badger beagle breeze brook cabin canyon cedar cliff cloud \
+        comet coral crane creek delta dew dune eagle ember feather \
+        fern field finch fire flora forest fox frog frost gazelle \
+        glacier grass grove haven haze hill island kite lagoon lake \
+        leaf lemur lily lynx marsh meadow mesa mist moon moose \
+        mountain oak oasis otter owl panda peak pebble pine pond \
+        rain ridge ripple river shadow sky smoke snow star stone \
+        stream sun thunder tree tundra valley vortex wave wind \
+        ash basin bear birch bloom blossom boulder cherry \
+        cove crystal daisy deer echo elk fawn flame flower \
+        gale garden geode glen gorge gull hawk heron iris \
+        ivy jay jewel lark loon lotus lupine maple moss \
+        nest orchid osprey path petal plum poppy prairie \
+        quartz quail rabbit reef robin rose sage sail sequoia \
+        shell shore shrew sparrow spruce storm summit swallow \
+        swan swift thistle thorn thrush tide trail trout \
+        tulip violet willow wing wren
+
+    set -l count 1
+    if set -q _flag_count
+        set count $_flag_count
+
+        if not string match -qr '^[1-9][0-9]*$' -- $count
+            echo "friendly-name: --count must be a positive integer" >&2
+            return 1
+        end
+    end
+
+    set -l excludes
+    if set -q _flag_exclude
+        set -a excludes $_flag_exclude
+    end
+
+    if set -q _flag_exclude_file
+        if not test -r "$_flag_exclude_file"
+            echo "friendly-name: cannot read exclude file '$_flag_exclude_file'" >&2
+            return 1
+        end
+
+        while read -l line
+            test -n "$line"; and set -a excludes $line
+        end <"$_flag_exclude_file"
+    end
+
+    if set -q _flag_exclude_stdin
+        while read -l line
+            test -n "$line"; and set -a excludes $line
+        end
+    end
+
+    set -l max_combinations (math (count $adjectives) \* (count $nouns))
+    if test (math $max_combinations - (count $excludes)) -lt $count
+        echo "friendly-name: not enough unique combinations available" >&2
+        return 1
+    end
+
+    set -l emitted
+    set -l emitted_count 0
+    set -l max_attempts 1000
+
+    while test $emitted_count -lt $count
+        set -l attempts 0
+
+        while true
+            set -l adjective $adjectives[(random 1 (count $adjectives))]
+            set -l noun $nouns[(random 1 (count $nouns))]
+            set -l name "$adjective-$noun"
+
+            if contains -- $name $excludes; or contains -- $name $emitted
+                set attempts (math $attempts + 1)
+
+                if test $attempts -ge $max_attempts
+                    echo "friendly-name: could not generate a unique name after $max_attempts attempts" >&2
+                    return 1
+                end
+
+                continue
+            end
+
+            echo $name
+            set -a emitted $name
+            set emitted_count (math $emitted_count + 1)
+            break
+        end
+    end
+end

dot_config/private_fish/functions/zmx-select.fish 🔗

@@ -1,11 +0,0 @@
-function zmx-select --description "Fuzzy-find or create a zmx session"
-    if not type -q zmx
-        echo "zmx-select: zmx is not installed or not on PATH" >&2
-        return 127
-    end
-
-    set -l session_name (zmx list 2>/dev/null | __zmx_format_sessions | __zmx_choose_session --preview 'zmx history {1}')
-    or return $status
-
-    zmx attach $session_name
-end

dot_config/private_fish/functions/zs.fish 🔗

@@ -0,0 +1,62 @@
+function zs --description "Fuzzy-find or create a local or remote zmx session"
+    argparse 'h/help' 'install' -- $argv
+    or return 1
+
+    if set -q _flag_help; or test (count $argv) -gt 1
+        echo "Usage: zs [--install] [[user@]host]"
+        echo "Examples:"
+        echo "  zs"
+        echo "  zs hel1"
+        echo "  zs --install finder-oscar.exe.xyz"
+        return 0
+    end
+
+    if test (count $argv) -eq 0
+        if set -q _flag_install
+            echo "zs: --install only applies when selecting sessions on a remote host" >&2
+            return 2
+        end
+
+        if not type -q zmx
+            echo "zs: zmx is not installed or not on PATH" >&2
+            return 127
+        end
+
+        set -l session_name (zmx list 2>/dev/null | __zmx_format_sessions | __zmx_choose_session --preview 'zmx history {1}')
+        or return $status
+
+        zmx attach $session_name
+        return $status
+    end
+
+    set -l destination $argv[1]
+
+    if not string match -qr '^[A-Za-z0-9._@:%+-]+$' -- $destination
+        echo "zs: destination contains characters that cannot be safely used in the preview command" >&2
+        return 2
+    end
+
+    mkdir -p "$HOME/.ssh/controlmasters"
+
+    if set -q _flag_install
+        zmx-remote-install $destination
+        or return $status
+    else
+        zmx-remote-install --upgrade-only $destination
+        or return $status
+    end
+
+    set -l remote_list (command ssh \
+        -o ControlMaster=auto \
+        -o ControlPersist=10m \
+        -o ControlPath="$HOME/.ssh/controlmasters/%C" \
+        $destination \
+        "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; command -v zmx >/dev/null 2>&1 || { echo \"zs: zmx is not installed on the remote host or is not on PATH; retry with zs --install HOST\" >&2; exit 127; }; zmx list 2>/dev/null'")
+    or return $status
+
+    set -l preview_command "ssh -o ControlMaster=auto -o ControlPersist=10m -o ControlPath=$HOME/.ssh/controlmasters/%C $destination 'sh -c '\''PATH=\"\$HOME/.local/bin:\$PATH\"; zmx history {1}'\'' '"
+    set -l session_name (printf '%s\n' $remote_list | __zmx_format_sessions | __zmx_choose_session --preview $preview_command)
+    or return $status
+
+    zssh $destination $session_name
+end

dot_config/private_fish/functions/zssh-select.fish 🔗

@@ -1,41 +0,0 @@
-function zssh-select --description "Fuzzy-find or create a zmx session on a remote host"
-    argparse 'h/help' 'install' -- $argv
-    or return 1
-
-    if set -q _flag_help; or test (count $argv) -ne 1
-        echo "Usage: zssh-select [--install] [user@]host"
-        echo "Example: zssh-select hel1"
-        return 0
-    end
-
-    set -l destination $argv[1]
-
-    if not string match -qr '^[A-Za-z0-9._@:%+-]+$' -- $destination
-        echo "zssh-select: destination contains characters that cannot be safely used in the preview command" >&2
-        return 2
-    end
-
-    mkdir -p "$HOME/.ssh/controlmasters"
-
-    if set -q _flag_install
-        zmx-remote-install $destination
-        or return $status
-    else
-        zmx-remote-install --upgrade-only $destination
-        or return $status
-    end
-
-    set -l remote_list (command ssh \
-        -o ControlMaster=auto \
-        -o ControlPersist=10m \
-        -o ControlPath="$HOME/.ssh/controlmasters/%C" \
-        $destination \
-        "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; command -v zmx >/dev/null 2>&1 || { echo \"zssh-select: zmx is not installed on the remote host or is not on PATH; retry with zssh-select --install\" >&2; exit 127; }; zmx list 2>/dev/null'")
-    or return $status
-
-    set -l preview_command "ssh -o ControlMaster=auto -o ControlPersist=10m -o ControlPath=$HOME/.ssh/controlmasters/%C $destination 'sh -c '\''PATH=\"\$HOME/.local/bin:\$PATH\"; zmx history {1}'\'' '"
-    set -l session_name (printf '%s\n' $remote_list | __zmx_format_sessions | __zmx_choose_session --preview $preview_command)
-    or return $status
-
-    zssh $destination $session_name
-end