Skip to content

Commit f250123

Browse files
committed
fix: proper newline behavior and footer hint (Shift+Enter / Ctrl+J) (#871)
Signed-off-by: Rakesh S <rakesh.s552004@gmail.com>
1 parent 37d14c9 commit f250123

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

pkg/tui/components/editor/editor.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,55 @@ func (e *editor) Update(msg tea.Msg) (layout.Model, tea.Cmd) {
238238
return e, nil
239239
case tea.KeyPressMsg:
240240
switch msg.String() {
241-
case "enter":
241+
// Handle send/newline keys:
242+
// - Enter: submit current input (if textarea inserted a newline, submit previous buffer).
243+
// - Shift+Enter: insert newline when supported by terminal.
244+
// - Ctrl+J: fallback to insert '\n' when Shift+Enter isn’t reported.
245+
246+
case "enter", "shift+enter", "ctrl+j":
242247
if !e.textarea.Focused() {
243248
return e, nil
244249
}
250+
251+
prev := e.textarea.Value()
252+
e.textarea, _ = e.textarea.Update(msg)
253+
245254
value := e.textarea.Value()
255+
256+
// If the textarea inserted a newline due to plain Enter, submit the previous value.
257+
if value != prev && msg.String() == "enter" {
258+
if prev != "" && !e.working {
259+
e.textarea.SetValue(prev)
260+
e.textarea.MoveToEnd()
261+
e.textarea.Reset()
262+
e.refreshSuggestion()
263+
return e, core.CmdHandler(SendMsg{Content: prev})
264+
}
265+
return e, nil
266+
}
267+
268+
// Shift+Enter: insert newline when supported by terminal (textarea handles it).
269+
if value != prev {
270+
e.refreshSuggestion()
271+
return e, nil
272+
}
273+
274+
// Ctrl+J fallback: append LF to the buffer (always works across TTYs).
275+
if msg.String() == "ctrl+j" {
276+
current := e.textarea.Value()
277+
e.textarea.SetValue(current + "\n")
278+
e.textarea.MoveToEnd()
279+
e.refreshSuggestion()
280+
return e, nil
281+
}
282+
283+
// Normal Enter submit (no textarea change): send current value.
246284
if value != "" && !e.working {
247285
e.textarea.Reset()
248286
e.refreshSuggestion()
249287
return e, core.CmdHandler(SendMsg{Content: value})
250288
}
289+
251290
return e, nil
252291
case "ctrl+c":
253292
return e, tea.Quit

pkg/tui/page/chat/chat.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ type chatPage struct {
8080

8181
// KeyMap defines key bindings for the chat page
8282
type KeyMap struct {
83-
Tab key.Binding
84-
Cancel key.Binding
83+
Tab key.Binding
84+
Cancel key.Binding
85+
ShiftNewline key.Binding
86+
CtrlJ key.Binding
8587
}
8688

8789
// defaultKeyMap returns the default key bindings
@@ -94,6 +96,12 @@ func defaultKeyMap() KeyMap {
9496
Cancel: key.NewBinding(
9597
key.WithKeys("esc"),
9698
),
99+
// Show newline help in footer. Terminals that support Shift+Enter will use it.
100+
// Ctrl+J acts as a fallback on terminals that don't distinguish Shift+Enter.
101+
ShiftNewline: key.NewBinding(
102+
key.WithKeys("shift+enter", "ctrl+j"),
103+
key.WithHelp("shift+enter / ctrl+j", "newline"),
104+
),
97105
}
98106
}
99107

@@ -464,6 +472,8 @@ func (p *chatPage) Bindings() []key.Binding {
464472
bindings := []key.Binding{
465473
p.keyMap.Tab,
466474
p.keyMap.Cancel,
475+
// show newline hints in the global footer
476+
p.keyMap.ShiftNewline,
467477
}
468478

469479
if p.focusedPanel == PanelChat {

0 commit comments

Comments
 (0)