summaryrefslogtreecommitdiffhomepage
path: root/internal/tui/components/chat
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-04-23 12:08:45 +0200
committerKujtim Hoxha <[email protected]>2025-04-24 16:40:36 +0200
commit2c5003e3fc94766cf848962ea0ffe94875c35d2b (patch)
treeb822c8161b23b6828b557f5687a1c82b74bd2da4 /internal/tui/components/chat
parent1b22acbc58ffd67b3a42d01bfc320edcb68e5fc7 (diff)
downloadopencode-2c5003e3fc94766cf848962ea0ffe94875c35d2b.tar.gz
opencode-2c5003e3fc94766cf848962ea0ffe94875c35d2b.zip
remove edit/normal mode
Diffstat (limited to 'internal/tui/components/chat')
-rw-r--r--internal/tui/components/chat/editor.go64
-rw-r--r--internal/tui/components/chat/list.go63
2 files changed, 64 insertions, 63 deletions
diff --git a/internal/tui/components/chat/editor.go b/internal/tui/components/chat/editor.go
index 4f6937039..2d89803cd 100644
--- a/internal/tui/components/chat/editor.go
+++ b/internal/tui/components/chat/editor.go
@@ -26,7 +26,6 @@ type FocusEditorMsg bool
type focusedEditorKeyMaps struct {
Send key.Binding
OpenEditor key.Binding
- Blur key.Binding
}
type bluredEditorKeyMaps struct {
@@ -35,30 +34,11 @@ type bluredEditorKeyMaps struct {
OpenEditor key.Binding
}
-var focusedKeyMaps = focusedEditorKeyMaps{
+var KeyMaps = focusedEditorKeyMaps{
Send: key.NewBinding(
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "send message"),
),
- Blur: key.NewBinding(
- key.WithKeys("esc"),
- key.WithHelp("esc", "focus messages"),
- ),
- OpenEditor: key.NewBinding(
- key.WithKeys("ctrl+e"),
- key.WithHelp("ctrl+e", "open editor"),
- ),
-}
-
-var bluredKeyMaps = bluredEditorKeyMaps{
- Send: key.NewBinding(
- key.WithKeys("ctrl+s", "enter"),
- key.WithHelp("ctrl+s/enter", "send message"),
- ),
- Focus: key.NewBinding(
- key.WithKeys("i"),
- key.WithHelp("i", "focus editor"),
- ),
OpenEditor: key.NewBinding(
key.WithKeys("ctrl+e"),
key.WithHelp("ctrl+e", "open editor"),
@@ -88,6 +68,9 @@ func openEditor() tea.Cmd {
if err != nil {
return util.ReportError(err)
}
+ if len(content) == 0 {
+ return util.ReportWarn("Message is empty")
+ }
os.Remove(tmpfile.Name())
return SendMsg{
Text: string(content),
@@ -106,7 +89,6 @@ func (m *editorCmp) send() tea.Cmd {
value := m.textarea.Value()
m.textarea.Reset()
- m.textarea.Blur()
if value == "" {
return nil
}
@@ -131,26 +113,32 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
}
case tea.KeyMsg:
- if key.Matches(msg, focusedKeyMaps.OpenEditor) {
+ if key.Matches(msg, messageKeys.PageUp) || key.Matches(msg, messageKeys.PageDown) ||
+ key.Matches(msg, messageKeys.HalfPageUp) || key.Matches(msg, messageKeys.HalfPageDown) {
+ return m, nil
+ }
+ if key.Matches(msg, KeyMaps.OpenEditor) {
if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
return m, util.ReportWarn("Agent is working, please wait...")
}
return m, openEditor()
}
// if the key does not match any binding, return
- if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Send) {
+ if m.textarea.Focused() && key.Matches(msg, KeyMaps.Send) {
return m, m.send()
}
- if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Send) {
- return m, m.send()
- }
- if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Blur) {
- m.textarea.Blur()
- return m, util.CmdHandler(EditorFocusMsg(false))
- }
- if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Focus) {
- m.textarea.Focus()
- return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
+
+ // Handle Enter key
+ if m.textarea.Focused() && msg.String() == "enter" {
+ value := m.textarea.Value()
+ if len(value) > 0 && value[len(value)-1] == '\\' {
+ // If the last character is a backslash, remove it and add a newline
+ m.textarea.SetValue(value[:len(value)-1] + "\n")
+ return m, nil
+ } else {
+ // Otherwise, send the message
+ return m, m.send()
+ }
}
}
m.textarea, cmd = m.textarea.Update(msg)
@@ -175,13 +163,7 @@ func (m *editorCmp) GetSize() (int, int) {
func (m *editorCmp) BindingKeys() []key.Binding {
bindings := []key.Binding{}
- if m.textarea.Focused() {
- bindings = append(bindings, layout.KeyMapToSlice(focusedKeyMaps)...)
- } else {
- bindings = append(bindings, layout.KeyMapToSlice(bluredKeyMaps)...)
- }
-
- bindings = append(bindings, layout.KeyMapToSlice(m.textarea.KeyMap)...)
+ bindings = append(bindings, layout.KeyMapToSlice(KeyMaps)...)
return bindings
}
diff --git a/internal/tui/components/chat/list.go b/internal/tui/components/chat/list.go
index 03a50541e..3ed98f6b8 100644
--- a/internal/tui/components/chat/list.go
+++ b/internal/tui/components/chat/list.go
@@ -14,7 +14,6 @@ import (
"github.com/kujtimiihoxha/opencode/internal/message"
"github.com/kujtimiihoxha/opencode/internal/pubsub"
"github.com/kujtimiihoxha/opencode/internal/session"
- "github.com/kujtimiihoxha/opencode/internal/tui/layout"
"github.com/kujtimiihoxha/opencode/internal/tui/styles"
"github.com/kujtimiihoxha/opencode/internal/tui/util"
)
@@ -26,7 +25,6 @@ type cacheItem struct {
type messagesCmp struct {
app *app.App
width, height int
- writingMode bool
viewport viewport.Model
session session.Session
messages []message.Message
@@ -38,6 +36,32 @@ type messagesCmp struct {
}
type renderFinishedMsg struct{}
+type MessageKeys struct {
+ PageDown key.Binding
+ PageUp key.Binding
+ HalfPageUp key.Binding
+ HalfPageDown key.Binding
+}
+
+var messageKeys = MessageKeys{
+ PageDown: key.NewBinding(
+ key.WithKeys("pgdown"),
+ key.WithHelp("f/pgdn", "page down"),
+ ),
+ PageUp: key.NewBinding(
+ key.WithKeys("pgup"),
+ key.WithHelp("b/pgup", "page up"),
+ ),
+ HalfPageUp: key.NewBinding(
+ key.WithKeys("ctrl+u"),
+ key.WithHelp("ctrl+u", "½ page up"),
+ ),
+ HalfPageDown: key.NewBinding(
+ key.WithKeys("ctrl+d", "ctrl+d"),
+ key.WithHelp("ctrl+d", "½ page down"),
+ ),
+}
+
func (m *messagesCmp) Init() tea.Cmd {
return tea.Batch(m.viewport.Init(), m.spinner.Tick)
}
@@ -45,8 +69,7 @@ func (m *messagesCmp) Init() tea.Cmd {
func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
- case EditorFocusMsg:
- m.writingMode = bool(msg)
+
case SessionSelectedMsg:
if msg.ID != m.session.ID {
cmd := m.SetSession(msg)
@@ -63,10 +86,6 @@ func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case renderFinishedMsg:
m.rendering = false
m.viewport.GotoBottom()
- case tea.KeyMsg:
- if m.writingMode {
- return m, nil
- }
case pubsub.Event[message.Message]:
needsRerender := false
if msg.Type == pubsub.CreatedEvent {
@@ -326,22 +345,14 @@ func (m *messagesCmp) working() string {
func (m *messagesCmp) help() string {
text := ""
- if m.writingMode {
+ if m.app.CoderAgent.IsBusy() {
text += lipgloss.JoinHorizontal(
lipgloss.Left,
styles.BaseStyle.Foreground(styles.ForgroundDim).Bold(true).Render("press "),
styles.BaseStyle.Foreground(styles.Forground).Bold(true).Render("esc"),
- styles.BaseStyle.Foreground(styles.ForgroundDim).Bold(true).Render(" to exit writing mode"),
- )
- } else {
- text += lipgloss.JoinHorizontal(
- lipgloss.Left,
- styles.BaseStyle.Foreground(styles.ForgroundDim).Bold(true).Render("press "),
- styles.BaseStyle.Foreground(styles.Forground).Bold(true).Render("i"),
- styles.BaseStyle.Foreground(styles.ForgroundDim).Bold(true).Render(" to start writing"),
+ styles.BaseStyle.Foreground(styles.ForgroundDim).Bold(true).Render(" to exit cancel"),
)
}
-
return styles.BaseStyle.
Width(m.width).
Render(text)
@@ -398,18 +409,26 @@ func (m *messagesCmp) SetSession(session session.Session) tea.Cmd {
}
func (m *messagesCmp) BindingKeys() []key.Binding {
- bindings := layout.KeyMapToSlice(m.viewport.KeyMap)
- return bindings
+ return []key.Binding{
+ m.viewport.KeyMap.PageDown,
+ m.viewport.KeyMap.PageUp,
+ m.viewport.KeyMap.HalfPageUp,
+ m.viewport.KeyMap.HalfPageDown,
+ }
}
func NewMessagesCmp(app *app.App) tea.Model {
s := spinner.New()
s.Spinner = spinner.Pulse
+ vp := viewport.New(0, 0)
+ vp.KeyMap.PageUp = messageKeys.PageUp
+ vp.KeyMap.PageDown = messageKeys.PageDown
+ vp.KeyMap.HalfPageUp = messageKeys.HalfPageUp
+ vp.KeyMap.HalfPageDown = messageKeys.HalfPageDown
return &messagesCmp{
app: app,
- writingMode: true,
cachedContent: make(map[string]cacheItem),
- viewport: viewport.New(0, 0),
+ viewport: vp,
spinner: s,
}
}