Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (

func newKeysCmd() *cobra.Command {
var (
filter string
asJSON bool
filter string
section string
asJSON bool
)

keysCmd := &cobra.Command{
Use: "keys",
Short: "Print keybindings",
Long: "Print the built-in keybinding reference without starting the TUI.",
RunE: func(cmd *cobra.Command, args []string) error {
sections := filterKeybindingSections(keybindings.Sections(), filter)
sections := filterKeybindingSectionsByID(keybindings.Sections(), section)
sections = filterKeybindingSections(sections, filter)
if asJSON {
enc := json.NewEncoder(cmd.OutOrStdout())
enc.SetIndent("", " ")
Expand All @@ -32,10 +34,24 @@ func newKeysCmd() *cobra.Command {
}

keysCmd.Flags().StringVar(&filter, "filter", "", "Only show sections, keys, or actions matching this text")
keysCmd.Flags().StringVar(&section, "section", "", "Only show the section with this id")
keysCmd.Flags().BoolVar(&asJSON, "json", false, "Print keybindings as JSON")
return keysCmd
}

func filterKeybindingSectionsByID(sections []keybindings.Section, section string) []keybindings.Section {
query := strings.TrimSpace(strings.ToLower(section))
if query == "" {
return sections
}
for _, item := range sections {
if strings.ToLower(item.ID) == query {
return []keybindings.Section{item}
}
}
return []keybindings.Section{}
}

func filterKeybindingSections(sections []keybindings.Section, filter string) []keybindings.Section {
query := strings.TrimSpace(strings.ToLower(filter))
if query == "" {
Expand Down
42 changes: 42 additions & 0 deletions cmd/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,48 @@ func TestKeysCommandFiltersBindings(t *testing.T) {
assert.NotContains(t, out.String(), "File Tree\n")
}

func TestKeysCommandFiltersBySection(t *testing.T) {
cmd := newKeysCmd()
cmd.SetArgs([]string{"--section", "filetree"})
var out bytes.Buffer
cmd.SetOut(&out)

err := cmd.Execute()

require.NoError(t, err)
assert.Contains(t, out.String(), "File Tree")
assert.NotContains(t, out.String(), "Global\n")
}

func TestKeysCommandFiltersBySectionJSON(t *testing.T) {
cmd := newKeysCmd()
cmd.SetArgs([]string{"--section", "global", "--json"})
var out bytes.Buffer
cmd.SetOut(&out)

err := cmd.Execute()

require.NoError(t, err)
var sections []keybindings.Section
require.NoError(t, json.Unmarshal(out.Bytes(), &sections))
require.Len(t, sections, 1)
assert.Equal(t, "global", sections[0].ID)
}

func TestKeysCommandUnknownSectionReturnsEmptyJSON(t *testing.T) {
cmd := newKeysCmd()
cmd.SetArgs([]string{"--section", "missing", "--json"})
var out bytes.Buffer
cmd.SetOut(&out)

err := cmd.Execute()

require.NoError(t, err)
var sections []keybindings.Section
require.NoError(t, json.Unmarshal(out.Bytes(), &sections))
assert.Empty(t, sections)
}

func TestKeysCommandPrintsJSON(t *testing.T) {
cmd := newKeysCmd()
cmd.SetArgs([]string{"--filter", "global", "--json"})
Expand Down
Loading