1package main
2
3import (
4	"github.com/xyproto/vt100"
5	"os"
6	"os/signal"
7	"sync"
8	"syscall"
9	"time"
10)
11
12func draw(c *vt100.Canvas) {
13	c.FillBackground(vt100.BackgroundBlue)
14
15	box := NewBox()
16
17	frame := box.GetFrame()
18	frame.W = int(c.W())
19	frame.H = int(c.H())
20	box.SetFrame(frame)
21
22	inner := box.GetInner()
23	inner.X = 0
24	inner.Y = 3 // This space is used by the title
25	inner.W = frame.W - inner.X
26	inner.H = frame.H - inner.Y
27	box.SetInner(inner)
28
29	infoBox := NewBox()
30	infoBox.SetThirdSize(box)
31	infoBox.FillWithPercentageMargins(box, 0.07, 0.1)
32
33	t := NewTheme()
34	infoBox.SetInner(t.DrawBox(c, infoBox, true))
35
36	listBox := NewBox()
37	choices := []string{"first", "second", "third"}
38	listBox.SetInner(&Rect{0, 0, 6 + 2, len(choices)})
39	listBox.Center(infoBox)
40	t.DrawList(c, listBox, choices, 1)
41
42	buttonBox1 := NewBox()
43	buttonBox1.SetInner(&Rect{0, 0, 6 + 2, 1})
44	buttonBox1.BottomCenterLeft(infoBox)
45	t.DrawButton(c, buttonBox1, "OK", true)
46
47	buttonBox2 := NewBox()
48	buttonBox2.SetInner(&Rect{0, 0, 10 + 2, 1})
49	buttonBox2.BottomCenterRight(infoBox)
50	t.DrawButton(c, buttonBox2, "Cancel", false)
51
52	c.Draw()
53}
54
55func main() {
56	var (
57		c = vt100.NewCanvas()
58
59		// Channel for terminal signals
60		sigChan = make(chan os.Signal, 1)
61
62		// Mutex used when the terminal is resized
63		resizeMut = &sync.RWMutex{}
64	)
65
66	signal.Notify(sigChan, syscall.SIGWINCH)
67	go func() {
68		for range sigChan {
69			resizeMut.Lock()
70			vt100.Close()
71
72			// Sleeping here is a bit dirty, but it works
73			time.Sleep(100 * time.Millisecond)
74			//vt100.Reset()
75
76			vt100.Init()
77			c = vt100.NewCanvas()
78			draw(c)
79
80			resizeMut.Unlock()
81		}
82	}()
83
84	resizeMut.Lock()
85
86	vt100.Init()
87	defer vt100.Close()
88
89	c.Clear()
90	draw(c)
91
92	resizeMut.Unlock()
93
94	vt100.WaitForKey()
95}
96