summaryrefslogtreecommitdiff
path: root/tui/text_input.go
blob: 3bceb24fc08e1b55b61d25dc827b1d7789ab84b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tui

import (
	"github.com/rivo/uniseg"
	"zgo.at/termfo/keys"
	"strings"
)

type TextInput struct {
	id string
	linesBefore []string
	beforeCursor string
	selection string
	selectionBefore bool
	afterCursor string
	linesAfter []string
}

func (t *TextInput) Text() string {
	return t.beforeCursor + t.selection + t.afterCursor
}

func (t *TextInput) SetText(text string) {
	t.beforeCursor = ""
	t.afterCursor = text
}

func toGraphemes(s string) []string {
	g := uniseg.NewGraphemes(s)
	var result []string
	for g.Next() {
		result = append(result, g.Str())
	}
	return result
}

func splitRight(s string) (string, string) {
	if s == "" {
		return "", ""
	}
	gs := toGraphemes(s)
	return strings.Join(gs[:len(gs) - 1], ""), gs[len(gs) - 1]
}

func splitLeft(s string) (string, string) {
	if s == "" {
		return "", ""
	}
	cluster, rest, _, _ := uniseg.FirstGraphemeCluster([]byte(s), 0)
	return string(cluster), string(rest)
}

func (t *TextInput) Left(selection bool, word bool) {
	t.Deselect()
	var right string
	t.beforeCursor, right = splitRight(t.beforeCursor)
	t.afterCursor = right + t.afterCursor
}

func (t *TextInput) Right(selection bool, word bool) {
	t.Deselect()
	var left string
	left, t.afterCursor = splitLeft(t.afterCursor)
	t.beforeCursor += left
}

func (t *TextInput) Start(selection bool) {
	t.afterCursor = t.beforeCursor + t.afterCursor
	t.beforeCursor = ""
}

func (t *TextInput) End(selection bool) {
	t.beforeCursor = t.beforeCursor + t.afterCursor
	t.afterCursor = ""
}

func (t *TextInput) Selection() string {
	return t.selection
}

func (t *TextInput) Deselect() {
	if t.selectionBefore {
		t.beforeCursor += t.selection
	} else {
		t.afterCursor = t.selection + t.afterCursor
	}
	t.selection = ""
}

func (t *TextInput) Write(text string) {
	t.selection = ""
	t.beforeCursor += text
}

func (t *TextInput) Update(ev Event) (usedKeybind bool) {
	if Selected != t.id {
		return
	}
	if ev.TextInput != 0 {
		t.Write(string(ev.TextInput))
	}

	selection := ev.Key & keys.Shift != 0
	word := ev.Key & keys.Ctrl != 0
	switch ev.Key.WithoutMods() {
	case keys.Left:
		t.Left(selection, word)
	case keys.Right:
		t.Right(selection, word)
	case 'a':
		if ev.Key & keys.Ctrl != 0 {
			t.Start(selection)
		}
	case 'e':
		if ev.Key & keys.Ctrl != 0 {
			t.End(selection)
		}
	case keys.Backspace:
		if t.selection != "" {
			t.selection = ""
		} else {
			t.beforeCursor, _ = splitRight(t.beforeCursor)
		}
	default:
		return false
	}
	return true
}

func (t *TextInput) Show(id string) {
	t.id = id
	Push(id, Box {Width: Fill, Height: 4})//TextSize})
	Text(t.beforeCursor, nil)
	if t.selectionBefore {
		Text(t.selection, &Style {Bg: Blue, Fg: White, selected: true})
	}
	Cursor()
	if !t.selectionBefore {
		Text(t.selection, &Style {Bg: Blue, Fg: White, selected: true})
	}
	Text(t.afterCursor, nil)
	Pop()
}