-
Notifications
You must be signed in to change notification settings - Fork 34
/
screen_data.go
236 lines (218 loc) · 5.94 KB
/
screen_data.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import "github.com/nsf/termbox-go"
const NAME_PADDING = 2
const TAB_MARGIN = 3
const THICK_LINE = "━"
const RIGHT_JOINT = "┕"
const LEFT_JOINT = "┙"
const DOUBLE_JOINT = "┷"
/*
const THICK_LINE = "═"
const RIGHT_JOINT = "╘"
const LEFT_JOINT = "╛"
const DOUBLE_JOINT = "╧"
*/
type TabListViewPort struct {
width int
offset int
}
type DataScreen struct {
tabs []*DataTab
tab_view_port TabListViewPort
active_tab int
show_tabs bool
}
func (screen *DataScreen) initializeWithFiles(files []FileInfo) {
var tabs []*DataTab
for _, file := range files {
tab := NewDataTab(file)
tabs = append(tabs, &tab)
}
screen.tabs = tabs
screen.show_tabs = true
}
func (screen *DataScreen) receiveEvents(input <-chan termbox.Event, output chan<- interface{}, quit <-chan bool) {
for _, t := range screen.tabs {
go func(tab *DataTab) {
tab.receiveEvents(output)
}(t)
}
for {
do_quit := false
select {
case event := <-input:
idx := screen.handleKeyEvent(event, output)
if idx >= ABOUT_SCREEN_INDEX {
output <- ScreenIndex(idx)
}
case <-quit:
do_quit = true
}
if do_quit {
for _, t := range screen.tabs {
t.quit_channel <- true
}
break
}
}
}
func (screen *DataScreen) handleKeyEvent(event termbox.Event, output chan<- interface{}) int {
active_tab := screen.tabs[screen.active_tab]
if active_tab.field_editor != nil {
return active_tab.handleKeyEvent(event, output)
} else if event.Key == termbox.KeyCtrlLsqBracket { // color palette
return PALETTE_SCREEN_INDEX
} else if event.Ch == '?' { // about
return ABOUT_SCREEN_INDEX
} else if event.Ch == 'S' {
screen.show_tabs = true
return DATA_SCREEN_INDEX
} else if event.Ch == 'W' {
screen.show_tabs = false
return DATA_SCREEN_INDEX
} else if event.Key == termbox.KeyCtrlT {
var new_tabs []*DataTab
for index, old_tab := range screen.tabs {
new_tabs = append(new_tabs, old_tab)
if old_tab == active_tab {
file_info, _ := openFile(old_tab.file_info.filename, false)
tab_copy := NewDataTab(*file_info)
tab_copy.cursor = old_tab.cursor
tab_copy.view_port = old_tab.view_port
tab_copy.cursor.pos = tab_copy.view_port.first_row * tab_copy.view_port.bytes_per_row
tab_copy.cursor.mode = StringMode
new_tabs = append(new_tabs, &tab_copy)
screen.active_tab = index + 1
go func() {
(&tab_copy).receiveEvents(output)
}()
}
}
screen.tabs = new_tabs
screen.show_tabs = true
return DATA_SCREEN_INDEX
} else if event.Key == termbox.KeyCtrlW {
active_tab.quit_channel <- true
if len(screen.tabs) == 1 {
return EXIT_SCREEN_INDEX
}
var new_tabs []*DataTab
for _, old_tab := range screen.tabs {
if old_tab != active_tab {
new_tabs = append(new_tabs, old_tab)
}
}
screen.tabs = new_tabs
if screen.active_tab >= len(new_tabs) {
screen.active_tab = len(new_tabs) - 1
}
return DATA_SCREEN_INDEX
} else if event.Ch == 'D' && screen.show_tabs {
if screen.active_tab < len(screen.tabs)-1 {
screen.active_tab++
}
return DATA_SCREEN_INDEX
} else if event.Ch == 'A' && screen.show_tabs {
if screen.active_tab > 0 {
screen.active_tab--
}
return DATA_SCREEN_INDEX
}
return active_tab.handleKeyEvent(event, output)
}
func (screen *DataScreen) performLayout() {
width, height := termbox.Size()
for _, tab := range screen.tabs {
if screen.show_tabs {
tab.performLayout(width, height-3)
} else {
tab.performLayout(width, height)
}
}
screen.tab_view_port.width = width
tab_pos := TAB_MARGIN
active_tab_start_pos := 0
active_tab_name_len := 0
for index, tab := range screen.tabs {
if index == screen.active_tab {
active_tab_start_pos = tab_pos
for _ = range tab.file_info.baseName() {
active_tab_name_len++
}
}
tab_pos += 2 + 2*NAME_PADDING
for _ = range tab.file_info.baseName() {
tab_pos++
}
}
active_tab_end_pos := active_tab_start_pos + 2 + 2*NAME_PADDING + active_tab_name_len
if tab_pos+TAB_MARGIN < screen.tab_view_port.offset+width {
if tab_pos+TAB_MARGIN > width {
screen.tab_view_port.offset = tab_pos + TAB_MARGIN - width
} else {
screen.tab_view_port.offset = 0
}
}
if screen.tab_view_port.offset > active_tab_start_pos-TAB_MARGIN {
screen.tab_view_port.offset = active_tab_start_pos - TAB_MARGIN
}
if screen.tab_view_port.offset+width < active_tab_end_pos+TAB_MARGIN {
screen.tab_view_port.offset = active_tab_end_pos - width + TAB_MARGIN
}
}
func (screen *DataScreen) drawScreen(style Style) {
width, _ := termbox.Size()
active_tab := screen.tabs[screen.active_tab]
if screen.show_tabs {
fg := style.default_fg
bg := style.default_bg
x_pos := -screen.tab_view_port.offset
for i := 0; i < TAB_MARGIN; i++ {
drawStringAtPoint(THICK_LINE, x_pos, 2, fg, bg)
x_pos++
}
for _, tab := range screen.tabs {
name_fg := fg
if tab != active_tab {
name_fg = style.rune_fg
}
drawStringAtPoint("╭", x_pos, 0, fg, bg)
drawStringAtPoint("│", x_pos, 1, fg, bg)
if tab == active_tab {
drawStringAtPoint(LEFT_JOINT, x_pos, 2, fg, bg)
} else {
drawStringAtPoint(DOUBLE_JOINT, x_pos, 2, fg, bg)
}
x_pos++
nameLength := drawStringAtPoint(tab.file_info.baseName(), x_pos+NAME_PADDING, 1, name_fg, bg)
for i := 0; i < 2*NAME_PADDING+nameLength; i++ {
drawStringAtPoint("─", x_pos, 0, fg, bg)
if tab != active_tab {
drawStringAtPoint(THICK_LINE, x_pos, 2, fg, bg)
}
x_pos++
}
drawStringAtPoint("╮", x_pos, 0, fg, bg)
drawStringAtPoint("│", x_pos, 1, fg, bg)
if tab == active_tab {
drawStringAtPoint(RIGHT_JOINT, x_pos, 2, fg, bg)
} else {
drawStringAtPoint(DOUBLE_JOINT, x_pos, 2, fg, bg)
}
x_pos++
}
if width-x_pos > 22 {
drawStringAtPoint("(?) help", width-20, 1, fg, bg)
}
if width-x_pos > 12 {
drawStringAtPoint("(q)uit", width-10, 1, fg, bg)
}
for x_pos < width {
drawStringAtPoint(THICK_LINE, x_pos, 2, fg, bg)
x_pos++
}
active_tab.drawTab(style, 3)
} else {
active_tab.drawTab(style, 0)
}
}