Skip to content

Commit

Permalink
Add option to show current key table in status bar
Browse files Browse the repository at this point in the history
Check `client_key_table` against the key tables and use corresponding
"icon" to show the command sequence in status bar.

This implements the idea in GitHub issue #2.
  • Loading branch information
whame committed Nov 27, 2022
1 parent bf687ce commit bc5720a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,17 @@ sticky window command mode will be entered directly. That is, after hitting `w`
once, you can now directly use `h`, `j`, `k` and `l` to select the window panes
(or any other window commands). Don't forget to exit the sticky command with
`q`.

### Show command keys in status bar

The option `@modal-show-cmd-keys` can be set in `.tmux.conf` to give immediate
feedback in the status bar during tmux-modal command sequences:

```
set -g @modal-show-cmd-keys on
```

The left status bar will now update to match the tmux-modal command currently in
use. For example, if you press `w` the status bar will change from the modal
command icon `[=]` to `[w]` (the window command). If you now further press `s`,
it will update to `[ws]` to signify the split window command sequence and so on.
98 changes: 92 additions & 6 deletions tmux-modal.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ elif ! parse_yn_opt "$ALWAYS_STICKY_VAL" $ALWAYS_STICKY_OPT; then
exit 22
fi

# Show command keys (key tables) in status bar.
SHOW_CMD_KEYS_OPT=@modal-show-cmd-keys
SHOW_CMD_KEYS_VAL=$(tmux show-options -g -v -q $SHOW_CMD_KEYS_OPT)
if [ -z "$SHOW_CMD_KEYS_VAL" ]; then
SHOW_CMD_KEYS_VAL=off
elif ! parse_yn_opt "$SHOW_CMD_KEYS_VAL" $SHOW_CMD_KEYS_OPT; then
exit 22
fi

# Create keybinding file to be sourced by tmux.

KBD_FILE="$CURRENT_DIR/.kbd.conf"
Expand Down Expand Up @@ -631,16 +640,93 @@ if [ "$START_OPT_VAL" == on ]; then
tmux set-option -g key-table $KT_CMD
fi

# Prepend left status bar with MODAL_ICON if our key tables are in use.
# Prepend left status bar with KT_CMD_ICON if our key tables are in use.
# Determine this by checking if current key table starts with our prefix.
MODAL_ICON="[=]"
KT_CMD_ICON="[=]"
STATUS_LEFT=`
`'#{'`
`'?#{==:'$KT_PREFIX'-,'`
`'#{='$((${#KT_PREFIX} + 1))':client_key_table}'`
`'},'`
`$KT_CMD_ICON' ,'`
`'}'

if [ "$SHOW_CMD_KEYS_VAL" == on ]; then
# Check which key table is in use and use corresponding "icon" in the left
# status bar. The icons are derived from the keybindings.
KT_WIN_ICON="[$KBD_WIN]"
KT_WIN_PANE_ICON="[$KBD_WIN$KBD_WIN_PANE]"
KT_WIN_SPLIT_ICON="[$KBD_WIN$KBD_WIN_SPLIT]"
KT_WIN_MOVE_ICON="[$KBD_WIN$KBD_WIN_MOVE]"
KT_WIN_ARRANGE_ICON="[$KBD_WIN$KBD_WIN_ARRANGE]"
KT_WIN_RESIZE_ICON="[$KBD_WIN$KBD_WIN_RESIZE]"

KT_SESS_ICON="[$KBD_SESS]"

KT_GOTO_ICON="[$KBD_GOTO]"
KT_GOTO_WIN_ICON="[$KBD_GOTO$KBD_GOTO_WIN]"
KT_GOTO_SESS_ICON="[$KBD_GOTO$KBD_GOTO_SESS]"

# Seems to be the only way to to do if-elseif-...-else in tmux format
# syntax...
STATUS_LEFT=`
`'#{'`
`'?#{==:'$KT_CMD','`
`'#{client_key_table}'`
`'},'`
`$KT_CMD_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN_PANE','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_PANE_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN_SPLIT','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_SPLIT_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN_MOVE','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_MOVE_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN_ARRANGE','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_ARRANGE_ICON' ,'`
`'#{'`
`'?#{==:'$KT_WIN_RESIZE','`
`'#{client_key_table}'`
`'},'`
`$KT_WIN_RESIZE_ICON' ,'`
`'#{'`
`'?#{==:'$KT_SESS','`
`'#{client_key_table}'`
`'},'`
`$KT_SESS_ICON' ,'`
`'#{'`
`'?#{==:'$KT_GOTO','`
`'#{client_key_table}'`
`'},'`
`$KT_GOTO_ICON' ,'`
`'#{'`
`'?#{==:'$KT_PREFIX'-,'`
`'#{='$((${#KT_PREFIX} + 1))':client_key_table}'`
`'?#{==:'$KT_GOTO_WIN','`
`'#{client_key_table}'`
`'},'`
`$MODAL_ICON' ,'`
`'}'
`$KT_GOTO_WIN_ICON' ,'`
`'#{'`
`'?#{==:'$KT_GOTO_SESS','`
`'#{client_key_table}'`
`'},'`
`$KT_GOTO_SESS_ICON' ,'`
`'}}}}}}}}}}}'
fi

# We want to set the left status bar once; do it only if we can't find our
# status string in current status (otherwise we would create several icons next
Expand Down

0 comments on commit bc5720a

Please sign in to comment.