...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
20 "fmt"
21 "os"
22
23 "github.com/gdamore/tcell/v2"
24 "github.com/gdamore/tcell/v2/views"
25 )
26
27 type boxL struct {
28 views.BoxLayout
29 }
30
31 var app = &views.Application{}
32 var box = &boxL{}
33
34 func (m *boxL) HandleEvent(ev tcell.Event) bool {
35 switch ev := ev.(type) {
36 case *tcell.EventKey:
37 if ev.Key() == tcell.KeyEscape {
38 app.Quit()
39 return true
40 }
41 }
42 return m.BoxLayout.HandleEvent(ev)
43 }
44
45 func main() {
46
47 title := &views.TextBar{}
48 title.SetStyle(tcell.StyleDefault.
49 Background(tcell.ColorYellow).
50 Foreground(tcell.ColorBlack))
51 title.SetCenter("Horizontal Boxes", tcell.StyleDefault)
52 title.SetLeft("ESC to exit", tcell.StyleDefault.
53 Background(tcell.ColorBlue).
54 Foreground(tcell.ColorWhite))
55 title.SetRight("==>X", tcell.StyleDefault)
56
57 inner := views.NewBoxLayout(views.Horizontal)
58
59 l := views.NewText()
60 m := views.NewText()
61 r := views.NewText()
62
63 l.SetText("Left (0.0)")
64 m.SetText("Middle (0.7)")
65 r.SetText("Right (0.3)")
66 l.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
67 Background(tcell.ColorRed))
68 m.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
69 Background(tcell.ColorLime))
70 r.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).
71 Background(tcell.ColorBlue))
72 l.SetAlignment(views.AlignBegin)
73 m.SetAlignment(views.AlignMiddle)
74 r.SetAlignment(views.AlignEnd)
75
76 inner.AddWidget(l, 0)
77 inner.AddWidget(m, 0.7)
78 inner.AddWidget(r, 0.3)
79
80 box.SetOrientation(views.Vertical)
81 box.AddWidget(title, 0)
82 box.AddWidget(inner, 1)
83 app.SetRootWidget(box)
84 if e := app.Run(); e != nil {
85 fmt.Fprintln(os.Stderr, e.Error())
86 os.Exit(1)
87 }
88 }
89
View as plain text