Debian Codegraph @

demos/hello/hello.go

raw symbols SCIP
1 // Copyright 2025 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "fmt"
19 "os"
20
21 "github.com/gdamore/tcell/v3"
22 "github.com/gdamore/tcell/v3/color"
23 )
24
25 func displayHelloWorld(s tcell.Screen) {
26 w, h := s.Size()
27 s.Clear()
28 s.SetTitle("Hello World")
29 x := w/2 - 7
30 y := h/2 - 1
31 style := tcell.StyleDefault.Foreground(color.CadetBlue.TrueColor()).Background(color.White)
32 s.PutStrStyled(x, y, "Hello, World!", style)
33 x = w/2 - 9
34 y += 2
35 s.PutStr(x, y, "Press ")
36 x += len("Press ")
37
38 s.PutStrStyled(x, y, "ESC", tcell.StyleDefault.Bold(true))
39 x += len("ESC")
40 s.PutStr(x, y, " to exit.")
41 s.Show()
42 }
43
44 // This program just prints "Hello, World!". Press ESC to exit.
45 func main() {
46
47 s, e := tcell.NewScreen()
48 if e != nil {
49 fmt.Fprintf(os.Stderr, "%v\n", e)
50 os.Exit(1)
51 }
52 if e := s.Init(); e != nil {
53 fmt.Fprintf(os.Stderr, "%v\n", e)
54 os.Exit(1)
55 }
56
57 defStyle := tcell.StyleDefault.
58 Background(color.Black).
59 Foreground(color.White)
60 s.SetStyle(defStyle)
61
62 displayHelloWorld(s)
63
64 for {
65 ev := <-s.EventQ()
66 switch ev := ev.(type) {
67 case *tcell.EventResize:
68 s.Sync()
69 displayHelloWorld(s)
70 case *tcell.EventKey:
71 if ev.Key() == tcell.KeyEscape {
72 s.Fini()
73 return
74 }
75 }
76 }
77 }