Skip to content

Commit

Permalink
Switch to ESC for back key (#153)
Browse files Browse the repository at this point in the history
* Allow sub components to handle *BackMsg

* Update helper text
  • Loading branch information
mdbenjam authored Dec 2, 2024
1 parent bb90d20 commit 03e6169
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
28 changes: 22 additions & 6 deletions pkg/tui/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type ClearScreenMsg struct {
NextMsg tea.Msg
}

type BackMsg struct {
Handled bool
}

func NewStack() *StackModel {
return &StackModel{}
}
Expand Down Expand Up @@ -105,6 +109,14 @@ func (m *StackModel) Init() tea.Cmd {
return tea.Batch(cmd)
}

func (m *StackModel) back() tea.Cmd {
m.Pop()
if len(m.stack) == 0 {
return tea.Quit
}
return m.Init()
}

func (m *StackModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if len(m.stack) == 0 {
return m, tea.Quit
Expand All @@ -118,11 +130,9 @@ func (m *StackModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyCtrlD:
m.Pop()
if len(m.stack) == 0 {
return m, tea.Quit
}
return m, m.Init()
return m, m.back()
case tea.KeyEsc:
subMsg = &BackMsg{}
case tea.KeyCtrlS:
// copy command to clipboard
if len(m.stack) > 0 {
Expand Down Expand Up @@ -194,6 +204,12 @@ func (m *StackModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.stack[len(m.stack)-1].Model, cmd = m.stack[len(m.stack)-1].Model.Update(subMsg)
}

if backMsg, ok := subMsg.(*BackMsg); ok {
if !backMsg.Handled {
return m, m.back()
}
}

return m, cmd
}

Expand Down Expand Up @@ -240,7 +256,7 @@ func (m *StackModel) header() string {

func (m *StackModel) footer() string {
quitCommand := fmt.Sprintf("%s: Quit", renderstyle.CommandKey.Render("[Ctrl+C]"))
prevCommand := fmt.Sprintf("%s: Back", renderstyle.CommandKey.Render("[Ctrl+D]"))
prevCommand := fmt.Sprintf("%s: Back", renderstyle.CommandKey.Render("[Esc]"))
saveToClipboard := fmt.Sprintf("%s: Copy command to clipboard", renderstyle.CommandKey.Render("[Ctrl+S]"))

var commands []string
Expand Down
11 changes: 10 additions & 1 deletion pkg/tui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (t *Table[T]) Init() tea.Cmd {

func (t *Table[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
subMsg := msg

switch msg := msg.(type) {
case LoadDataMsg[[]T]:
t.data = msg.Data
Expand Down Expand Up @@ -129,9 +131,16 @@ func (t *Table[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
}
case *BackMsg:
if t.Model.GetIsFilterInputFocused() {
subMsg = tea.KeyMsg{
Type: tea.KeyEsc,
}
msg.Handled = true
}
}

t.Model, cmd = t.Model.Update(msg)
t.Model, cmd = t.Model.Update(subMsg)
return t, cmd
}

Expand Down

0 comments on commit 03e6169

Please sign in to comment.