Skip to content

Commit

Permalink
feat: add ability to customize keyb hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx committed Jul 19, 2022
1 parent 4b5006e commit 43ae9b8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 65 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,23 @@ Both ANSI and hex color codes are supported.
| `border_color` | - | Border color |

#### Hotkeys

| Hotkey | Description |
|--------------------------- | ---------------- |
| <kbd>j, k / Up, Down</kbd> | Move cursor |
| <kbd>Ctrl + u, d</kbd> | Move half window |
| <kbd>Ctrl + b, f</kbd> | Move full window |
| <kbd>H, M, L</kbd> | Go to top, middle, bottom of screen |
| <kbd>g, G</kbd> | Go to first, last line |
| <kbd>/</kbd> | Enter search mode|
| <kbd>Alt + d</kbd> | Clear current search |
| <kbd>Esc</kbd> | Exit search mode |
| <kbd>Ctrl + c, q</kbd> | Quit |
Multiple keys may be set for a single binding, separated by commas.

| Hotkey | Default | Description |
| ----------------------- | -------------------------- | ---------------- |
| `up`, `down` | <kbd>j, k / Up, Down</kbd> | Move cursor |
| `half_up, half_down` | <kbd>Ctrl + u, d</kbd> | Move half window |
| `full_up, full_down` | <kbd>Ctrl + b, f</kbd> | Move full window |
| `top, middle, bottom` | <kbd>H, M, L</kbd> | Go to top, middle, bottom of screen |
| `first_line, last_line` | <kbd>g, G</kbd> | Go to first, last line |
| `search` | <kbd>/</kbd> | Enter search mode |
| `clear_search` | <kbd>Alt + d</kbd> | Clear current search (remains in search mode) |
| `normal` | <kbd>Esc</kbd> | Exit search mode |
| `quit` | <kbd>Ctrl + c, q</kbd> | Quit |

## Roadmap

- [ ] Ability to customize keyb hotkeys
- [x] Ability to customize keyb hotkeys
- [ ] Export to additional file formats (`json, toml, conf/ini` etc.)
- [ ] `-a, --add` flag to quickly add a single hotkey entry from the CLI
- [ ] Automatic parsing from online cheatsheet repos (eg. `cheat/cheatsheets`)
Expand Down
36 changes: 36 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
type Config struct {
Settings `yaml:"settings"`
Color `yaml:"color"`
Keys `yaml:"keys"`
}

