xonecas.io
62159
Sy

symb / files

symb / internal / tui / update_keypress.go

2.8 KB
1package tui
2
3import (
4	tea "charm.land/bubbletea/v2"
5	"github.com/rs/zerolog/log"
6)
7
8// handleKeyPress processes key events. Returns (model, cmd, true) if handled.
9func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
10	handler := m.keyPressHandlers()[msg.Keystroke()]
11	if handler == nil {
12		return Model{}, nil, false
13	}
14	return handler(m)
15}
16
17func (m *Model) keyPressHandlers() map[string]func(*Model) (Model, tea.Cmd, bool) {
18	return map[string]func(*Model) (Model, tea.Cmd, bool){
19		"ctrl+c":       (*Model).handleCtrlC,
20		"ctrl+shift+c": (*Model).handleCtrlShiftC,
21		"ctrl+shift+v": (*Model).handleCtrlShiftV,
22		"esc":          (*Model).handleEsc,
23		"enter":        (*Model).handleEnter,
24		"@":            (*Model).handleAtSign,
25		"ctrl+h":       (*Model).handleCtrlH,
26		"ctrl+m":       (*Model).handleCtrlM,
27	}
28}
29
30func (m *Model) handleCtrlC() (Model, tea.Cmd, bool) {
31	return *m, tea.Batch(m.cancelProgramCmd(), m.flushAndQuit()), true
32}
33
34func (m *Model) handleCtrlShiftC() (Model, tea.Cmd, bool) {
35	if cmd := m.copySelection(); cmd != nil {
36		return *m, cmd, true
37	}
38	return *m, nil, true
39}
40
41func (m *Model) handleCtrlShiftV() (Model, tea.Cmd, bool) {
42	return *m, tea.ReadClipboard, true
43}
44
45func (m *Model) handleEsc() (Model, tea.Cmd, bool) {
46	if m.llmInFlight {
47		cmd := m.cancelTurnCmd()
48		m.cancelTurn()
49		return *m, tea.Batch(cmd, m.waitForLLMUpdate()), true
50	}
51	m.agentInput.Blur()
52	return *m, nil, true
53}
54
55func (m *Model) cancelProgramCmd() tea.Cmd {
56	if m.cancel == nil {
57		return nil
58	}
59	cancel := m.cancel
60	return func() tea.Msg {
61		cancel()
62		return nil
63	}
64}
65
66func (m *Model) handleEnter() (Model, tea.Cmd, bool) {
67	if m.agentInput.Value() != "" && m.turnCancel == nil && !m.turnPending && !m.undoInFlight {
68		display := m.agentInput.Value()
69		m.agentInput.Reset()
70		return *m, m.sendToLLM(display, expandAtMentions(display)), true
71	}
72	return Model{}, nil, false
73}
74
75func (m *Model) handleAtSign() (Model, tea.Cmd, bool) {
76	if m.searcher == nil || !m.agentInput.Focused() {
77		return Model{}, nil, false
78	}
79	m.agentInput.DeleteSelection()
80	m.agentInput.InsertText("@")
81	m.atOffset = m.agentInput.CursorOffset() // offset is now just after the @
82	m.openFileModal()
83	return *m, nil, true
84}
85
86func (m *Model) handleCtrlH() (Model, tea.Cmd, bool) {
87	m.openKeybindsModal()
88	return *m, nil, true
89}
90
91func (m *Model) handleCtrlM() (Model, tea.Cmd, bool) {
92	log.Info().Bool("turnCancel", m.turnCancel != nil).Bool("turnPending", m.turnPending).Bool("undoInFlight", m.undoInFlight).Msg("handleCtrlM")
93	if m.turnCancel != nil || m.turnPending || m.undoInFlight {
94		return *m, nil, false
95	}
96	return *m, m.fetchModelsCmd(), true
97}
98
99func (m *Model) flushAndQuit() tea.Cmd {
100	queue := m.storeQueue
101	done := m.storeQueueDone
102	return func() tea.Msg {
103		if queue != nil {
104			close(queue)
105			queue = nil
106		}
107		if done != nil {
108			<-done
109		}
110		return tea.Quit()
111	}
112}