Debian Codegraph @

_demos/setsize.go

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