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
67 changes: 59 additions & 8 deletions internal/tui/keybinds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tui

import (
"strings"
"tusshi/internal/constants"
"tusshi/internal/ssh"
"tusshi/internal/tui/commands"
Expand Down Expand Up @@ -113,13 +114,7 @@ func (m *Model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return m, m.ActiveComponent.Init()

case utils.MatchesMultipleStringOptions(msg.String(), constants.KeyEnter):
if len(m.Filtered) > 0 {
selected := m.Filtered[m.SelectedIndex]
sshCmd := ssh.NewSSHCommand(selected.Alias)
return m, tea.ExecProcess(sshCmd, func(err error) tea.Msg {
return SSHFinishedMsg{Err: err}
})
}
return m.connectSelectedHost()
}

return m, nil
Expand All @@ -128,9 +123,32 @@ func (m *Model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
// handleSearchKey processes keyboard input when performing a text search.
func (m *Model) handleSearchKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case constants.KeyEsc, constants.KeyEnter:
case constants.KeyEsc:
m.Mode = ModeNormal
m.SearchInput.Blur()
return m, nil

case constants.KeyEnter:
m.Mode = ModeNormal
m.SearchInput.Blur()
return m.connectSelectedHost()

case "up": // TODO: maybe refactor keybinds constants? but this would likely never change...
if m.SelectedIndex > 0 {
m.SelectedIndex--
}
return m, nil

case "down":
if m.SelectedIndex < len(m.Filtered)-1 {
m.SelectedIndex++
}
return m, nil

// TODO: test on multiple terminal emulators
case "ctrl+backspace", "ctrl+h", "ctrl+w": // why: kitty and xterm-compatible terminals transmit ctrl+backspace as ctrl+h
deleteWordBackwards(&m.SearchInput)
m.FilterHosts()
return m, nil
}

Expand Down Expand Up @@ -159,3 +177,36 @@ func (m *Model) handleCommandKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.CommandInput, cmdCmd = m.CommandInput.Update(msg)
return m, cmdCmd
}

func (m *Model) connectSelectedHost() (tea.Model, tea.Cmd) {
if len(m.Filtered) == 0 {
return m, nil
}
selected := m.Filtered[m.SelectedIndex]
sshCmd := ssh.NewSSHCommand(selected.Alias)
return m, tea.ExecProcess(sshCmd, func(err error) tea.Msg {
return SSHFinishedMsg{Err: err}
})

}

func deleteWordBackwards(ti *textinput.Model) {
val := ti.Value()
pos := ti.Position()
if pos == 0 || len(val) == 0 {
return
}

left := strings.TrimRight(val[:pos], " ")
lastSpace := strings.LastIndex(left, " ")

var newVal string
if lastSpace == -1 {
newVal = val[pos:]
ti.SetCursor(0)
} else {
newVal = left[:lastSpace+1] + val[pos:]
ti.SetCursor(lastSpace + 1)
}
ti.SetValue(newVal)
}
1 change: 1 addition & 0 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (m *Model) Reload() {
}

// FilterHosts filters and fuzzy-matches the host list using active tab and search query.
// Handles index under & overflow for selectedIndex
func (m *Model) FilterHosts() {
var filtered []*config.Host
searchQ := strings.ToLower(m.SearchInput.Value())
Expand Down
Loading