__zmx_choose_session.fish

 1function __zmx_choose_session --description "Choose or create a zmx session name with fzf"
 2    argparse 'p/preview=' -- $argv
 3    or return 1
 4
 5    if not type -q fzf
 6        echo "__zmx_choose_session: fzf is not installed or not on PATH" >&2
 7        return 127
 8    end
 9
10    set -l preview_command 'zmx history {1}'
11    if set -q _flag_preview
12        set preview_command $_flag_preview
13    end
14
15    set -l display
16    read -lz display
17
18    set -l output_file (mktemp)
19    begin
20        if test -n "$display"
21            printf "%s" $display
22        end
23    end | fzf \
24        --print-query \
25        --expect=ctrl-n \
26        --height=80% \
27        --reverse \
28        --prompt="zmx> " \
29        --header="Enter: select | Ctrl-N: create new" \
30        --preview=$preview_command \
31        --preview-window=right:60%:follow \
32        >$output_file
33
34    set -l fzf_status $status
35    set -l query (sed -n '1p' $output_file)
36    set -l key (sed -n '2p' $output_file)
37    set -l selected (sed -n '3p' $output_file)
38    rm -f $output_file
39
40    if test "$key" = ctrl-n; and test -n "$query"
41        echo $query
42    else if test $fzf_status -eq 0; and test -n "$selected"
43        string split -f1 ' ' -- $selected
44    else if test -n "$query"
45        echo $query
46    else
47        return 130
48    end
49end