Debian Codegraph @

examples/prependline/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "log"
7 "math/rand"
8 "time"
9
10 uv "github.com/charmbracelet/ultraviolet"
11 "github.com/charmbracelet/ultraviolet/screen"
12 "github.com/charmbracelet/x/ansi"
13 )
14
15 func main() {
16 // Create a new default terminal that uses [os.Stdin] and [os.Stdout] for
17 // I/O.
18 t := uv.DefaultTerminal()
19
20 w, _, err := t.GetSize()
21 if err != nil {
22 log.Fatalf("failed to get terminal size: %v", err)
23 }
24
25 frameHeight := 1
26 t.Resize(w, frameHeight)
27
28 // Without starting the program, we cannot display anything on the screen.
29 if err := t.Start(); err != nil {
30 log.Fatalf("failed to start program: %v", err)
31 }
32
33 t.ExitAltScreen()
34
35 t.WriteString(ansi.SetWindowTitle("Hello, World!"))
36
37 var st uv.Style
38 bg := 1
39 st = st.Background(ansi.BasicColor(bg)).Foreground(ansi.Black)
40 display := func() {
41 // This is the program display. It takes a function that receives a
42 // [uv.Frame]. The frame contains the last displayed buffer, cursor
43 // position, and the viewport area the program is operating on.
44 // Under the hood, the program will call [uv.Screen.Display] to display the
45 // frame on the screen and the implementation will depend on the screen
46 // type and how it handles displaying frames.
47 const hw = "Hello, World!"
48 bg := uv.EmptyCell
49 bg.Style = st
50 screen.FillArea(t, &bg, uv.Rect(0, 0, t.Bounds().Dx(), 1))
51 for i, r := range hw {
52 t.SetCell(i, 0, &uv.Cell{
53 Content: string(r),
54 Style: st,
55 Width: 1,
56 })
57 }
58 t.Display()
59 }
60
61 // Now input is separate from the program. Just like a uv.screen displaying
62 // a channel, the channel airs the program to the uv.screen. The program is
63 // not aware of the input source.
64 // Here, our [uv.Screen] is a terminal and is capable of receiving input
65 // events. These events can come from different sources, such as a
66 // keyboard, mouse, window resize, etc. Each input source implements the
67 // [uv.InputReceiver] interface, which is responsible for receiving input
68 // events and sending them to a receiver channel.
69 //
70 // ```go
71 // // InputReceiver is an interface for receiving input events from an input source.
72 // type InputReceiver interface {
73 // // ReceiveEvents read input events and channel them to the given event
74 // // channel. The listener stops when either the context is done or an error
75 // // occurs. Caller is responsible for closing the channels.
76 // ReceiveEvents(ctx context.Context, events chan<- Event) error
77 // }
78 // ```
79
80 // Create a context that we can cancel anytime to stop the event loop.
81 ctx, cancel := context.WithCancel(context.Background())
82 defer cancel()
83
84 display()
85
86 // Events returns an event channel that receives input events from the all
87 // the terminal input sources. It will block until we close the event
88 // channel or cancel the context.
89 LOOP:
90 for {
91 select {
92 case <-ctx.Done():
93 break LOOP
94 case ev := <-t.Events():
95 // Handle events here
96 switch ev := ev.(type) {
97 case uv.KeyPressEvent:
98 switch {
99 case ev.MatchString("q", "ctrl+c"):
100 cancel()
101 }
102
103 st = st.Background(ansi.BasicColor(rand.Intn(16)))
104 case uv.WindowSizeEvent:
105 w = ev.Width
106 t.Resize(w, frameHeight)
107 t.Erase()
108 }
109
110 t.PrependString(fmt.Sprintf("%T %v", ev, ev))
111
112 rd := rand.Intn(8)
113 st.Background(ansi.BasicColor(rd))
114 display()
115 }
116 }
117
118 t.Resize(w, frameHeight+1)
119 display()
120
121 t.WriteString(ansi.SetWindowTitle(""))
122
123 // Gracefully shutdown the program.
124 ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
125 defer cancel()
126
127 if err := t.Shutdown(ctx); err != nil {
128 log.Printf("shutdown: %v", err)
129 }
130 }