-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabless.go
357 lines (307 loc) · 7.09 KB
/
tabless.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// tabless - a table viewer like less
// Copyright (C) 2018 George Kettleborough
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"os"
"strconv"
"strings"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/alexflint/go-arg"
)
const (
maxInt = int(^uint(0)>>1)
readAheadMult = 10
)
// isNumeric returns true if the string is considered a float by
// strconv.ParseFloat
func isNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
func max(x int, y int) int {
if x > y {
return x
}
return y
}
type tabless struct {
// the application's screen
screen tcell.Screen
// the main primitive
table *tview.Table
// the input file
file *os.File
// channel for sending rows of cells to be added to table
cellCh chan []string
// channel to request more rows to be added to table
reqCh chan int
// channel to request redraw
drawCh chan bool
// number of fixed rows and columns
rfix, cfix int
// table borders
borders bool
// records whether we have a full screen full yet
screenFull bool
}
func newTabless() *tabless {
return &tabless{}
}
func (t *tabless) read(delimiter string) {
scanner := bufio.NewScanner(t.file)
for scanner.Scan() {
line := scanner.Text()
texts := strings.Split(line, delimiter)
t.cellCh <- texts
}
close(t.cellCh)
}
// add_cells takes a Cell from cellCh and adds it to the table
func (t *tabless) addRows() {
row, reqRows := 0, 0
for {
reqRows = max(<-t.reqCh, reqRows)
for row < reqRows {
select {
case newReq, more := <-t.reqCh:
if !more {
return
}
reqRows = max(newReq, reqRows)
case cells, more := <-t.cellCh:
if !more {
reqRows = row
break
}
for col, text := range cells {
alignment := tview.AlignCenter
expansion := 1
maxWidth := 10
color := tcell.ColorWhite
if col < t.cfix || row < t.rfix {
color = tcell.ColorYellow
} else if isNumeric(text) {
alignment = tview.AlignRight
maxWidth = 20
} else {
alignment = tview.AlignLeft
expansion = 2
}
t.table.SetCell(row, col,
tview.NewTableCell(text).
SetTextColor(color).
SetAlign(alignment).
SetMaxWidth(maxWidth).
SetExpansion(expansion))
}
if !t.screenFull {
select {
case t.drawCh <- true:
break
default:
break
}
}
row++
}
}
// send to unblock event loop for redraw
t.drawCh <- true
t.screenFull = true
}
}
// convert some extra keys into standard tview.Table bindings
func inputCapture (event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
switch key {
case tcell.KeyCtrlN:
return tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone)
case tcell.KeyCtrlP:
return tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone)
case tcell.KeyCtrlV:
return tcell.NewEventKey(tcell.KeyPgDn, 0, tcell.ModNone)
case tcell.KeyRune:
rune := event.Rune()
if (event.Modifiers() & tcell.ModAlt) != 0 {
switch rune {
case 'v':
// M-v
return tcell.NewEventKey(tcell.KeyPgUp, 0, tcell.ModNone)
case '>':
// M->
return tcell.NewEventKey(tcell.KeyEnd, 0, tcell.ModNone)
case '<':
// M-<
return tcell.NewEventKey(tcell.KeyHome, 0, tcell.ModNone)
default:
return event
}
} else {
switch rune {
case 'q':
// app.Stop()
return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
default:
return event
}
}
default:
return event
}
}
func (t *tabless) Draw() {
if t.screen == nil {
return
}
t.table.Draw(t.screen)
t.screen.Show()
}
func (t *tabless) Run() error {
var err error
t.reqCh = make(chan int)
t.cellCh = make(chan []string)
t.drawCh = make(chan bool)
t.screen, err = tcell.NewScreen()
if err != nil {
return err
}
if err = t.screen.Init(); err != nil {
return err
}
// We catch panics to clean up because they mess up the terminal.
defer func() {
if p := recover(); p != nil {
if t.screen != nil {
t.screen.Fini()
}
panic(p)
}
}()
t.table = tview.NewTable().SetBorders(t.borders)
t.table.Select(0, 0).SetFixed(t.rfix, t.cfix)
width, height := t.screen.Size()
t.table.SetRect(0, 0, width, height)
// request a screenful of rows
var rowOffset int
rowOffset = 0
if t.borders {
t.reqCh <- rowOffset + height/2
} else {
t.reqCh <- rowOffset + height
}
waiting, done := false, false
go func() {
for {
if waiting {
done = true
waiting = false
}
<-t.drawCh
t.Draw()
}
}()
// event loop
for {
if t.screen == nil {
break
}
event := t.screen.PollEvent()
if event == nil {
break
}
switch event := event.(type) {
case *tcell.EventKey:
event = inputCapture(event)
if event.Key() == tcell.KeyEnd && !done {
t.reqCh <- maxInt
waiting = true
continue
} else if event.Key() == tcell.KeyCtrlC {
if waiting {
waiting = false
done = true
t.file.Close()
continue
}
t.Stop()
return nil
} else if waiting {
continue
}
// passthrough to the table
handler := t.table.InputHandler()
handler(event, func(p tview.Primitive) {})
case *tcell.EventResize:
width, height = t.screen.Size()
t.table.SetRect(0, 0, width, height)
t.screen.Clear()
t.Draw()
}
rowOffset, _ = t.table.GetOffset()
if t.borders {
t.reqCh <- rowOffset + height/2
} else {
t.reqCh <- rowOffset + height
}
t.drawCh <- true
}
return nil
}
func (t *tabless) Stop() {
if t.screen == nil {
return
}
close(t.reqCh)
t.screen.Fini()
t.screen = nil
}
func main() {
var args struct {
Delimiter string `arg:"-d" help:"Column delimiter character(s)"`
Borders bool `arg:"-b" help:"Display table with borders"`
Input string `arg:"positional" help:"Input file"`
Cfix int `arg:"-c" help:"Number of columns to fix"`
Rfix int `arg:"-r" help:"Number of rows to fix"`
}
args.Delimiter = "\t"
args.Borders = true
args.Cfix = 0
args.Rfix = 1
parser := arg.MustParse(&args)
tabless := newTabless()
var err error
if args.Input != "" && args.Input != "-" {
tabless.file, err = os.Open(args.Input)
if err != nil {
parser.Fail("file not found")
}
} else {
// make sure stdin is being piped
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
parser.WriteUsage(os.Stderr)
os.Exit(1)
} else {
tabless.file = os.Stdin
}
}
tabless.cfix, tabless.rfix = args.Cfix, args.Rfix
tabless.borders = args.Borders
go tabless.read(args.Delimiter)
go tabless.addRows()
if err := tabless.Run(); err != nil {
panic(err)
}
}