type Settings struct {
Expand Down Expand Up @@ -48,6 +49,24 @@ type Color struct {
BorderColor string `yaml:"border_color"`
}

type Keys struct {
Quit string
Up string
Down string
HalfUp string `yaml:"half_up"`
HalfDown string `yaml:"half_down"`
FullUp string `yaml:"full_up"`
FullDown string `yaml:"full_bottom"`
GoToFirstLine string `yaml:"first_line"`
GoToLastLine string `yaml:"last_line"`
GoToTop string `yaml:"top"`
GoToMiddle string `yaml:"middle"`
GoToBottom string `yaml:"bottom"`
Search string
ClearSearch string `yaml:"clear_search"`
Normal string
}

func Parse(flagKPath, cfgPath string) (Apps, *Config, error) {
if err := CreateDefaultConfigFile(); err != nil {
return nil, nil, fmt.Errorf("no config file found: %w", err)
Expand Down Expand Up @@ -159,6 +178,23 @@ func generateDefaultConfig() (*Config, error) {
Color: Color{
FilterFg: "#FFA066",
},
Keys: Keys{
Quit: "q, ctrl+c",
Up: "k, up",
Down: "j, down",
HalfUp: "ctrl+u",
HalfDown: "ctrl+d",
FullUp: "ctrl+b",
FullDown: "ctrl+f",
GoToFirstLine: "g",
GoToLastLine: "G",
GoToTop: "H",
GoToMiddle: "M",
GoToBottom: "L",
Search: "/",
ClearSearch: "alt+d",
Normal: "esc",
},
}, nil
}

Expand Down
17 changes: 17 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ func TestParse(t *testing.T) {
Color: Color{
FilterFg: "#FFA066",
},
Keys: Keys{
Quit: "q, ctrl+c",
Up: "k, up",
Down: "j, down",
HalfUp: "ctrl+u",
HalfDown: "ctrl+d",
FullUp: "ctrl+b",
FullDown: "ctrl+f",
GoToFirstLine: "g",
GoToLastLine: "G",
GoToTop: "H",
GoToMiddle: "M",
GoToBottom: "L",
Search: "/",
ClearSearch: "alt+d",
Normal: "esc",
},
}
got, err := ParseConfig(path.Join(parentDir, "testconfig.yml"))
if err != nil {
Expand Down
85 changes: 35 additions & 50 deletions ui/list/keymap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package list

import "github.com/charmbracelet/bubbles/key"
import (
"strings"

"github.com/charmbracelet/bubbles/key"
"github.com/kencx/keyb/config"
)

type KeyMap struct {
Quit key.Binding
Expand All @@ -24,57 +29,37 @@ type KeyMap struct {
Normal key.Binding
}

func DefaultKeyMap() KeyMap {
func CreateKeyMap(keys config.Keys) KeyMap {
return KeyMap{
Quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
),
Up: key.NewBinding(
key.WithKeys("up", "k"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
),
HalfUp: key.NewBinding(
key.WithKeys("ctrl+u"),
),
HalfDown: key.NewBinding(
key.WithKeys("ctrl+d"),
),
FullUp: key.NewBinding(
key.WithKeys("ctrl+b"),
),
FullDown: key.NewBinding(
key.WithKeys("ctrl+f"),
),
GoToFirstLine: key.NewBinding(
key.WithKeys("g"),
),
GoToLastLine: key.NewBinding(
key.WithKeys("G"),
),
GoToTop: key.NewBinding(
key.WithKeys("H"),
),
GoToMiddle: key.NewBinding(
key.WithKeys("M"),
),
GoToBottom: key.NewBinding(
key.WithKeys("L"),
),
Quit: SetKey(keys.Quit),
Up: SetKey(keys.Up),
Down: SetKey(keys.Down),
HalfUp: SetKey(keys.HalfUp),
HalfDown: SetKey(keys.HalfDown),
FullUp: SetKey(keys.FullUp),
FullDown: SetKey(keys.FullDown),
GoToFirstLine: SetKey(keys.GoToFirstLine),
GoToLastLine: SetKey(keys.GoToLastLine),
GoToTop: SetKey(keys.GoToTop),
GoToMiddle: SetKey(keys.GoToMiddle),
GoToBottom: SetKey(keys.GoToBottom),

CenterCursor: key.NewBinding(
key.WithKeys("Z"),
),
Search: SetKey(keys.Search),
ClearSearch: SetKey(keys.ClearSearch),
Normal: SetKey(keys.Normal),
}
}

func SetKey(s string) key.Binding {
return key.NewBinding(
key.WithKeys(splitAndTrim(s, ",")...),
)
}

Search: key.NewBinding(
key.WithKeys("/"),
),
ClearSearch: key.NewBinding(
key.WithKeys("alt+d"),
),
Normal: key.NewBinding(
key.WithKeys("esc"),
),
func splitAndTrim(s, sep string) []string {
sl := strings.Split(s, sep)
for i := range sl {
sl[i] = strings.TrimSpace(sl[i])
}
return sl
}
3 changes: 1 addition & 2 deletions ui/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func (m *Model) configure(c *config.Config) {

m.title = c.Title
m.debug = c.Debug
// TODO customize keymap
m.keys = DefaultKeyMap()
m.keys = CreateKeyMap(c.Keys)

m.margin = c.Margin
m.padding = c.Padding
Expand Down

0 comments on commit 43ae9b8

Please sign in to comment.