Skip to content

Commit

Permalink
feat!: standardize navigation keys and change untaint key (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Aug 1, 2024
1 parent 7338004 commit 178bff4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Press `s` to go to the state page, listing a workspace's resources.
|`D`|Run `terraform state rm`|✓|
|`M`|Run `terraform state mv`|✗|
|`Ctrl+t`|Run `terraform taint`|✓|
|`Ctrl+u`|Run `terraform untaint`|✓|
|`U`|Run `terraform untaint`|✓|
|`Ctrl+r`|Run `terraform state pull`|-|

### Tasks
Expand Down Expand Up @@ -249,8 +249,10 @@ Common vim key bindings are supported for navigation.
|--|--|
|`Up/k`|Up one row|
|`Down/j`|Down one row|
|`PgUp/b`|Up one page|
|`PgDown/f`|Down one page|
|`PgUp`|Up one page|
|`PgDown`|Down one page|
|`Ctrl+u`|Up half page|
|`Ctrl+d`|Down half page|
|`Home/g`|Go to top|
|`End/G`|Go to bottom|

Expand Down
2 changes: 1 addition & 1 deletion demo/vhs.tape
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Escape Sleep 0.5s
# select all resources
Ctrl+a Sleep 0.5s
# taint all resources
Ctrl+u
Type "U"
# we're taken to the untaint task group page
Sleep 0.5s
# preview output for several tasks
Expand Down
4 changes: 2 additions & 2 deletions internal/app/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestState_SingleTaint_Untaint(t *testing.T) {
})

// Untaint resource
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlU})
tm.Type("U")

// Expect to be taken to task page for untaint
waitFor(t, tm, func(s string) bool {
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestState_MultipleTaint_Untaint(t *testing.T) {
// Untaint all resources (need to select all again, because resources have
// been reloaded).
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlA})
tm.Send(tea.KeyMsg{Type: tea.KeyCtrlU})
tm.Type("U")

// Expect to be taken to task group page for untaint
waitFor(t, tm, func(s string) bool {
Expand Down
30 changes: 20 additions & 10 deletions internal/tui/keys/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
)

type navigation struct {
LineUp key.Binding
LineDown key.Binding
PageUp key.Binding
PageDown key.Binding
GotoTop key.Binding
GotoBottom key.Binding
LineUp key.Binding
LineDown key.Binding
PageUp key.Binding
PageDown key.Binding
HalfPageUp key.Binding
HalfPageDown key.Binding
GotoTop key.Binding
GotoBottom key.Binding
}

// Navigation returns key bindings for navigation.
Expand All @@ -24,12 +26,20 @@ var Navigation = navigation{
key.WithHelp("↓/j", "down"),
),
PageUp: key.NewBinding(
key.WithKeys("b", "pgup"),
key.WithHelp("b/pgup", "page up"),
key.WithKeys("pgup"),
key.WithHelp("pgup", "page up"),
),
PageDown: key.NewBinding(
key.WithKeys("f", "pgdown"),
key.WithHelp("f/pgdn", "page down"),
key.WithKeys("pgdown"),
key.WithHelp("pgdn", "page down"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("ctrl+u"),
key.WithHelp("ctrl+u", "½ page up"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("ctrl+d"),
key.WithHelp("ctrl+d", "½ page down"),
),
GotoTop: key.NewBinding(
key.WithKeys("home", "g"),
Expand Down
4 changes: 4 additions & 0 deletions internal/tui/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func (m Model[V]) Update(msg tea.Msg) (Model[V], tea.Cmd) {
m.MoveUp(m.viewport.Height)
case key.Matches(msg, keys.Navigation.PageDown):
m.MoveDown(m.viewport.Height)
case key.Matches(msg, keys.Navigation.HalfPageUp):
m.MoveUp(m.viewport.Height / 2)
case key.Matches(msg, keys.Navigation.HalfPageDown):
m.MoveDown(m.viewport.Height / 2)
case key.Matches(msg, keys.Navigation.GotoTop):
m.GotoTop()
case key.Matches(msg, keys.Navigation.GotoBottom):
Expand Down
12 changes: 12 additions & 0 deletions internal/tui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package tui
import (
"fmt"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/hokaccha/go-prettyjson"
"github.com/leg100/pug/internal/tui/keys"
"github.com/leg100/reflow/wrap"
)

Expand Down Expand Up @@ -52,6 +54,16 @@ func (m Viewport) Update(msg tea.Msg) (Viewport, tea.Cmd) {
cmds []tea.Cmd
)

switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Navigation.GotoTop):
m.viewport.SetYOffset(0)
case key.Matches(msg, keys.Navigation.GotoBottom):
m.viewport.SetYOffset(m.viewport.TotalLineCount())
}
}

// Handle keyboard and mouse events in the viewport
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/workspace/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var resourcesKeys = resourcesKeyMap{
key.WithHelp("ctrl+t", "taint"),
),
Untaint: key.NewBinding(
key.WithKeys("ctrl+u"),
key.WithHelp("ctrl+u", "untaint"),
key.WithKeys("U"),
key.WithHelp("U", "untaint"),
),
Move: key.NewBinding(
key.WithKeys("M"),
Expand Down

0 comments on commit 178bff4

Please sign in to comment.