~adnano/astronaut

ref: fe7ff9ba0883926a38681e6abb9d36a77d78a1e0 astronaut/ui/input.go -rw-r--r-- 4.7 KiB
fe7ff9baAdnan Maolood Update dependencies 2 months ago
                                                                                
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ui

import (
	"astronaut/styles"

	"github.com/gdamore/tcell/v2"
	"github.com/mattn/go-runewidth"
)

// The following code is modified from git.sr.ht/~sircmpwn/aerc/lib/ui/textinput.go

// Copyright (c) 2018-2019 Drew DeVault
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Input represents a text input.
type Input struct {
	view      *View
	prompt    string
	input     []rune
	focus     bool
	sensitive bool
	confirm   bool
	index     int
	scroll    int
}

// SetView sets the UI view.
func (i *Input) SetView(view *View) {
	i.view = view
}

// Draw draws the text input.
func (i *Input) Draw() {
	if !i.focus {
		i.view.HideCursor()
	}

	style := styles.Default
	x := i.view.DrawText(0, 0, i.prompt, style.Bold(true))

	w, _ := i.view.Size()
	w -= x
	if i.index >= i.scroll+w {
		i.scroll = i.index - w + 1
	}
	if i.index < i.scroll {
		i.scroll = i.index
	}

	input := string(i.input[i.scroll:])

	if i.sensitive {
		cells := runewidth.StringWidth(input)
		for j := 0; j < cells; j++ {
			i.view.SetContent(x+j, 0, '*', nil, style)
		}
	} else {
		i.view.DrawText(x, 0, input, style)
	}

	sindex := i.index - i.scroll
	cells := runewidth.StringWidth(string(i.input[:sindex]))
	if i.focus {
		i.view.ShowCursor(x+cells, 0)
	}
}

// Prompt sets the text input prompt.
func (i *Input) Prompt(prompt string) {
	i.view.Invalidate()
	i.Reset()
	i.focus = true
	i.prompt = prompt
}

// Sensitive specifies whether the input is considered sensitive.
func (i *Input) Sensitive(sensitive bool) {
	i.sensitive = sensitive
}

// SetInput sets the current input.
func (i *Input) SetInput(input string) {
	i.input = []rune(input)
	i.index = len(i.input)
}

// Reset resets the text input.
func (i *Input) Reset() {
	i.focus = false
	i.sensitive = false
	i.input = nil
	i.prompt = ""
	i.index = 0
	i.scroll = 0
}

// Event handles events.
func (i *Input) Event(event tcell.Event) (string, bool) {
	switch event := event.(type) {
	case *tcell.EventKey:
		switch event.Key() {
		case tcell.KeyBackspace, tcell.KeyBackspace2:
			i.backspace()
			i.view.Invalidate()
		case tcell.KeyDelete:
			i.delete()
			i.view.Invalidate()
		case tcell.KeyLeft:
			if i.index > 0 {
				i.index--
			}
			i.view.Invalidate()
		case tcell.KeyRight:
			if i.index < len(i.input) {
				i.index++
			}
			i.view.Invalidate()
		case tcell.KeyHome:
			i.index = 0
			i.view.Invalidate()
		case tcell.KeyEnd:
			i.index = len(i.input)
			i.view.Invalidate()
		case tcell.KeyESC:
			i.Reset()
			i.view.Invalidate()
			return "", true
		case tcell.KeyRune:
			i.insert(event.Rune())
			i.view.Invalidate()
		case tcell.KeyEnter:
			input := string(i.input)
			i.Reset()
			i.view.Invalidate()
			return input, true
		}
	case *tcell.EventMouse:
		if !i.view.Contains(event.Position()) {
			break
		}
		buttons := event.Buttons()
		if buttons&tcell.Button1 != 0 {
			x, _ := i.view.GetLocal(event.Position())
			i.index = x - runewidth.StringWidth(i.prompt)
			if i.index < 0 {
				i.index = 0
			}
			if i.index >= len(i.input) {
				i.index = len(i.input)
			}
			i.view.Invalidate()
		}
		if buttons&tcell.WheelDown != 0 {
			if i.index < len(i.input) {
				i.index++
				i.view.Invalidate()
			}
		}
		if buttons&tcell.WheelUp != 0 {
			if i.index > 0 {
				i.index--
				i.view.Invalidate()
			}
		}
	}
	return "", false
}

func (i *Input) backspace() {
	if i.scroll > 0 {
		i.scroll--
	}
	if len(i.input) > 0 && i.index != 0 {
		copy(i.input[i.index-1:], i.input[i.index:])
		i.input = i.input[:len(i.input)-1]
		i.index--
	}
}

func (i *Input) delete() {
	if len(i.input) > 0 && i.index != len(i.input) {
		copy(i.input[i.index:], i.input[i.index+1:])
		i.input = i.input[:len(i.input)-1]
	}
}

func (i *Input) insert(r rune) {
	i.input = append(i.input, 0)
	copy(i.input[i.index+1:], i.input[i.index:])
	i.input[i.index] = r
	i.index++
}