Debian Codegraph @

examples/space/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "image/color"
7 "log"
8 "math/rand"
9 "time"
10
11 uv "github.com/charmbracelet/ultraviolet"
12 "github.com/charmbracelet/ultraviolet/screen"
13 )
14
15 func setupColors(width, height int) [][]color.Color {
16 height = height * 2 // double height for half blocks
17 colors := make([][]color.Color, height)
18
19 for y := 0; y < height; y++ {
20 colors[y] = make([]color.Color, width)
21 randomnessFactor := float64(height-y) / float64(height)
22
23 for x := 0; x < width; x++ {
24 baseValue := randomnessFactor * (float64(height-y) / float64(height))
25 randomOffset := (rand.Float64() * 0.2) - 0.1
26 value := clamp(baseValue+randomOffset, 0, 1)
27
28 // Convert value to grayscale color (0-255)
29 gray := uint8(value * 255)
30 colors[y][x] = color.RGBA{
31 R: gray, G: gray, B: gray, A: 255,
32 }
33 }
34 }
35 return colors
36 }
37
38 func clamp(value, min, max float64) float64 {
39 if value < min {
40 return min
41 }
42 if value > max {
43 return max
44 }
45 return value
46 }
47
48 type tickEvent struct{}
49
50 func main() {
51 t := uv.DefaultTerminal()
52
53 if err := t.Start(); err != nil {
54 log.Fatalf("failed to start terminal: %v", err)
55 }
56
57 var err error
58 var area uv.Rectangle
59 area.Max.X, area.Max.Y, err = t.GetSize()
60 if err != nil {
61 log.Fatalf("failed to get terminal size: %v", err)
62 }
63
64 ctx, cancel := context.WithCancel(context.Background())
65 defer cancel()
66
67 var frameCount int
68 var elapsed time.Duration
69 now := time.Now()
70 fps := 60.0
71 fpsFrameCount := 0
72
73 var lastWidth, lastHeight int
74 colors := setupColors(area.Dx(), area.Dy())
75
76 go t.SendEvent(tickEvent{})
77
78 LOOP:
79 for {
80 select {
81 case <-ctx.Done():
82 break LOOP
83 case ev := <-t.Events():
84 switch ev := ev.(type) {
85 case uv.KeyPressEvent:
86 switch ev.String() {
87 case "q", "ctrl+c":
88 cancel()
89 break LOOP
90 }
91
92 case uv.WindowSizeEvent:
93 area.Max.X = ev.Width
94 area.Max.Y = ev.Height
95 if width, height := area.Dx(), area.Dy(); width != lastWidth || height != lastHeight {
96 colors = setupColors(width, height)
97 lastWidth = width
98 lastHeight = height
99 t.Resize(area.Dx(), area.Dy())
100 t.Erase()
101 }
102 case tickEvent:
103 if len(colors) == 0 {
104 continue
105 }
106
107 frameCount++
108 fpsFrameCount++
109 screen.Clear(t)
110
111 // Title
112 uv.NewStyledString(fmt.Sprintf("\x1b[1mSpace / FPS: %.1f\x1b[m", fps)).
113 Draw(t, uv.Rect(0, 0, area.Dx(), 1))
114
115 // Color display
116 width, height := area.Dx(), area.Dy() // Reserve one line for the title
117 for y := 1; y < height; y++ {
118 for x := 0; x < width; x++ {
119 xi := (x + frameCount) % width
120 fg := colors[y*2][xi]
121 bg := colors[y*2+1][xi]
122 st := uv.Style{
123 Fg: fg,
124 Bg: bg,
125 }
126 t.SetCell(x, y, &uv.Cell{
127 Content: "▀",
128 Style: st,
129 Width: 1,
130 })
131 }
132 }
133
134 t.Display()
135 elapsed = time.Since(now)
136 if elapsed > time.Second && fpsFrameCount > 2 {
137 fps = float64(fpsFrameCount) / elapsed.Seconds()
138 now = time.Now()
139 fpsFrameCount = 0
140 }
141
142 go t.SendEvent(tickEvent{})
143 }
144 }
145 }
146
147 if err := t.Shutdown(context.Background()); err != nil {
148 log.Printf("failed to shutdown terminal: %v", err)
149 }
150 }