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
|
package ui
import (
"sync/atomic"
"time"
"github.com/gdamore/tcell"
)
type Config struct {
NickColWidth int
AutoComplete func(cursorIdx int, text []rune) []Completion
}
type UI struct {
screen tcell.Screen
Events chan tcell.Event
exit atomic.Value // bool
config Config
bs BufferList
e Editor
}
func New(config Config) (ui *UI, err error) {
ui = &UI{
config: config,
}
ui.screen, err = tcell.NewScreen()
if err != nil {
return
}
err = ui.screen.Init()
if err != nil {
return
}
w, h := ui.screen.Size()
ui.screen.Clear()
ui.screen.ShowCursor(0, h-2)
ui.Events = make(chan tcell.Event, 128)
go func() {
for !ui.ShouldExit() {
ui.Events <- ui.screen.PollEvent()
}
}()
ui.exit.Store(false)
ui.bs = NewBufferList(w, h, ui.config.NickColWidth)
ui.e = NewEditor(w, ui.config.AutoComplete)
ui.Resize()
return
}
func (ui *UI) ShouldExit() bool {
return ui.exit.Load().(bool)
}
func (ui *UI) Exit() {
ui.exit.Store(true)
}
func (ui *UI) Close() {
ui.screen.Fini()
}
func (ui *UI) CurrentBuffer() string {
return ui.bs.Current()
}
func (ui *UI) CurrentBufferOldestTime() (t *time.Time) {
return ui.bs.CurrentOldestTime()
}
func (ui *UI) NextBuffer() {
ui.bs.Next()
}
func (ui *UI) PreviousBuffer() {
ui.bs.Previous()
}
func (ui *UI) ScrollUp() {
ui.bs.ScrollUp()
}
func (ui *UI) ScrollDown() {
ui.bs.ScrollDown()
}
func (ui *UI) IsAtTop() bool {
return ui.bs.IsAtTop()
}
func (ui *UI) AddBuffer(title string) {
_ = ui.bs.Add(title)
}
func (ui *UI) RemoveBuffer(title string) {
_ = ui.bs.Remove(title)
}
func (ui *UI) AddLine(buffer string, highlight bool, line Line) {
ui.bs.AddLine(buffer, highlight, line)
}
func (ui *UI) AddLines(buffer string, lines []Line) {
ui.bs.AddLines(buffer, lines)
}
func (ui *UI) SetStatus(status string) {
ui.bs.SetStatus(status)
}
func (ui *UI) InputIsCommand() bool {
return ui.e.IsCommand()
}
func (ui *UI) InputLen() int {
return ui.e.TextLen()
}
func (ui *UI) InputRune(r rune) {
ui.e.PutRune(r)
}
func (ui *UI) InputRight() {
ui.e.Right()
}
func (ui *UI) InputLeft() {
ui.e.Left()
}
func (ui *UI) InputHome() {
ui.e.Home()
}
func (ui *UI) InputEnd() {
ui.e.End()
}
func (ui *UI) InputUp() {
ui.e.Up()
}
func (ui *UI) InputDown() {
ui.e.Down()
}
func (ui *UI) InputBackspace() (ok bool) {
return ui.e.RemRune()
}
func (ui *UI) InputDelete() (ok bool) {
return ui.e.RemRuneForward()
}
func (ui *UI) InputAutoComplete() (ok bool) {
return ui.e.AutoComplete()
}
func (ui *UI) InputEnter() (content string) {
return ui.e.Flush()
}
func (ui *UI) Resize() {
w, h := ui.screen.Size()
ui.e.Resize(w)
ui.bs.Resize(w, h)
}
func (ui *UI) Draw() {
_, h := ui.screen.Size()
ui.e.Draw(ui.screen, h-2)
ui.bs.Draw(ui.screen)
ui.screen.Show()
}
|