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
5 changes: 5 additions & 0 deletions internal/app/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "charm.land/bubbles/v2/key"
type KeyMap struct {
Quit key.Binding
Interrupt key.Binding
Clear key.Binding
}

// DefaultKeyMap returns the default keybindings.
Expand All @@ -19,5 +20,9 @@ func DefaultKeyMap() KeyMap {
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "interrupt"),
),
Clear: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc esc", "clear"),
),
}
}
33 changes: 33 additions & 0 deletions internal/app/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"strings"
"time"

"charm.land/bubbles/v2/key"
"charm.land/bubbles/v2/spinner"
Expand All @@ -27,6 +28,7 @@ type State int
const (
StateInput State = iota
StateExecuting
StatePending
)

// ReadyMsg signals the ui that execution is done and it should prompt.
Expand All @@ -41,6 +43,10 @@ type QuitRequestMsg struct{}
// ConfirmQuitMsg is used internally to finalize quitting.
type ConfirmQuitMsg struct{}

// seqTimeoutMsg is sent after 500ms to cancel a pending key
// seq if the second key was not pressed on time.
type seqTimeoutMsg struct{ seq int }

type cancel func(ctx context.Context) error

type execute func(query string) tea.Cmd
Expand All @@ -56,6 +62,7 @@ type Model struct {
prevUserInput string
version string
highlighter func(string) string
escSeq int

keys KeyMap
styles Styles
Expand Down Expand Up @@ -168,7 +175,17 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m.handleInput()

case seqTimeoutMsg:
if msg.seq == m.escSeq {
m.state = StateInput
}
return m, nil

case tea.KeyMsg:
if m.state == StatePending && !key.Matches(msg, m.keys.Clear) {
m.state = StateInput
}

if key.Matches(msg, m.keys.Quit) {
return m, func() tea.Msg {
return QuitRequestMsg{}
Expand All @@ -189,6 +206,22 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.input.Reset()
return m, nil
}
if key.Matches(msg, m.keys.Clear) {
if m.state == StateExecuting {
return m, nil
}
if m.state == StatePending {
m.state = StateInput
m.input.Reset()
return m, nil
}
m.state = StatePending
m.escSeq++
n := m.escSeq
return m, tea.Tick(500*time.Millisecond, func(t time.Time) tea.Msg {
return seqTimeoutMsg{seq: n}
})
}
}

// Route to input only if in input state, avoiding capturing keystrokes while executing.
Expand Down
1 change: 0 additions & 1 deletion pgxspecial/dbcommands/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ func RequiresRowResult(t *testing.T, r pgxspecial.SpecialCommandResult) pgxspeci
t.Fatalf("expected rows result, got %T", r)
}


rowsResult, ok := r.(pgxspecial.RowResult)
if !ok {
t.Fatalf("expected RowsResult, got %T", r)
Expand Down
2 changes: 1 addition & 1 deletion pgxspecial/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func Export() []CommandExport {
for key, cmd := range commandRegistry {
// key contains the command name with the alias
// \quit, '\q, \exit has the same command, only differs by key/alias
cmd := New(key, cmd.Syntax, cmd.Description)
cmd := New(key, cmd.Syntax, cmd.Description)
cmds = append(cmds, cmd)
}
return cmds
Expand Down
2 changes: 0 additions & 2 deletions pgxspecial/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ func TestExecuteCommand(t *testing.T) {
t.Errorf("Expected result kind to be rows, got: %T", result)
}


rows := result.(pgxspecial.RowResult).Rows

defer rows.Close()
Expand Down Expand Up @@ -202,7 +201,6 @@ func TestRegisterCommandAlias(t *testing.T) {
t.Errorf("Expected result kind to be rows, got: %T", result)
}


rows := result.(pgxspecial.RowResult).Rows
defer rows.Close()

Expand Down
Loading