main zmx / src / completions.zig
Max Rydahl Andersen  ·  2026-06-29
  1const std = @import("std");
  2
  3pub const Shell = enum {
  4    bash,
  5    zsh,
  6    fish,
  7    nu,
  8
  9    pub fn fromString(s: []const u8) ?Shell {
 10        if (std.mem.eql(u8, s, "bash")) return .bash;
 11        if (std.mem.eql(u8, s, "zsh")) return .zsh;
 12        if (std.mem.eql(u8, s, "fish")) return .fish;
 13        if (std.mem.eql(u8, s, "nu")) return .nu;
 14
 15        return null;
 16    }
 17
 18    pub fn getCompletionScript(self: Shell) []const u8 {
 19        return switch (self) {
 20            .bash => bash_completions,
 21            .zsh => zsh_completions,
 22            .fish => fish_completions,
 23            .nu => nu_completions,
 24        };
 25    }
 26};
 27
 28const bash_completions =
 29    \\_zmx_completions() {
 30    \\  local cur prev words cword
 31    \\  COMPREPLY=()
 32    \\  cur="${COMP_WORDS[COMP_CWORD]}"
 33    \\  prev="${COMP_WORDS[COMP_CWORD-1]}"
 34    \\
 35    \\  local commands="attach run send print write detach list kill history get set clear wait tail completions version help"
 36    \\
 37    \\  if [[ $COMP_CWORD -eq 1 ]]; then
 38    \\    COMPREPLY=($(compgen -W "$commands" -- "$cur"))
 39    \\    return 0
 40    \\  fi
 41    \\
 42    \\  case "$prev" in
 43    \\    attach|run|send|print|write|kill|history|get|set|clear|wait|tail)
 44    \\      local sessions=$(zmx list --short 2>/dev/null | tr '\n' ' ')
 45    \\      COMPREPLY=($(compgen -W "$sessions" -- "$cur"))
 46    \\      ;;
 47    \\    completions)
 48    \\      COMPREPLY=($(compgen -W "bash zsh fish nu" -- "$cur"))
 49    \\      ;;
 50    \\    list)
 51    \\      COMPREPLY=($(compgen -W "--short" -- "$cur"))
 52    \\      ;;
 53    \\    *)
 54    \\      ;;
 55    \\  esac
 56    \\}
 57    \\
 58    \\complete -o bashdefault -o default -F _zmx_completions zmx
 59;
 60
 61const zsh_completions =
 62    \\#compdef zmx
 63    \\_zmx() {
 64    \\  local context state state_descr line
 65    \\  typeset -A opt_args
 66    \\
 67    \\  _arguments -C \
 68    \\    '1: :->commands' \
 69    \\    '2: :->args' \
 70    \\    '*: :->trailing' \
 71    \\    && return 0
 72    \\
 73    \\  case $state in
 74    \\    commands)
 75    \\      local -a commands
 76    \\      commands=(
 77    \\        'attach:Attach to session, creating if needed'
 78    \\        'run:Send command without attaching'
 79    \\        'send:Send raw input to session PTY'
 80    \\        'print:Inject text into session display'
 81    \\        'write:Write stdin to file_path through the session'
 82    \\        'detach:Detach all clients from current session'
 83    \\        'list:List active sessions'
 84    \\        'kill:Kill a session'
 85    \\        'history:Output session scrollback'
 86    \\        'wait:Wait for session tasks to complete'
 87    \\        'tail:Follow session output'
 88    \\        'completions:Shell completion scripts'
 89    \\        'get:Get session labels'
 90    \\        'set:Set session labels'
 91    \\        'clear:Clear all session labels'
 92    \\        'version:Show version'
 93    \\        'help:Show help message'
 94    \\      )
 95    \\      _describe 'command' commands
 96    \\      ;;
 97    \\    args)
 98    \\      case $words[2] in
 99    \\        attach|a|kill|k|run|r|send|s|print|p|write|wr|history|get|g|set|clear|hi|wait|w|tail|t)
