summaryrefslogtreecommitdiffhomepage
path: root/internal/tui/components/repl/editor.go
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-03-23 19:19:08 +0100
committerKujtim Hoxha <[email protected]>2025-03-23 19:19:08 +0100
commit8daa6e774a6e02698c90392e7b2008542f789594 (patch)
tree8053fc1a4de87fc8dc3fd6e776577b0221c02ac5 /internal/tui/components/repl/editor.go
parent796bbf4d66c0fc70e750c7f582019cb9a7298e59 (diff)
downloadopencode-8daa6e774a6e02698c90392e7b2008542f789594.tar.gz
opencode-8daa6e774a6e02698c90392e7b2008542f789594.zip
add initial stuff
Diffstat (limited to 'internal/tui/components/repl/editor.go')
-rw-r--r--internal/tui/components/repl/editor.go127
1 files changed, 118 insertions, 9 deletions
diff --git a/internal/tui/components/repl/editor.go b/internal/tui/components/repl/editor.go
index 8a04889ab..1de216bd0 100644
--- a/internal/tui/components/repl/editor.go
+++ b/internal/tui/components/repl/editor.go
@@ -1,21 +1,130 @@
package repl
-import tea "github.com/charmbracelet/bubbletea"
+import (
+ "github.com/charmbracelet/bubbles/key"
+ tea "github.com/charmbracelet/bubbletea"
+ "github.com/kujtimiihoxha/termai/internal/app"
+ "github.com/kujtimiihoxha/termai/internal/tui/layout"
+ "github.com/kujtimiihoxha/vimtea"
+)
-type editorCmp struct{}
+type EditorCmp interface {
+ tea.Model
+ layout.Focusable
+ layout.Sizeable
+ layout.Bordered
+}
+
+type editorCmp struct {
+ app *app.App
+ editor vimtea.Editor
+ editorMode vimtea.EditorMode
+ sessionID string
+ focused bool
+ width int
+ height int
+}
+
+type localKeyMap struct {
+ SendMessage key.Binding
+ SendMessageI key.Binding
+}
+
+var keyMap = localKeyMap{
+ SendMessage: key.NewBinding(
+ key.WithKeys("enter"),
+ key.WithHelp("enter", "send message normal mode"),
+ ),
+ SendMessageI: key.NewBinding(
+ key.WithKeys("ctrl+s"),
+ key.WithHelp("ctrl+s", "send message insert mode"),
+ ),
+}
+
+func (m *editorCmp) Init() tea.Cmd {
+ return m.editor.Init()
+}
+
+func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+ switch msg := msg.(type) {
+ case vimtea.EditorModeMsg:
+ m.editorMode = msg.Mode
+ case SelectedSessionMsg:
+ if msg.SessionID != m.sessionID {
+ m.sessionID = msg.SessionID
+ }
+ }
+ if m.IsFocused() {
+ switch msg := msg.(type) {
+ case tea.KeyMsg:
+ switch {
+ case key.Matches(msg, keyMap.SendMessage):
+ if m.editorMode == vimtea.ModeNormal {
+ return m, m.Send()
+ }
+ case key.Matches(msg, keyMap.SendMessageI):
+ if m.editorMode == vimtea.ModeInsert {
+ return m, m.Send()
+ }
+ }
+ }
+ u, cmd := m.editor.Update(msg)
+ m.editor = u.(vimtea.Editor)
+ return m, cmd
+ }
+ return m, nil
+}
-func (i *editorCmp) Init() tea.Cmd {
+// Blur implements EditorCmp.
+func (m *editorCmp) Blur() tea.Cmd {
+ m.focused = false
return nil
}
-func (i *editorCmp) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
- return i, nil
+// BorderText implements EditorCmp.
+func (m *editorCmp) BorderText() map[layout.BorderPosition]string {
+ return map[layout.BorderPosition]string{
+ layout.TopLeftBorder: "New Message",
+ }
+}
+
+// Focus implements EditorCmp.
+func (m *editorCmp) Focus() tea.Cmd {
+ m.focused = true
+ return m.editor.Tick()
+}
+
+// GetSize implements EditorCmp.
+func (m *editorCmp) GetSize() (int, int) {
+ return m.width, m.height
+}
+
+// IsFocused implements EditorCmp.
+func (m *editorCmp) IsFocused() bool {
+ return m.focused
+}
+
+// SetSize implements EditorCmp.
+func (m *editorCmp) SetSize(width int, height int) {
+ m.width = width
+ m.height = height
+ m.editor.SetSize(width, height)
+}
+
+func (m *editorCmp) Send() tea.Cmd {
+ return func() tea.Msg {
+ // TODO: Send message
+ return nil
+ }
}
-func (i *editorCmp) View() string {
- return "Editor"
+func (m *editorCmp) View() string {
+ return m.editor.View()
}
-func NewEditorCmp() tea.Model {
- return &editorCmp{}
+func NewEditorCmp(app *app.App) EditorCmp {
+ return &editorCmp{
+ app: app,
+ editor: vimtea.NewEditor(),
+ }
}