Debian Codegraph @

demos/beep/beep.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 // beep makes a beep for every 3 seconds, or when B is pressed, until you press ESC or CTRL-Q
16 package main
17
18 import (
19 "fmt"
20 "os"
21 "time"
22
23 "github.com/gdamore/tcell/v3"
24 )
25
26 func draw(s tcell.Screen, remain int) {
27 style := tcell.StyleDefault
28 s.Clear()
29 s.PutStrStyled(1, 1, fmt.Sprintf("Beep will occur in %d seconds...", remain), style)
30 s.PutStrStyled(1, 3, "Press ESC or CTRL-Q to quit, B to beep now.", style.Italic(true))
31
32 s.Show()
33 }
34
35 func main() {
36 s, e := tcell.NewScreen()
37 if e != nil {
38 fmt.Fprintf(os.Stderr, "%v\n", e)
39 os.Exit(1)
40 }
41 if e = s.Init(); e != nil {
42 fmt.Fprintf(os.Stderr, "%v\n", e)
43 os.Exit(1)
44 }
45 defer s.Fini()
46
47 s.SetStyle(tcell.StyleDefault)
48
49 remain := 3
50 draw(s, remain)
51 for {
52 select {
53 case ev := <-s.EventQ():
54 switch ev := ev.(type) {
55 case *tcell.EventKey:
56 switch ev.Key() {
57 case tcell.KeyEscape, tcell.KeyCtrlQ:
58 return
59 case tcell.KeyCtrlL:
60 draw(s, remain)
61 s.Sync()
62 case tcell.KeyRune:
63 if ev.Str() == "b" || ev.Str() == "B" {
64 remain = 3
65 s.Beep()
66 draw(s, remain)
67 }
68 }
69 case *tcell.EventResize:
70 s.Sync()
71 }
72 case <-time.After(time.Second):
73 // imprecise, but good enough for demo
74 remain--
75 if remain == 0 {
76 remain = 3
77 s.Beep()
78 }
79 draw(s, remain)
80 }
81 }
82 }