-
Notifications
You must be signed in to change notification settings - Fork 14
/
tui.go
909 lines (742 loc) · 15.5 KB
/
tui.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
* ELF tree - Tree viewer for ELF library dependency
*
* Copyright (C) 2017 Namhyung Kim <[email protected]>
*
* Released under MIT license.
*/
package main
import (
"fmt"
tui "github.com/airking05/termui" // for termui bug #177
)
type TreeItem struct {
node interface{}
parent *TreeItem
prev *TreeItem // pointer to siblings
next *TreeItem
child *TreeItem // pointer to first child
folded bool
total int // number of (shown) children (not count itself)
}
type TreeView struct {
tui.Block // embedded
Root *TreeItem
Top *TreeItem
Curr *TreeItem
ItemFgColor tui.Attribute
ItemBgColor tui.Attribute
FocusFgColor tui.Attribute
FocusBgColor tui.Attribute
idx int // current cursor position
off int // first entry displayed
pos int // position of x-axis
rows int
cols int
}
type FileInfo struct {
Root *TreeItem
Top *TreeItem
Curr *TreeItem
idx int
off int
pos int
}
const (
MODE_FILE = iota
MODE_SYMBOL
MODE_DYNAMIC
MODE_SECTION
)
var (
mode int
finfo map[string]*FileInfo
yinfo map[string]*FileInfo
dinfo map[string]*FileInfo
sinfo map[string]*FileInfo
focus *TreeView
)
type StatusLine struct {
tui.Block // embedded
tv *TreeView
}
func NewTreeView() *TreeView {
tv := &TreeView{Block: *tui.NewBlock()}
tv.ItemFgColor = tui.ThemeAttr("list.item.fg")
tv.ItemBgColor = tui.ThemeAttr("list.item.bg")
tv.idx = 0
tv.off = 0
return tv
}
func NewStatusLine(tv *TreeView) *StatusLine {
sl := &StatusLine{Block: *tui.NewBlock()}
sl.Block.Border = false
sl.tv = tv
return sl
}
func (ti *TreeItem) prevItem() *TreeItem {
if ti.prev == nil {
return ti.parent
}
ti = ti.prev
// find last child of previous sibling
for ti != nil {
if ti.child == nil || ti.folded {
return ti
}
ti = ti.child
for ti.next != nil {
ti = ti.next
}
}
return nil
}
func (ti *TreeItem) nextItem() *TreeItem {
if ti.child == nil || ti.folded {
for ti != nil {
if ti.next != nil {
return ti.next
}
ti = ti.parent
}
return nil
}
return ti.child
}
func (ti *TreeItem) expand() {
if !ti.folded || ti.child == nil {
return
}
for c := ti.child; c != nil; c = c.next {
ti.total += c.total + 1
}
for p := ti.parent; p != nil; p = p.parent {
p.total += ti.total
}
ti.folded = false
}
func (ti *TreeItem) fold() {
if ti.folded || ti.child == nil {
return
}
for p := ti.parent; p != nil; p = p.parent {
p.total -= ti.total
}
ti.total = 0
ti.folded = true
}
func (ti *TreeItem) toggle() {
if ti.folded {
ti.expand()
} else {
ti.fold()
}
}
func (tv *TreeView) drawDepsNode(buf tui.Buffer, dn *DepsNode, i, printed int, folded bool) {
fg := tv.ItemFgColor
bg := tv.ItemBgColor
if i == tv.idx {
if focus == tv {
fg = tv.FocusFgColor
bg = tv.FocusBgColor
} else {
fg = tv.ItemBgColor
bg = tv.ItemFgColor
}
}
indent := 3 * dn.depth
text_width := tv.cols - 2 - indent
if text_width < 0 {
text_width = 0
}
cs := tui.DefaultTxBuilder.Build(dn.name, fg, bg)
cs = tui.DTrimTxCls(cs, text_width)
j := 0
if i == tv.idx {
// draw current line cursor from the beginning
for j < indent {
if j+1 > tv.pos {
buf.Set(j+1-tv.pos, printed+1, tui.Cell{' ', fg, bg})
}
j++
}
} else {
j = indent
}
if j+1 > tv.pos {
if folded {
buf.Set(j+1-tv.pos, printed+1, tui.Cell{'+', fg, bg})
} else {
buf.Set(j+1-tv.pos, printed+1, tui.Cell{'-', fg, bg})
}
}
if j+2 > tv.pos {
buf.Set(j+2-tv.pos, printed+1, tui.Cell{' ', fg, bg})
}
j += 2
for _, vv := range cs {
w := vv.Width()
if j+1 > tv.pos {
buf.Set(j+1-tv.pos, printed+1, vv)
}
j += w
}
if i != tv.idx {
return
}
// draw current line cursor to the end
for j < tv.cols+tv.pos {
if j+1 > tv.pos {
buf.Set(j+1-tv.pos, printed+1, tui.Cell{' ', fg, bg})
}
j++
}
}
func (tv *TreeView) drawStrNode(buf tui.Buffer, s string, i, printed int, sign rune) {
fg := tv.ItemFgColor
bg := tv.ItemBgColor
if i == tv.idx {
if focus == tv {
fg = tv.FocusFgColor
bg = tv.FocusBgColor
}
}
cs := tui.DefaultTxBuilder.Build(s, fg, bg)
cs = tui.DTrimTxCls(cs, tv.cols-2)
j := 0
if j+1 > tv.pos {
buf.Set(tv.X+j+1-tv.pos, printed+1, tui.Cell{sign, fg, bg})
}
if j+2 > tv.pos {
buf.Set(tv.X+j+2-tv.pos, printed+1, tui.Cell{' ', fg, bg})
}
j += 2
for _, vv := range cs {
w := vv.Width()
if j+1 > tv.pos {
buf.Set(tv.X+j+1-tv.pos, printed+1, vv)
}
j += w
}
if i != tv.idx {
return
}
// draw current line cursor to the end
for j < tv.cols+tv.pos {
if j+1 > tv.pos {
buf.Set(tv.X+j+1-tv.pos, printed+1, tui.Cell{' ', fg, bg})
}
j++
}
}
// Buffer implements Bufferer interface.
func (tv *TreeView) Buffer() tui.Buffer {
buf := tv.Block.Buffer()
i := 0
printed := 0
var ti *TreeItem
for ti = tv.Root; ti != nil; ti = ti.nextItem() {
if i < tv.off {
i++
continue
}
if printed == tv.rows {
break
}
switch node := ti.node.(type) {
case *DepsNode:
tv.drawDepsNode(buf, node, i, printed, ti.folded)
printed++
i++
case string:
sign := ' '
if ti.folded {
sign = '+'
} else if ti.child != nil {
sign = '-'
}
tv.drawStrNode(buf, node, i, printed, sign)
printed++
i++
default:
}
}
return buf
}
func (tv *TreeView) Down() {
if tv.idx < tv.Root.total {
tv.idx++
tv.Curr = tv.Curr.nextItem()
}
if tv.idx-tv.off >= tv.rows {
tv.off++
tv.Top = tv.Top.nextItem()
}
}
func (tv *TreeView) Up() {
if tv.idx > 0 {
tv.idx--
tv.Curr = tv.Curr.prevItem()
}
if tv.idx < tv.off {
tv.off = tv.idx
tv.Top = tv.Curr
}
}
func (tv *TreeView) PageDown() {
idx := tv.idx
bottom := tv.off + tv.rows - 1
if bottom > tv.Root.total {
bottom = tv.Root.total
}
// At first, move to the bottom of current page
if tv.idx != bottom {
tv.idx = bottom
for idx != bottom {
tv.Curr = tv.Curr.nextItem()
idx++
}
return
}
tv.idx += tv.rows
if tv.idx > tv.Root.total {
tv.idx = tv.Root.total
}
for idx != tv.idx {
tv.Curr = tv.Curr.nextItem()
idx++
}
off := tv.off
if tv.idx-tv.off >= tv.rows {
tv.off = tv.idx - tv.rows + 1
for off != tv.off {
tv.Top = tv.Top.nextItem()
off++
}
}
}
func (tv *TreeView) PageUp() {
idx := tv.idx
// At first, move to the top of current page
if tv.idx != tv.off {
tv.idx = tv.off
tv.Curr = tv.Top
return
}
tv.idx -= tv.rows
if tv.idx < 0 {
tv.idx = 0
}
tv.off = tv.idx
for idx != tv.idx {
tv.Curr = tv.Curr.prevItem()
idx--
}
tv.Top = tv.Curr
}
func (tv *TreeView) Home() {
tv.idx = 0
tv.off = 0
tv.Curr = tv.Root
tv.Top = tv.Root
}
func (tv *TreeView) End() {
tv.idx = tv.Root.total
tv.off = tv.idx - tv.rows + 1
if tv.off < 0 {
tv.off = 0
}
for next := tv.Curr; next != nil; next = next.nextItem() {
tv.Curr = next
}
off := tv.idx
top := tv.Curr
for off != tv.off {
top = top.prevItem()
off--
}
tv.Top = top
}
func (tv *TreeView) Left(i int) {
tv.pos -= i
if tv.pos < 0 {
tv.pos = 0
}
}
func (tv *TreeView) Right(i int) {
tv.pos += i
}
func (tv *TreeView) Toggle() {
tv.Curr.toggle()
}
// Buffer implements Bufferer interface.
func (sl *StatusLine) Buffer() tui.Buffer {
buf := sl.Block.Buffer()
var line string
curr := sl.tv.Curr
if curr != nil {
node := curr.node.(*DepsNode)
line = node.name
n := node.parent
for n != nil {
line = n.name + " > " + line
n = n.parent
}
} else {
line = "ELF tree"
}
fg := tui.ColorBlack
bg := tui.ColorWhite
cs := tui.DefaultTxBuilder.Build(line, fg, bg)
cs = tui.DTrimTxCls(cs, sl.Width-3)
buf.Set(0, sl.Y, tui.Cell{' ', fg, bg})
buf.Set(1, sl.Y, tui.Cell{' ', fg, bg})
j := 2
for _, vv := range cs {
w := vv.Width()
buf.Set(j, sl.Y, vv)
j += w
}
// draw status line to the end
for j < sl.Width {
buf.Set(j, sl.Y, tui.Cell{' ', fg, bg})
j++
}
return buf
}
func makeDepsItems(dep *DepsNode, parent *TreeItem) *TreeItem {
item := &TreeItem{node: dep, parent: parent, folded: false, total: len(dep.child)}
var prev *TreeItem
for _, v := range dep.child {
c := makeDepsItems(v, item)
if item.child == nil {
item.child = c
}
if prev != nil {
prev.next = c
c.prev = prev
}
prev = c
item.total += c.total
}
return item
}
func AddSubTree(name string, items []string, parent *TreeItem) {
var t, p *TreeItem
t = &TreeItem{node: name, parent: parent}
if parent.child == nil {
parent.child = t
} else {
p = parent.child
for p.next != nil {
p = p.next
}
p.next = t
t.prev = p
}
p = nil
parent = t
for _, item := range items {
t = &TreeItem{node: item, parent: parent}
if p == nil {
parent.child = t
} else {
p.next = t
t.prev = p
}
p = t
}
parent.total = len(items)
parent.parent.total += len(items) + 1
}
func makeFileInfo(name string, info *DepsInfo) *FileInfo {
root := &TreeItem{node: name}
// general file info
AddSubTree("", nil, root)
AddSubTree("File Info", []string{" Path: " + info.path,
" Type: " + info.kind.String() + ", " + info.mach.String(),
" Data: " + info.bits.String() + ", " + info.endian.String()},
root)
// program headers
var phdr []string
for _, v := range info.prog {
phdr = append(phdr, " "+progHdrString(v))
}
AddSubTree("", nil, root)
AddSubTree(fmt.Sprintf("%-16s %s %18s %10s %8s",
"Program Info", "Flags", "Vaddr", "Size", "Align"),
phdr, root)
// dependent libraries
var libs []string
for _, v := range info.libs {
libs = append(libs, " "+v)
}
AddSubTree("", nil, root)
AddSubTree("Dependencies", libs, root)
return &FileInfo{Root: root, Top: root, Curr: root}
}
func makeSymbolInfo(name string, info *DepsInfo) *FileInfo {
root := &TreeItem{node: name}
// dynamic symbols
AddSubTree("", nil, root)
var dsym []string
for _, v := range info.dsym {
dsym = append(dsym, makeSymbolString(v))
}
AddSubTree("Dynamic Symbols", dsym, root)
// normal symbols
AddSubTree("", nil, root)
var nsym []string
for _, v := range info.syms {
nsym = append(nsym, makeSymbolString(v))
}
AddSubTree("Symbols", nsym, root)
return &FileInfo{Root: root, Top: root, Curr: root}
}
func makeDynamicInfo(name string, info *DepsInfo) *FileInfo {
root := &TreeItem{node: name}
// dynamic info
AddSubTree("", nil, root)
AddSubTree("Dynamic Info", makeDynamicStrings(info), root)
return &FileInfo{Root: root, Top: root, Curr: root}
}
func makeSectionInfo(name string, info *DepsInfo) *FileInfo {
root := &TreeItem{node: name}
// section headers
AddSubTree("", nil, root)
var sect []string
sect = append(sect, fmt.Sprintf(" %4s %-24s %-12s %8s %8s %4s",
"Idx", "Name", "Type", "Offset", "Size", "Flag"))
for i, v := range info.sect {
sect = append(sect, makeSectionString(i, v))
}
AddSubTree("Section Info", sect, root)
return &FileInfo{Root: root, Top: root, Curr: root}
}
func saveInfoView(tv, iv *TreeView) {
if focus != tv {
return
}
curr := tv.Curr
node := curr.node.(*DepsNode)
var info *FileInfo
if mode == MODE_FILE {
info = finfo[node.name]
} else if mode == MODE_SYMBOL {
info = yinfo[node.name]
} else if mode == MODE_DYNAMIC {
info = dinfo[node.name]
} else if mode == MODE_SECTION {
info = sinfo[node.name]
}
info.Root = iv.Root
info.Top = iv.Top
info.Curr = iv.Curr
info.off = iv.off
info.idx = iv.idx
info.pos = iv.pos
}
func restoreInfoView(tv, iv *TreeView) {
if focus != tv {
return
}
curr := tv.Curr
node := curr.node.(*DepsNode)
var info *FileInfo
if mode == MODE_FILE {
info = finfo[node.name]
} else if mode == MODE_SYMBOL {
info = yinfo[node.name]
} else if mode == MODE_DYNAMIC {
info = dinfo[node.name]
} else if mode == MODE_SECTION {
info = sinfo[node.name]
}
iv.Root = info.Root
iv.Top = info.Top
iv.Curr = info.Curr
iv.off = info.off
iv.idx = info.idx
iv.pos = info.pos
}
func resize(tv, iv *TreeView, sl *StatusLine) {
tv.Height = tui.TermHeight() - 1
tv.Width = tui.TermWidth() * 3 / 5
tv.rows = tv.Height - 2 // exclude border at top and bottom
tv.cols = tv.Width - 2 // exclude border at left and right
iv.Height = tui.TermHeight() - 1
iv.Width = tui.TermWidth() - tv.Width
iv.X = tv.Width
iv.rows = iv.Height - 2
iv.cols = iv.Width - 2
sl.Height = 1
sl.Width = tui.TermWidth()
sl.Y = tui.TermHeight() - 1
}
func ShowWithTUI(dep *DepsNode) {
if err := tui.Init(); err != nil {
panic(err)
}
defer tui.Close()
root := makeDepsItems(dep, nil)
tv := NewTreeView()
tv.Root = root
tv.Curr = root
tv.Top = root
tv.FocusFgColor = tui.ColorYellow
tv.FocusBgColor = tui.ColorBlue
tv.BorderLabel = "ELF Tree"
iv := NewTreeView()
iv.FocusFgColor = tui.ColorYellow
iv.FocusBgColor = tui.ColorBlue
sl := NewStatusLine(tv)
finfo = make(map[string]*FileInfo)
yinfo = make(map[string]*FileInfo)
dinfo = make(map[string]*FileInfo)
sinfo = make(map[string]*FileInfo)
for k, v := range deps {
finfo[k] = makeFileInfo(k, &v)
yinfo[k] = makeSymbolInfo(k, &v)
dinfo[k] = makeDynamicInfo(k, &v)
sinfo[k] = makeSectionInfo(k, &v)
}
mode = MODE_FILE
focus = tv
restoreInfoView(tv, iv)
resize(tv, iv, sl)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
// handle key pressing
tui.Handle("/sys/kbd/q", func(tui.Event) {
// press q to quit
tui.StopLoop()
})
tui.Handle("/sys/kbd/C-c", func(tui.Event) {
// press Ctrl-C to quit
tui.StopLoop()
})
tui.Handle("/sys/kbd/f", func(tui.Event) {
if focus == tv {
mode = MODE_FILE
restoreInfoView(tv, iv)
}
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/y", func(tui.Event) {
if focus == tv {
mode = MODE_SYMBOL
restoreInfoView(tv, iv)
}
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/d", func(tui.Event) {
if focus == tv {
mode = MODE_DYNAMIC
restoreInfoView(tv, iv)
}
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/s", func(tui.Event) {
if focus == tv {
mode = MODE_SECTION
restoreInfoView(tv, iv)
}
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<down>", func(tui.Event) {
saveInfoView(tv, iv)
focus.Down()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<up>", func(tui.Event) {
saveInfoView(tv, iv)
focus.Up()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<left>", func(tui.Event) {
focus.Left(1)
tui.Render(focus)
// no need to redraw sl
})
tui.Handle("/sys/kbd/<right>", func(tui.Event) {
focus.Right(1)
tui.Render(focus)
// no need to redraw sl
})
tui.Handle("/sys/kbd/<", func(tui.Event) {
focus.Left(3)
tui.Render(focus)
// no need to redraw sl
})
tui.Handle("/sys/kbd/>", func(tui.Event) {
focus.Right(3)
tui.Render(focus)
// no need to redraw sl
})
tui.Handle("/sys/kbd/<next>", func(tui.Event) {
saveInfoView(tv, iv)
focus.PageDown()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<previous>", func(tui.Event) {
saveInfoView(tv, iv)
focus.PageUp()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<home>", func(tui.Event) {
saveInfoView(tv, iv)
focus.Home()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<end>", func(tui.Event) {
saveInfoView(tv, iv)
focus.End()
restoreInfoView(tv, iv)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<enter>", func(tui.Event) {
focus.Toggle()
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/kbd/<tab>", func(tui.Event) {
if focus == tv {
focus = iv
} else {
focus = tv
}
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Handle("/sys/wnd/resize", func(tui.Event) {
resize(tv, iv, sl)
tui.Render(tv)
tui.Render(iv)
tui.Render(sl)
})
tui.Loop()
}