feat: toggle keybind help at runtime (#833)

This commit is contained in:
ayn2op 2026-08-27 20:31:52 -04:00 • committed by GitHub
parent e53354a738
commit fb39147eae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 64 additions and 14 deletions

6
go.mod
View file

@ -12,10 +12,10 @@ go 1.27.0
require (
github.com/BurntSushi/toml v1.6.0
github.com/alecthomas/chroma/v2 v2.27.0
github.com/andybalholm/brotli v1.2.2
github.com/andybalholm/brotli v1.2.3
github.com/ayn2op/arikawa/v3 v3.0.0-20260827004545-b90631eb8a21
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827004927-1b60a8f71a95
github.com/ayn2op/tview v0.0.0-20260821041152-0e033858692a
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4
github.com/ayn2op/tview v0.0.0-20260827231920-c0e5ff669a2c
github.com/bogdanfinn/fhttp v0.6.8
github.com/bogdanfinn/tls-client v1.15.1
github.com/gdamore/tcell/v3 v3.4.2

12
go.sum
View file

@ -8,14 +8,14 @@ github.com/alecthomas/chroma/v2 v2.27.0 h1:FodwmyOBgJULFYmDqibcp9pvfDLWdtPRh9v/r
github.com/alecthomas/chroma/v2 v2.27.0/go.mod h1:NjJ3ciIgrqBNeIkWZ4e46nseoLDslxU1LmfCoL+wcY8=
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM=
github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/andybalholm/brotli v1.2.3 h1:8H1qwOkl2LPfjf3YezB90JnCliZb6SInJ/OJkEbA5NQ=
github.com/andybalholm/brotli v1.2.3/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/ayn2op/arikawa/v3 v3.0.0-20260827004545-b90631eb8a21 h1:+6W+QtSmIV8Dry4iRtlVNOaH2Z5yzKxvr3uy0ajVG3E=
github.com/ayn2op/arikawa/v3 v3.0.0-20260827004545-b90631eb8a21/go.mod h1:C7NGoHHt0MQH/plbA9ly4YUQwG0v+n0XdgXmdEPoRpU=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827004927-1b60a8f71a95 h1:z1k/o65WutgG/8FCoD6mZnNFrvdBAK1MTqnkwfz5wjw=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827004927-1b60a8f71a95/go.mod h1:VyJr7bsyJmVAa0AUCSRaA5qBh6h6OER+5Yxhl0K5xs4=
github.com/ayn2op/tview v0.0.0-20260821041152-0e033858692a h1:325ZB3uIaH7DjxeGwpSVQBcDo7DgrbHow7NGvj6DNXc=
github.com/ayn2op/tview v0.0.0-20260821041152-0e033858692a/go.mod h1:iFzkW7DBltCaeLMR9SvXVUAw/zgKIbgE8JaUU8W8crk=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4 h1:Jl3hKkjANCCGPHTyrxLqy+FVdvdftGY41E5f4/1YrRA=
github.com/ayn2op/ningen/v3 v3.0.1-0.20260827010304-ee3a9ce6c2e4/go.mod h1:p+rsTJpVomHc8gyyLY0/Wwzj9qsPKoXgwCg2yIlG04A=
github.com/ayn2op/tview v0.0.0-20260827231920-c0e5ff669a2c h1:zh00g4QZXpXQCU/GTE+V47P1uwJOYU+CbH9nguFznJM=
github.com/ayn2op/tview v0.0.0-20260827231920-c0e5ff669a2c/go.mod h1:iFzkW7DBltCaeLMR9SvXVUAw/zgKIbgE8JaUU8W8crk=
github.com/bdandy/go-errors v1.2.2 h1:WdFv/oukjTJCLa79UfkGmwX7ZxONAihKu4V0mLIs11Q=
github.com/bdandy/go-errors v1.2.2/go.mod h1:NkYHl4Fey9oRRdbB1CoC6e84tuqQHiqrOcZpqFEkBxM=
github.com/bdandy/go-socks4 v1.2.3 h1:Q6Y2heY1GRjCtHbmlKfnwrKVU/k81LS8mRGLRlmDlic=

View file

@ -35,7 +35,7 @@ theme = "monokai"
[help]
# Whether to show keybind help.
enabled = false
enabled = true
# Show compact key modifiers in help, e.g. "^x" instead of "ctrl+x".
compact_modifiers = true
# [left, right]
@ -104,7 +104,10 @@ guild_store = "s-"
# Hide/show the guilds tree.
toggle_guilds_tree = "ctrl+b"
toggle_channels_picker = "ctrl+k"
toggle_help = "ctrl+."
# Hide/show keybind help.
toggle_help = "alt+."
# Switch between compact and full keybind help.
toggle_full_help = "ctrl+."
focus_guilds_tree = "ctrl+g"
focus_messages_list = "ctrl+t"
focus_composer = "ctrl+i"

View file

@ -107,6 +107,7 @@ type Keybinds struct {
ToggleGuildsTree Keybind `toml:"toggle_guilds_tree"`
ToggleChannelsPicker Keybind `toml:"toggle_channels_picker"`
ToggleHelp Keybind `toml:"toggle_help"`
ToggleFullHelp Keybind `toml:"toggle_full_help"`
Suspend Keybind `toml:"suspend"`
FocusGuildsTree Keybind `toml:"focus_guilds_tree"`
@ -199,7 +200,8 @@ func defaultKeybinds() Keybinds {
return Keybinds{
ToggleGuildsTree: desc("toggle guilds"),
ToggleChannelsPicker: desc("channels picker"),
ToggleHelp: desc("help"),
ToggleHelp: desc("toggle help"),
ToggleFullHelp: desc("full help"),
Suspend: desc("suspend"),
FocusGuildsTree: desc("guilds"),

View file

@ -10,6 +10,7 @@ var _ help.KeyMap = (*Model)(nil)
func (m *Model) ShortHelp() []keybind.Keybind {
global := []keybind.Keybind{
m.cfg.Keybinds.ToggleHelp.Keybind,
m.cfg.Keybinds.ToggleFullHelp.Keybind,
m.cfg.Keybinds.Suspend.Keybind,
m.cfg.Keybinds.Quit.Keybind,
}
@ -23,6 +24,7 @@ func (m *Model) ShortHelp() []keybind.Keybind {
func (m *Model) FullHelp() [][]keybind.Keybind {
global := []keybind.Keybind{
m.cfg.Keybinds.ToggleHelp.Keybind,
m.cfg.Keybinds.ToggleFullHelp.Keybind,
m.cfg.Keybinds.Suspend.Keybind,
m.cfg.Keybinds.Quit.Keybind,
}

View file

@ -149,6 +149,9 @@ func (m *Model) Update(msg tview.Msg) tview.Cmd {
}
switch {
case keybind.Matches(msg, m.cfg.Keybinds.ToggleHelp.Keybind):
m.toggleHelp()
return nil
case keybind.Matches(msg, m.cfg.Keybinds.ToggleFullHelp.Keybind):
if m.help == nil {
return nil
}
@ -176,6 +179,19 @@ func (m *Model) Update(msg tview.Msg) tview.Cmd {
return nil
}
func (m *Model) toggleHelp() {
if m.help == nil {
return
}
count := m.rootFlex.GetItemCount()
if count > 0 && m.rootFlex.GetItem(count-1) == m.help {
m.rootFlex.RemoveItem(m.help)
} else {
m.rootFlex.AddItem(m.help, m.helpHeight(), 0, false)
}
}
func (m *Model) showModal(request ui.ModalMsg) tview.Cmd {
if m.modalRequest != nil {
return nil

View file

@ -30,12 +30,31 @@ func TestHelpEnabled(t *testing.T) {
m.Update(tcell.NewEventKey(tcell.KeyRune, ".", tcell.ModCtrl))
if !m.help.ShowAll() {
t.Fatal("toggle_help did not enable full help")
t.Fatal("toggle_full_help did not enable full help")
}
help := m.help
m.Update(tcell.NewEventKey(tcell.KeyRune, ".", tcell.ModAlt))
if m.help != help {
t.Fatal("toggle_help replaced help model")
}
if count := m.rootFlex.GetItemCount(); count != 0 {
t.Fatalf("layout item count = %d, want 0", count)
}
m.Update(tcell.NewEventKey(tcell.KeyRune, ".", tcell.ModAlt))
if m.help != help {
t.Fatal("toggle_help replaced help model")
}
if count := m.rootFlex.GetItemCount(); count != 1 {
t.Fatalf("layout item count = %d, want 1", count)
}
})
t.Run("disabled", func(t *testing.T) {
m := NewModel(cfg)
cfg := *cfg
cfg.Help.Enabled = false
m := NewModel(&cfg)
if m.help != nil {
t.Fatal("help model was created")
@ -45,8 +64,16 @@ func TestHelpEnabled(t *testing.T) {
}
m.Update(tcell.NewEventKey(tcell.KeyRune, ".", tcell.ModCtrl))
if m.help != nil {
t.Fatal("toggle_full_help created help model")
}
m.Update(tcell.NewEventKey(tcell.KeyRune, ".", tcell.ModAlt))
if m.help != nil {
t.Fatal("toggle_help created help model")
}
if count := m.rootFlex.GetItemCount(); count != 0 {
t.Fatalf("layout item count = %d, want 0", count)
}
})
}