shell-pool / shpool

Think tmux, then aim... lower
Apache License 2.0
1.03k stars 13 forks source link

Sharing my fzf session selector #49

Closed thiswillbeyourgithub closed 1 week ago

thiswillbeyourgithub commented 1 week ago

Hi,

I just wanted to share my session selector made with fzf. I'll probably enhance it in the future but it's still a good starting point.

Note that I'm using zsh and my fzf version is 0.52.1 (it's very outdated on most distro!):

# shpool session selector
# keybindings:
#     k/x to kill
#     a/n/enter to attach
# Shortcut: ctrl+a then w
shpool_choose() {
    list=$(shpool list | tail -n +2)
    cmd_output=$(
    shpool list | tail -n +2 | cut -f1 | fzf \
        --bind 'k:execute(shpool kill {})' \
        --bind 'x:execute(shpool kill {})' \
        --bind 'a:execute(shpool attach --force {})' \
        --bind 'n:execute(shpool attach --force {})' \
        --preview 'shpool list | tail -n +2 | sed -n "$(({n}+1))"p' \
        --bind "change:reload(shpool list | tail -n +2)" \
        --reverse \
        --height=~100% \
        --preview-window down:wrap \
        --header "Shpool sessions" \
        --no-select-1 \
        --no-exit-0
    )

    # notify-send "$cmd_output"
    [ -n "$cmd_output" ] && shpool attach --force $cmd_output
}
bindkey -s "^aw" 'shpool_choose\n'
ethanpailes commented 1 week ago

This is pretty cool! We should probably add stuff like this to a wiki so it is easy for people to find

ethanpailes commented 1 week ago

I created a wiki page for this and linked to it from the wiki homepage. I'll also update the README to link to the wiki homepage so people can find it.

thiswillbeyourgithub commented 1 week ago

Just a correction: i don't think of this as a way to launch shpool but rather as a way to easily attach to sessions

ethanpailes commented 1 week ago

Once https://github.com/shell-pool/shpool/pull/54 merges this will be fully incorporated in shpool's documentation.

thiswillbeyourgithub commented 1 week ago

Thanks!

Addendum: In effect, the way I launch shpool currently is directly in my .zshrc, only when not using kitty. This is usually when I ssh, usually via termux to my computer.

I think this can serve as inspiration to others too:

## directly checks if it should start a tmux session to avoir running the entire zshrc twice
if [[ ! "$TERM" == "xterm-kitty" ]]  # kitty does not seem to work well and it's mainly to be used when I ssh into here
then
    # create a new shpool session
    $(printenv | grep -q '^SHPOOL_SESSION_NAME=') || {  # if we're not already in a shpool session
        {
            sessions=$(shpool list | tail -n +2 | cut -f1)
            for letter in {a..z}  # sessions are named a through z to be easy to type on termux, as shpool has no autocomplete
            do
                $(echo "$sessions" | grep -q "$letter") || {
                    # notify-send "Creating shpool session $letter"
                    shpool attach "$letter" || notify-send "Error creating shpool session"
                    break  # we could logout here too
                }
            done
        } || { notify-send -u critical "Error creating shpool session" ; echo "Error creating shpool session" }
    }
    $(printenv | grep -q '^SHPOOL_SESSION_NAME=') && {
        echo "Using shpool session $SHPOOL_SESSION_NAME"
    }
fi
ethanpailes commented 1 week ago

@thiswillbeyourgithub are you able to directly contribute this to the wiki? (If not let me know and I can try to dig up the right settings).

thiswillbeyourgithub commented 1 week ago

I don't think that I can. I can only clone the wiki, and probably make a PR

ethanpailes commented 1 week ago

Try now

thiswillbeyourgithub commented 1 week ago

Like a charm. Thanks!

thiswillbeyourgithub commented 1 week ago

I added my zsh start script btw.