Skip to content

Commit b2f2c9a

Browse files
committed
fix: use real cursor instead of virtual cursor
1 parent 6317222 commit b2f2c9a

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

‎packages/tui/internal/components/chat/editor.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type EditorComponent interface {
3131
tea.Model
3232
tea.ViewModel
3333
Content() string
34+
Cursor() *tea.Cursor
3435
Lines() int
3536
Value() string
3637
Length() int
@@ -407,6 +408,10 @@ func (m *editorComponent) Content() string {
407408
return content
408409
}
409410

411+
func (m *editorComponent) Cursor() *tea.Cursor {
412+
return m.textarea.Cursor()
413+
}
414+
410415
func (m *editorComponent) View() string {
411416
width := m.width
412417
if m.app.Session.ID == "" {
@@ -694,6 +699,7 @@ func NewEditorComponent(app *app.App) EditorComponent {
694699
ta.Prompt = " "
695700
ta.ShowLineNumbers = false
696701
ta.CharLimit = -1
702+
ta.VirtualCursor = false
697703
ta = updateTextareaStyles(ta)
698704

699705
m := &editorComponent{

‎packages/tui/internal/tui/tui.go‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const interruptDebounceTimeout = 1 * time.Second
5959
const exitDebounceTimeout = 1 * time.Second
6060

6161
type Model struct {
62+
tea.Model
63+
tea.CursorModel
6264
width, height int
6365
app *app.App
6466
modal layout.Modal
@@ -748,15 +750,17 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
748750
return a, tea.Batch(cmds...)
749751
}
750752

751-
func (a Model) View() string {
753+
func (a Model) View() (string, *tea.Cursor) {
752754
t := theme.CurrentTheme()
753755

754756
var mainLayout string
755757

758+
var editorX int
759+
var editorY int
756760
if a.app.Session.ID == "" {
757-
mainLayout = a.home()
761+
mainLayout, editorX, editorY = a.home()
758762
} else {
759-
mainLayout = a.chat()
763+
mainLayout, editorX, editorY = a.chat()
760764
}
761765
mainLayout = styles.NewStyle().
762766
Background(t.Background()).
@@ -780,7 +784,12 @@ func (a Model) View() string {
780784
if theme.CurrentThemeUsesAnsiColors() {
781785
mainLayout = util.ConvertRGBToAnsi16Colors(mainLayout)
782786
}
783-
return mainLayout + "\n" + a.status.View()
787+
788+
cursor := a.editor.Cursor()
789+
cursor.Position.X += editorX
790+
cursor.Position.Y += editorY
791+
792+
return mainLayout + "\n" + a.status.View(), cursor
784793
}
785794

786795
func (a Model) Cleanup() {
@@ -807,7 +816,7 @@ func (a Model) openFile(filepath string) (tea.Model, tea.Cmd) {
807816
return a, cmd
808817
}
809818

810-
func (a Model) home() string {
819+
func (a Model) home() (string, int, int) {
811820
t := theme.CurrentTheme()
812821
effectiveWidth := a.width - 4
813822
baseStyle := styles.NewStyle().Background(t.Background())
@@ -899,14 +908,21 @@ func (a Model) home() string {
899908
styles.WhitespaceStyle(t.Background()),
900909
)
901910

902-
editorX := (effectiveWidth - editorWidth) / 2
911+
editorX := max(0, (effectiveWidth-editorWidth)/2)
903912
editorY := (a.height / 2) + (mainHeight / 2) - 2
904913

905914
if editorLines > 1 {
915+
content := a.editor.Content()
916+
editorHeight := lipgloss.Height(content)
917+
918+
if editorY+editorHeight > a.height {
919+
difference := (editorY + editorHeight) - a.height
920+
editorY -= difference
921+
}
906922
mainLayout = layout.PlaceOverlay(
907923
editorX,
908924
editorY,
909-
a.editor.Content(),
925+
content,
910926
mainLayout,
911927
)
912928
}
@@ -924,10 +940,10 @@ func (a Model) home() string {
924940
)
925941
}
926942

927-
return mainLayout
943+
return mainLayout, editorX + 5, editorY + 2
928944
}
929945

930-
func (a Model) chat() string {
946+
func (a Model) chat() (string, int, int) {
931947
effectiveWidth := a.width - 4
932948
t := theme.CurrentTheme()
933949
editorView := a.editor.View()
@@ -944,14 +960,20 @@ func (a Model) chat() string {
944960
)
945961

946962
mainLayout := messagesView + "\n" + editorView
947-
editorX := (effectiveWidth - editorWidth) / 2
963+
editorX := max(0, (effectiveWidth-editorWidth)/2)
964+
editorY := a.height - editorHeight
948965

949966
if lines > 1 {
950-
editorY := a.height - editorHeight
967+
content := a.editor.Content()
968+
editorHeight := lipgloss.Height(content)
969+
if editorY+editorHeight > a.height {
970+
difference := (editorY + editorHeight) - a.height
971+
editorY -= difference
972+
}
951973
mainLayout = layout.PlaceOverlay(
952974
editorX,
953975
editorY,
954-
a.editor.Content(),
976+
content,
955977
mainLayout,
956978
)
957979
}
@@ -970,7 +992,7 @@ func (a Model) chat() string {
970992
)
971993
}
972994

973-
return mainLayout
995+
return mainLayout, editorX + 5, editorY + 2
974996
}
975997

976998
func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {

0 commit comments

Comments
 (0)