repos / zmx

session persistence for terminal processes
git clone https://github.com/neurosnap/zmx.git

zmx / src
Jay Botte  ·  2026-04-21

completions.zig

  1const std = @import("std");
  2
  3pub const Shell = enum {
  4    bash,
  5    zsh,
  6    fish,
  7
  8    pub fn fromString(s: []const u8) ?Shell {
  9        if (std.mem.eql(u8, s, "bash")) return .bash;
 10        if (std.mem.eql(u8, s, "zsh")) return .zsh;
 11        if (std.mem.eql(u8, s, "fish")) return .fish;
 12
 13        return null;
 14    }
 15
 16    pub fn getCompletionScript(self: Shell) []const u8 {
 17        return switch (self) {
 18            .bash => bash_completions,
 19            .zsh => zsh_completions,
 20            .fish => fish_completions,
 21        };
 22    }
 23};
 24
 25const bash_completions =
 26    \\_zmx_completions() {
 27    \\  local cur prev words cword
 28    \\  COMPREPLY=()
 29    \\  cur="${COMP_WORDS[COMP_CWORD]}"
 30    \\  prev="${COMP_WORDS[COMP_CWORD-1]}"
 31    \\
 32    \\  local commands="attach run send detach list completions kill history version help"
 33    \\
 34    \\  if [[ $COMP_CWORD -eq 1 ]]; then
 35    \\    COMPREPLY=($(compgen -W "$commands" -- "$cur"))
 36    \\    return 0
 37    \\  fi
 38    \\
 39    \\  case "$prev" in
 40    \\    attach|run|send|kill|history)
 41    \\      local sessions=$(zmx list --short 2>/dev/null | tr '\n' ' ')
 42    \\      COMPREPLY=($(compgen -W "$sessions" -- "$cur"))
 43    \\      ;;
 44    \\    completions)
 45    \\      COMPREPLY=($(compgen -W "bash zsh fish" -- "$cur"))
 46    \\      ;;
 47    \\    list)
 48    \\      COMPREPLY=($(compgen -W "--short" -- "$cur"))
 49    \\      ;;
 50    \\    *)
 51    \\      ;;
 52    \\  esac
 53    \\}
 54    \\
 55    \\complete -o bashdefault -o default -F _zmx_completions zmx
 56;
 57
 58const zsh_completions =
 59    \\_zmx() {
 60    \\  local context state state_descr line
 61    \\  typeset -A opt_args
 62    \\
 63    \\  _arguments -C \
 64    \\    '1: :->commands' \
 65    \\    '2: :->args' \
 66    \\    '*: :->trailing' \
 67    \\    && return 0
 68    \\
 69    \\  case $state in
 70    \\    commands)
 71    \\      local -a commands
 72    \\      commands=(
 73    \\        'attach:Attach to session, creating if needed'
 74    \\        'run:Send command without attaching'
 75    \\        'send:Send raw input to session PTY'
 76    \\        'detach:Detach all clients from current session'
 77    \\        'list:List active sessions'
 78    \\        'completions:Shell completion scripts'
 79    \\        'kill:Kill a session'
 80    \\        'history:Output session scrollback'
 81    \\        'version:Show version'
 82    \\        'help:Show help message'
 83    \\      )
 84    \\      _describe 'command' commands
 85    \\      ;;
 86    \\    args)
 87    \\      case $words[2] in
 88    \\        attach|a|kill|k|run|r|send|s|history|hi)
 89    \\          _zmx_sessions
 90    \\          ;;
 91    \\        completions|c)
 92    \\          _values 'shell' 'bash' 'zsh' 'fish'
 93    \\          ;;
 94    \\        list|l)
 95    \\          _values 'options' '--short'
 96    \\          ;;
 97    \\      esac
 98    \\      ;;
 99    \\    trailing)
100    \\      # Additional args for commands like 'attach' or 'run'
101    \\      ;;
102    \\  esac
103    \\}
104    \\
105    \\_zmx_sessions() {
106    \\  local -a sessions
107    \\
108    \\  local local_sessions=$(zmx list --short 2>/dev/null)
109    \\  if [[ -n "$local_sessions" ]]; then
110    \\    sessions+=(${(f)local_sessions})
111    \\  fi
112    \\
113    \\  _describe 'local session' sessions
114    \\}
115    \\
116    \\compdef _zmx zmx
117;
118
119const fish_completions =
120    \\complete -c zmx -f
121    \\
122    \\# zmx flags
123    \\complete -c zmx -x -n '__fish_is_nth_token 1' -s v -l version -d 'Show version'
124    \\complete -c zmx -x -n '__fish_is_nth_token 1' -s h -d 'Show help message'
125    \\
126    \\# zmx subcommands
127    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'a attach' -d 'Attach to session, creating if needed'
128    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'r run' -d 'Send command without attaching'
129    \\complete -c zmx -n "__fish_is_nth_token 1" -a 's send' -d 'Send raw input to session PTY'
130    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'wr write' -d 'Write stdin to file_path through the session'
131    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'd detach' -d 'Detach all clients (ctrl+\ for current client)'
132    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'l list' -d 'List active sessions'
133    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'k kill' -d 'Kill session and all attached clients'
134    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'hi history' -d 'Output session scrollback'
135    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'w wait' -d 'Wait for session tasks to complete'
136    \\complete -c zmx -n "__fish_is_nth_token 1" -a 't tail' -d 'Follow session output'
137    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'c completions' -d 'Shell completions (bash, zsh, fish)'
138    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'v version' -d 'Show version'
139    \\complete -c zmx -n "__fish_is_nth_token 1" -a 'h help' -d 'Show help message'
140    \\
141    \\# Complete session names and shells
142    \\complete -c zmx -n "__fish_is_nth_token 2; and __fish_seen_subcommand_from a attach r run s send wr write hi history" -a '(zmx list --short 2>/dev/null)' -d 'Session name'
143    \\complete -c zmx -n "not __fish_is_nth_token 1; and __fish_seen_subcommand_from k kill w wait t tail" -a '(zmx list --short 2>/dev/null)' -d 'Session name'
144    \\
145    \\complete -c zmx -n "__fish_is_nth_token 2; and __fish_seen_subcommand_from c completions" -a 'bash zsh fish' -d Shell
146    \\
147    \\# Subcommand flags
148    \\complete -c zmx -n "__fish_seen_subcommand_from r run" -s d -d 'Detach from the calling terminal; use `wait` to track its status'
149    \\complete -c zmx -n "__fish_seen_subcommand_from r run" -l fish -d 'Required when the session runs fish shell'
150    \\complete -c zmx -n "__fish_seen_subcommand_from l list" -l short -d 'Short output'
151    \\complete -c zmx -n "__fish_seen_subcommand_from k kill" -l force -d 'Force kill'
152    \\complete -c zmx -n "__fish_seen_subcommand_from hi history" -l vt -d 'History format for escape sequences'
153    \\complete -c zmx -n "__fish_seen_subcommand_from hi history" -l html -d 'History format for escape sequences'
154;