tui: update/clean up input handling
This commit is contained in:
@@ -2,7 +2,6 @@ package tui
|
||||
|
||||
// The terminal UI for lmcli, launched from the `lmcli chat` command
|
||||
// TODO:
|
||||
// - conversation list view
|
||||
// - change model
|
||||
// - rename conversation
|
||||
// - set system prompt
|
||||
@@ -83,28 +82,33 @@ func (m model) Init() tea.Cmd {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
|
||||
switch msg.String() {
|
||||
case "ctrl+c":
|
||||
if m.chat.waitingForReply {
|
||||
m.chat.stopSignal <- struct{}{}
|
||||
return nil
|
||||
} else {
|
||||
return tea.Quit
|
||||
func (m *model) handleGlobalInput(msg tea.KeyMsg) (bool, tea.Cmd) {
|
||||
// delegate input to the active child state first, only handling it at the
|
||||
// global level if the child state does not
|
||||
var cmds []tea.Cmd
|
||||
switch m.state {
|
||||
case stateChat:
|
||||
handled, cmd := m.chat.handleInput(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
if handled {
|
||||
m.chat, cmd = m.chat.Update(nil)
|
||||
cmds = append(cmds, cmd)
|
||||
return true, tea.Batch(cmds...)
|
||||
}
|
||||
case "q":
|
||||
if m.chat.focus != focusInput {
|
||||
return tea.Quit
|
||||
}
|
||||
default:
|
||||
switch m.state {
|
||||
case stateChat:
|
||||
return m.chat.handleInput(msg)
|
||||
case stateConversations:
|
||||
return m.conversations.handleInput(msg)
|
||||
case stateConversations:
|
||||
handled, cmd := m.conversations.handleInput(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
if handled {
|
||||
m.conversations, cmd = m.conversations.Update(nil)
|
||||
cmds = append(cmds, cmd)
|
||||
return true, tea.Batch(cmds...)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "ctrl+q":
|
||||
return true, tea.Quit
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
@@ -112,8 +116,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
cmd := m.handleGlobalInput(msg)
|
||||
if cmd != nil {
|
||||
handled, cmd := m.handleGlobalInput(msg)
|
||||
if handled {
|
||||
return m, cmd
|
||||
}
|
||||
case msgChangeState:
|
||||
|
||||
Reference in New Issue
Block a user