okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman Bagabasrefactor(examples): move examples to advanced directoryf72cc87

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