0

Here is my script:

#!/usr/bin/env bash

kill-all-sessions() {
    tmux kill-server
}

kill-other-sessions() {
    tmux kill-session -a
}

kill-unattached-sessions() {
    tmux list-sessions -F '#{session_name} #{session_attached}' | awk '$2 == 0 { print $1 }' | xargs -n1 tmux kill-session -t
}

list-attached-sessions() {
    tmux list-sessions -F "#{session_attached} #{session_name} #{session_path}" | awk '$1 == 1 { print $2, $3 }'
}

# Main logic: check the argument and call the corresponding function
case "$1" in
    kill-all)
        kill-all-sessions
        ;;
    kill-other)
        kill-other-sessions
        ;;
    kill-unattached)
        kill-unattached-sessions
        ;;
    list-attached)
        list-attached-sessions
        ;;
    *)
        echo "Usage: $0 {kill-all|kill-other|kill-unattached|list-attached}"
        exit 1
        ;;
esac

I need a zsh completion function for this script.

The code I came up with is:

#compdef tmux-manager

_tmux-manager() {
  local -a commands
  commands=(
    'kill-all:Kill all tmux sessions'
    'kill-other:Kill all sessions except the current one'
    'kill-unattached:Kill only unattached sessions'
    'list-attached:List only attached sessions'
  )

  _describe 'command' commands
}

_tmux-manager "$@"

The code is not working. What might be the solution.

1 Answer 1

1

The first answer is:

#compdef tmux-manager

compadd kill-all kill-other kill-unattached list-attached

If you want description using compadd then use this solution:

#compdef tmux-manager

function _tmux-manager {
  local -a _descriptions _values

  _descriptions=(
    'kill-all        - Kill all tmux sessions'
    'kill-other      - Kill all sessions except the current one'
    'kill-unattached - Kill only unattached sessions'
    'list-attached   - List only attached sessions'
  )

  _values=(
    kill-all
    kill-other
    kill-unattached
    list-attached
  )

  compadd -d _descriptions -a _values
}

Third solution:

#compdef tmux-manager
_arguments '1: :(kill-all kill-other kill-unattached list-attached)'

The fourth solution is:

#compdef tmux-manager

function _tmux-manager {
    _arguments -C \
    "1:commands:(kill-all kill-other kill-unattached list-attached)" \
    "*::arg:->args"
}

Fifth Solution (If you want description using _arguments):

#compdef tmux-manager

_arguments \
  '1: :((kill-all\:"Kill all tmux sessions" \
         kill-other\:"Kill all sessions except the current one" \
         kill-unattached\:"Kill only unattached sessions" \
         list-attached\:"List only attached sessions"))'

Or you can use the following code:

#compdef tmux-manager

local -a descriptions=(
  'kill-all:Kill all tmux sessions'
  'kill-other:Kill all except current'
  'kill-unattached:Kill only unattached'
  'list-attached:List attached sessions'
)

_arguments '1: :->commands'

case "$state" in
  commands)
    _describe 'command' descriptions
    ;;
esac

Sixth Solution:

#compdef tmux-manager

local -a commands=(
  'kill-all:Kill all tmux sessions'
  'kill-other:Kill all sessions except the current one'
  'kill-unattached:Kill only unattached sessions'
  'list-attached:List only attached sessions'
)

_describe 'command' commands

Seventh solution:

#compdef tmux-manager

_values 'tmux-manager commands' \
  'kill-all[Kill all tmux sessions]' \
  'kill-other[Kill all sessions except the current one]' \
  'kill-unattached[Kill only unattached sessions]' \
  'list-attached[List only attached sessions]'

Eighth Solution:

#compdef tmux-manager

_alternative \
  'commands:command:((
    kill-all\:"Kill all tmux sessions"
    kill-other\:"Kill all except current"
    kill-unattached\:"Kill only unattached"
    list-attached\:"List attached sessions"
  ))' \
  'files:filename:_files'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.