Debian Codegraph @

examples/tv/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "image/color"
6 "log"
7
8 uv "github.com/charmbracelet/ultraviolet"
9 )
10
11 var (
12 gray = color.RGBA{R: 104, G: 104, B: 104, A: 255} // #686868
13 white = color.RGBA{R: 180, G: 180, B: 180, A: 255} // #b4b4b4
14 yellow = color.RGBA{R: 180, G: 180, B: 16, A: 255} // #b4b416
15 cyan = color.RGBA{R: 16, G: 180, B: 180, A: 255} // #10b4b4
16 green = color.RGBA{R: 16, G: 180, B: 16, A: 255} // #10b410
17 magenta = color.RGBA{R: 180, G: 16, B: 180, A: 255} // #b410b4
18 red = color.RGBA{R: 180, G: 16, B: 16, A: 255} // #b41010
19 blue = color.RGBA{R: 16, G: 16, B: 180, A: 255} // #1010b4
20 black = color.RGBA{R: 16, G: 16, B: 16, A: 255} // #101010
21 fullWhite = color.RGBA{R: 235, G: 235, B: 235, A: 255} // #ebebeb
22 fullBlack = color.RGBA{R: 0, G: 0, B: 0, A: 255} // #000000
23 lightBlack = color.RGBA{R: 26, G: 26, B: 26, A: 255} // #1a1a1a
24 purple = color.RGBA{R: 72, G: 16, B: 116, A: 255} // #481074
25 brown = color.RGBA{R: 106, G: 52, B: 16, A: 255} // #6a3410
26 navy = color.RGBA{R: 16, G: 70, B: 106, A: 255} // #10466a
27 )
28
29 func main() {
30 t := uv.DefaultTerminal()
31 if err := t.Start(); err != nil {
32 log.Fatalf("Error starting terminal: %v", err)
33 }
34
35 var err error
36 var area uv.Rectangle
37 area.Max.X, area.Max.Y, err = t.GetSize()
38 if err != nil {
39 log.Fatalf("Error getting terminal size: %v", err)
40 }
41
42 t.EnterAltScreen()
43
44 ctx, cancel := context.WithCancel(context.Background())
45
46 const barCount = 7
47 const botBarCount = 6
48 rowColors := [3][]color.RGBA{
49 0: {
50 0: white,
51 1: yellow,
52 2: cyan,
53 3: green,
54 4: magenta,
55 5: red,
56 6: blue,
57 },
58 1: {
59 0: blue,
60 1: black,
61 2: magenta,
62 3: black,
63 4: cyan,
64 5: black,
65 6: white,
66 },
67 2: {
68 0: navy,
69 1: fullWhite,
70 2: purple,
71 3: black,
72 4: black,
73 5: black,
74 },
75 }
76
77 display := func() {
78 t.Clear()
79 topRow := uv.Rect(0, 0, area.Max.X, (area.Max.Y*66)/100)
80 midRow := uv.Rect(0, topRow.Max.Y, area.Max.X, (area.Max.Y*8)/100)
81 botRow := uv.Rect(0, midRow.Max.Y, area.Max.X, (area.Max.Y*26)/100)
82
83 barWidth := topRow.Max.X / barCount
84 for i, row := range []uv.Rectangle{
85 topRow, midRow,
86 } {
87 for j := 0; j < barCount; j++ {
88 bar := uv.Rect(j*barWidth, row.Min.Y, (j+1)*barWidth, row.Max.Y)
89 cell := uv.EmptyCell
90 cell.Style.Bg = rowColors[i][j%len(rowColors[i])]
91 t.FillArea(&cell, bar)
92 }
93 }
94
95 botBarWidth := botRow.Max.X / botBarCount
96 for i := 0; i < botBarCount; i++ {
97 bar := uv.Rect(i*botBarWidth, botRow.Min.Y, (i+1)*botBarWidth, botRow.Max.Y)
98 cell := uv.EmptyCell
99 cell.Style.Bg = rowColors[2][i%len(rowColors[2])]
100 t.FillArea(&cell, bar)
101 }
102
103 // Special case for the before last bar
104 const specialRow = 5
105 subBarWidth := barWidth / 3
106 for i := 0; i < 3; i++ {
107 bar := uv.Rect(specialRow*barWidth+i*subBarWidth, botRow.Min.Y, subBarWidth, botRow.Max.Y)
108 cell := uv.EmptyCell
109 switch i {
110 case 0:
111 cell.Style.Bg = fullBlack
112 case 1:
113 continue
114 case 2:
115 cell.Style.Bg = lightBlack
116 }
117 t.FillArea(&cell, bar)
118 }
119
120 if err := t.Display(); err != nil {
121 log.Fatalf("Error displaying terminal: %v", err)
122 }
123 }
124
125 LOOP:
126 for {
127 select {
128 case <-ctx.Done():
129 break LOOP
130 case ev := <-t.Events():
131 switch ev := ev.(type) {
132 case uv.WindowSizeEvent:
133 area.Max.X, area.Max.Y = ev.Width, ev.Height
134 t.Resize(area.Max.X, area.Max.Y)
135 t.Erase()
136 display()
137 case uv.KeyPressEvent:
138 switch {
139 case ev.MatchString("q", "ctrl+c"):
140 cancel()
141 }
142 }
143 }
144 }
145
146 if err := t.Shutdown(ctx); err != nil {
147 log.Fatalf("Error shutting down terminal: %v", err)
148 }
149 }