Skip to content

Commit e120d04

Browse files
committed
impv: text input
1 parent a29c41f commit e120d04

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

utils/text_input.go

+67-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"fmt"
5+
"unicode"
56

67
tea "github.com/charmbracelet/bubbletea"
78
"github.com/initia-labs/weave/styles"
@@ -35,39 +36,95 @@ func (ti *TextInput) WithPlaceholder(placeholder string) {
3536
func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
3637
switch msg := msg.(type) {
3738
case tea.KeyMsg:
38-
switch msg.Type {
39-
case tea.KeyEnter:
39+
switch {
40+
// Handle Enter key
41+
case msg.Type == tea.KeyEnter:
4042
ti.IsEntered = true
4143
return ti, nil, ti.ValidationFn(ti.Text) == nil
42-
case tea.KeyBackspace, tea.KeyCtrlH:
44+
45+
// Handle Backspace key
46+
case msg.Type == tea.KeyBackspace || msg.Type == tea.KeyCtrlH:
4347
ti.IsEntered = false
4448
if ti.Cursor > 0 && len(ti.Text) > 0 {
4549
ti.Text = ti.Text[:ti.Cursor-1] + ti.Text[ti.Cursor:]
4650
ti.Cursor--
4751
}
48-
case tea.KeyRunes, tea.KeySpace:
52+
53+
// Handle Option + Left (move one word left) - Detected as "alt+b"
54+
case msg.String() == "alt+b":
4955
ti.IsEntered = false
50-
ti.Text = ti.Text[:ti.Cursor] + string(msg.Runes) + ti.Text[ti.Cursor:]
51-
ti.Cursor += len(msg.Runes)
52-
case tea.KeyLeft:
56+
ti.Cursor = moveToPreviousWord(ti.Text, ti.Cursor)
57+
58+
// Handle Option + Right (move one word right) - Detected as "alt+f"
59+
case msg.String() == "alt+f":
60+
ti.IsEntered = false
61+
ti.Cursor = moveToNextWord(ti.Text, ti.Cursor)
62+
63+
// Handle Arrow Left (move cursor one character left)
64+
case msg.Type == tea.KeyLeft:
5365
ti.IsEntered = false
5466
if ti.Cursor > 0 {
5567
ti.Cursor--
5668
}
57-
case tea.KeyRight:
69+
70+
// Handle Arrow Right (move cursor one character right)
71+
case msg.Type == tea.KeyRight:
5872
ti.IsEntered = false
5973
if ti.Cursor < len(ti.Text) {
6074
ti.Cursor++
6175
}
62-
case tea.KeyCtrlC:
76+
77+
// Handle Ctrl+C (quit)
78+
case msg.Type == tea.KeyCtrlC:
6379
ti.IsEntered = false
6480
return ti, tea.Quit, false
65-
}
6681

82+
default:
83+
ti.IsEntered = false
84+
85+
// Normal typing
86+
ti.Text = ti.Text[:ti.Cursor] + string(msg.Runes) + ti.Text[ti.Cursor:]
87+
ti.Cursor += len(msg.Runes)
88+
89+
}
6790
}
6891
return ti, nil, false
6992
}
7093

94+
// Helper function to move the cursor to the beginning of the previous word
95+
func moveToPreviousWord(text string, cursor int) int {
96+
if cursor == 0 {
97+
return 0
98+
}
99+
100+
// Move cursor left while encountering spaces
101+
for cursor > 0 && unicode.IsSpace(rune(text[cursor-1])) {
102+
cursor--
103+
}
104+
// Move cursor left until the beginning of the word is found
105+
for cursor > 0 && !unicode.IsSpace(rune(text[cursor-1])) {
106+
cursor--
107+
}
108+
return cursor
109+
}
110+
111+
// Helper function to move the cursor to the beginning of the next word
112+
func moveToNextWord(text string, cursor int) int {
113+
if cursor >= len(text) {
114+
return len(text)
115+
}
116+
117+
// Move cursor right while encountering non-space characters (current word)
118+
for cursor < len(text) && !unicode.IsSpace(rune(text[cursor])) {
119+
cursor++
120+
}
121+
// Move cursor right while encountering spaces
122+
for cursor < len(text) && unicode.IsSpace(rune(text[cursor])) {
123+
cursor++
124+
}
125+
return cursor
126+
}
127+
71128
func (ti TextInput) View() string {
72129
var beforeCursor, cursorChar, afterCursor string
73130
bottomText := styles.Text("Press Enter to submit or Ctrl+C to quit.", styles.Gray)

0 commit comments

Comments
 (0)