100    \\          _zmx_sessions
101    \\          ;;
102    \\        completions|c)
103    \\          _values 'shell' 'bash' 'zsh' 'fish' 'nu'
104    \\          ;;
105    \\        list|l)
106    \\          _values 'options' '--short'
107    \\          ;;
108    \\      esac
109    \\      ;;
110    \\    trailing)
111    \\      # Additional args for commands like 'attach' or 'run'
112    \\      ;;
113    \\  esac
114    \\}
115    \\
116    \\_zmx_sessions() {
117    \\  local -a sessions
118    \\
119    \\  local local_sessions=$(zmx list --short 2>/dev/null)
120    \\  if [[ -n "$local_sessions" ]]; then
121    \\    sessions+=(${(f)local_sessions})
122    \\  fi
123    \\
124    \\  _describe 'local session' sessions
125    \\}
126    \\
127    \\compdef _zmx zmx
128;
129
130const fish_completions =
131    \\complete -c zmx -f
132    \\
133    \\# zmx flags
134    \\complete -c zmx -x -n '__fish_is_nth_token 1' -s v -l version -d 'Show version'
135    \\complete -c zmx -x -n '__fish_is_nth_token 1' -s h -d 'Show help message'
136    \\
137    \\# zmx subcommands
138    \\complete -c zmx -n "__fish_is_nth_token 1" -a attach -d 'Attach to session, creating if needed'
139    \\complete -c zmx -n "__fish_is_nth_token 1" -a run -d 'Send command without attaching'
140    \\complete -c zmx -n "__fish_is_nth_token 1" -a send -d 'Send raw input to session PTY'
141    \\complete -c zmx -n "__fish_is_nth_token 1" -a print -d 'Inject text into session display'
142    \\complete -c zmx -n "__fish_is_nth_token 1" -a write -d 'Write stdin to file_path through the session'
143    \\complete -c zmx -n "__fish_is_nth_token 1" -a detach -d 'Detach all clients (ctrl+\ for current client)'
144    \\complete -c zmx -n "__fish_is_nth_token 1" -a list -d 'List active sessions'
145    \\complete -c zmx -n "__fish_is_nth_token 1" -a kill -d 'Kill session and all attached clients'
146    \\complete -c zmx -n "__fish_is_nth_token 1" -a history -d 'Output session scrollback'
147    \\complete -c zmx -n "__fish_is_nth_token 1" -a wait -d 'Wait for session tasks to complete'
148    \\complete -c zmx -n "__fish_is_nth_token 1" -a tail -d 'Follow session output'
149    \\complete -c zmx -n "__fish_is_nth_token 1" -a completions -d 'Shell completions (bash, zsh, fish, nu)'
150    \\complete -c zmx -n "__fish_is_nth_token 1" -a version -d 'Show version'
151    \\complete -c zmx -n "__fish_is_nth_token 1" -a get -d 'Get session labels'
152    \\complete -c zmx -n "__fish_is_nth_token 1" -a set -d 'Set session labels'
153    \\complete -c zmx -n "__fish_is_nth_token 1" -a clear -d 'Clear all session labels'
154    \\complete -c zmx -n "__fish_is_nth_token 1" -a help -d 'Show help message'
155    \\
156    \\# Complete session names and shells
157    \\complete -c zmx -n "__fish_is_nth_token 2; and __fish_seen_subcommand_from a attach r run s send p print wr write hi history g get se set cl clear" -a '(zmx list --short 2>/dev/null)' -d 'Session name'
158    \\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'
159    \\
160    \\complete -c zmx -n "__fish_is_nth_token 2; and __fish_seen_subcommand_from c completions" -a 'bash zsh fish nu' -d Shell
161    \\
162    \\# Subcommand flags
163    \\complete -c zmx -n "__fish_seen_subcommand_from r run" -s d -d 'Detach from the calling terminal; use `wait` to track its status'
164    \\complete -c zmx -n "__fish_seen_subcommand_from r run" -l fish -d 'Required when the session runs fish shell'
165    \\complete -c zmx -n "__fish_seen_subcommand_from l list" -l short -d 'Short output'
166    \\complete -c zmx -n "__fish_seen_subcommand_from l list" -l where -d 'Filter by label (key=value)' -r
167    \\complete -c zmx -n "__fish_seen_subcommand_from k kill" -l force -d 'Force kill'
168    \\complete -c zmx -n "__fish_seen_subcommand_from hi history" -l vt -d 'History format for escape sequences'
169    \\complete -c zmx -n "__fish_seen_subcommand_from hi history" -l html -d 'History format for escape sequences'
170;
171
172const nu_completions =
173    \\def "nu-complete zmx sessions" [] {
174    \\    zmx list --short | lines
175    \\}
176    \\
177    \\def "nu-complete zmx complete" [] {
178    \\    [bash fish nu zsh]
179    \\}
180    \\
181    \\export extern "zmx attach" [
182    \\    name: string@"nu-complete zmx sessions"
183    \\    ...rest: string
184    \\]
185    \\
186    \\export extern "zmx run" [
187    \\    name: string@"nu-complete zmx sessions"
188    \\    -d
189    \\    --fish
190    \\    ...rest: string
191    \\]
192    \\
193    \\export extern "zmx send" [
194    \\    name: string@"nu-complete zmx sessions"
195    \\    text: string
196    \\]
197    \\
198    \\export extern "zmx print" [
199    \\    name: string@"nu-complete zmx sessions"
200    \\    text: string
201    \\]
202    \\
203    \\export extern "zmx write" [
204    \\    name: string@"nu-complete zmx sessions"
205    \\    path: path
206    \\]
207    \\
208    \\export extern "zmx kill" [
209    \\    --force
210    \\    name: string@"nu-complete zmx sessions"
211    \\]
212    \\
213    \\export extern "zmx detach" []
214    \\export extern "zmx list" [--short]
215    \\export extern "zmx history" [name: string@"nu-complete zmx sessions", --vt, --html]
216    \\export extern "zmx wait" [...sessions: string@"nu-complete zmx sessions"]
217    \\export extern "zmx tail" [...sessions: string@"nu-complete zmx sessions"]
218    \\export extern "zmx version" []
219    \\export extern "completions" [shell: string@"nu-complete zmx complete"]
220    \\export extern "zmx get" [
221    \\    name?: string@"nu-complete zmx sessions"
222    \\]
223    \\
224    \\export extern "zmx set" [
225    \\    name?: string@"nu-complete zmx sessions"
226    \\    ...pairs: string
227    \\]
228    \\
229    \\export extern "zmx clear" [
230    \\    name?: string@"nu-complete zmx sessions"
231    \\]
232    \\
233    \\export extern "zmx help" []
234;