Skip to content

Commit

Permalink
refactor: keymaps
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 5, 2024
1 parent a0af09e commit 12d2ce0
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 10 deletions.
5 changes: 3 additions & 2 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type model struct {
timedOut bool
showHelp bool
help help.Model
keymap keymap

// styles
cursorStyle lipgloss.Style
Expand Down Expand Up @@ -156,7 +157,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
case tea.KeyMsg:
start, end := m.paginator.GetSliceBounds(len(m.items))
km := defaultKeymap()
km := m.keymap
switch {
case key.Matches(msg, km.Down):
m.index++
Expand Down Expand Up @@ -298,7 +299,7 @@ func (m model) View() string {
}
parts = append(parts, s.String())
if m.showHelp {
parts = append(parts, m.help.View(defaultKeymap()))
parts = append(parts, m.help.View(m.keymap))
}

return lipgloss.JoinVertical(lipgloss.Left, parts...)
Expand Down
1 change: 1 addition & 0 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (o Options) Run() error {
timeout: o.Timeout,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
}, tea.WithOutput(os.Stderr)).Run()
if err != nil {
return fmt.Errorf("failed to start tea program: %w", err)
Expand Down
1 change: 1 addition & 0 deletions file/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (o Options) Run() error {
aborted: false,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
}

tm, err := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
Expand Down
3 changes: 2 additions & 1 deletion file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type model struct {
hasTimeout bool
showHelp bool
help help.Model
keymap keymap
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -110,6 +111,6 @@ func (m model) View() string {
return lipgloss.JoinVertical(
lipgloss.Top,
m.filepicker.View(),
m.help.View(defaultKeymap()),
m.help.View(m.keymap),
)
}
1 change: 1 addition & 0 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (o Options) Run() error {
sort: o.Sort && o.FuzzySort,
strict: o.Strict,
showHelp: o.ShowHelp,
keymap: defaultKeymap(),
help: help.New(),
}, options...)

Expand Down
7 changes: 4 additions & 3 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type model struct {
fuzzy bool
sort bool
showHelp bool
keymap keymap
help help.Model
timeout time.Duration
hasTimeout bool
Expand Down Expand Up @@ -204,7 +205,7 @@ func (m model) View() string {

help := ""
if m.showHelp {
help = m.help.View(defaultKeymap())
help = m.help.View(m.keymap)
}

// View the input and the filtered choices
Expand Down Expand Up @@ -253,14 +254,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.headerStyle.Render(m.header))
}
if m.showHelp {
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.help.View(defaultKeymap()))
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.help.View(m.keymap))
}
m.viewport.Width = msg.Width
if m.reverse {
m.viewport.YOffset = clamp(0, len(m.matches), len(m.matches)-m.viewport.Height)
}
case tea.KeyMsg:
km := defaultKeymap()
km := m.keymap
switch {
case key.Matches(msg, km.Abort):
m.aborted = true
Expand Down
1 change: 1 addition & 0 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (o Options) Run() error {
autoWidth: o.Width < 1,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
}, tea.WithOutput(os.Stderr))
tm, err := p.Run()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

type keymap textinput.KeyMap

func defaultKeymap() help.KeyMap {
func defaultKeymap() keymap {
k := textinput.DefaultKeyMap
return keymap(k)
}
Expand Down Expand Up @@ -52,6 +52,7 @@ type model struct {
hasTimeout bool
showHelp bool
help help.Model
keymap keymap
}

func (m model) Init() tea.Cmd {
Expand All @@ -77,7 +78,7 @@ func (m model) View() string {
lipgloss.Top,
m.textinput.View(),
"",
m.help.View(defaultKeymap()),
m.help.View(m.keymap),
)
}

Expand Down
1 change: 1 addition & 0 deletions write/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (o Options) Run() error {
autoWidth: o.Width < 1,
help: help.New(),
showHelp: o.ShowHelp,
keymap: defaultKeymap(),
}, tea.WithOutput(os.Stderr), tea.WithReportFocus())
tm, err := p.Run()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type model struct {
textarea textarea.Model
showHelp bool
help help.Model
keymap keymap
}

func (m model) Init() tea.Cmd { return textarea.Blink }
Expand All @@ -89,7 +90,7 @@ func (m model) View() string {
}
parts = append(parts, m.textarea.View())
if m.showHelp {
parts = append(parts, m.help.View(defaultKeymap()))
parts = append(parts, m.help.View(m.keymap))
}
return lipgloss.JoinVertical(lipgloss.Left, parts...)
}
Expand All @@ -114,7 +115,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.textarea.SetValue(msg.content)
case tea.KeyMsg:
km := defaultKeymap()
km := m.keymap
switch {
case key.Matches(msg, km.Quit):
m.aborted = true
Expand Down

0 comments on commit 12d2ce0

Please sign in to comment.