This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textarea.go
126 lines (108 loc) · 2.4 KB
/
textarea.go
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
// Copyright 2015 The Tops'l Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package topsl
import (
"strings"
)
type TextArea struct {
view *CellView
model *linesModel
}
type linesModel struct {
lines []string
width int
height int
x int
y int
hide bool
cursor bool
}
func (m *linesModel) GetCell(x, y int) (rune, Style) {
var ch rune
if x < 0 || y < 0 || y >= len(m.lines) || x >= len(m.lines[y]) {
return ch, StyleDefault
}
return rune(m.lines[y][x]), StyleText
}
func (m *linesModel) GetBounds() (int, int) {
return m.width, m.height
}
func (m *linesModel) limitCursor() {
if m.x < 0 {
m.x = 0
}
if m.y < 0 {
m.y = 0
}
if m.x > m.width-1 {
m.x = m.width - 1
}
if m.y > m.height-1 {
m.y = m.height - 1
}
}
func (m *linesModel) SetCursor(x, y int) {
m.x = x
m.y = y
m.limitCursor()
}
func (m *linesModel) MoveCursor(x, y int) {
m.x += x
m.y += y
m.limitCursor()
}
func (m *linesModel) GetCursor() (int, int, bool, bool) {
return m.x, m.y, m.cursor, !m.hide
}
func (ta *TextArea) SetLines(lines []string) {
m := ta.model
m.width = 0
m.height = len(lines)
m.lines = append([]string{}, lines...)
for _, l := range lines {
if len(l) > m.width {
m.width = len(l)
}
}
ta.view.SetModel(m)
}
func (ta *TextArea) EnableCursor(on bool) {
ta.model.cursor = on
}
func (ta *TextArea) HideCursor(on bool) {
ta.model.hide = on
}
func (ta *TextArea) Draw() {
ta.view.Draw()
}
func (ta *TextArea) HandleEvent(ev Event) bool {
return ta.view.HandleEvent(ev)
}
func (ta *TextArea) Resize() {
ta.view.Resize()
}
func (ta *TextArea) SetView(view View) {
ta.view.SetView(view)
}
func (ta *TextArea) SetContent(text string) {
lines := strings.Split(strings.Trim(text, "\n"), "\n")
ta.SetLines(lines)
}
func NewTextArea() *TextArea {
lm := &linesModel{lines: []string{}, width: 0}
ta := &TextArea{model: lm}
ta.view = NewCellView()
ta.view.SetModel(lm)
return ta
}