Debian Codegraph @

_demos/notify.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 "time"
24
25 "github.com/gdamore/tcell/v3"
26 "github.com/gdamore/tcell/v3/color"
27 )
28
29 func displayHelloWorld(s tcell.Screen, secs int) {
30 w, h := s.Size()
31 s.Clear()
32 style := tcell.StyleDefault.Foreground(color.CadetBlue.TrueColor()).Background(color.White)
33 msg := "Notification Demo"
34 s.PutStrStyled((w-len(msg))/2, h/2-1, msg, style)
35 msg = "(Minimize This Window)"
36 s.PutStrStyled((w-len(msg))/2, h/2+1, msg, style)
37 if secs > 0 {
38 msg = fmt.Sprintf("Incoming in %d Seconds", secs)
39 } else {
40 msg = "Notification Sent!"
41 }
42 s.PutStr((w-len(msg))/2, h/2+3, msg)
43 msg = "Press ESC to exit, ENTER to restart."
44 s.PutStr((w-len(msg))/2, h/2+5, msg)
45 s.Show()
46 }
47
48 // This program just prints "Hello, World!". Press ESC to exit.
49 func main() {
50
51 s, e := tcell.NewScreen()
52 if e != nil {
53 fmt.Fprintf(os.Stderr, "%v\n", e)
54 os.Exit(1)
55 }
56 if e := s.Init(); e != nil {
57 fmt.Fprintf(os.Stderr, "%v\n", e)
58 os.Exit(1)
59 }
60
61 defStyle := tcell.StyleDefault.
62 Background(color.Black).
63 Foreground(color.White)
64 s.SetStyle(defStyle)
65
66 when := 10
67 displayHelloWorld(s, when)
68
69 ticker := time.NewTicker(time.Second)
70 var ev tcell.Event
71 for {
72 select {
73 case ev = <-s.EventQ():
74 case <-ticker.C:
75 if when > 0 {
76 when--
77 if when == 0 {
78 s.ShowNotification("Ding Dong!", "The wicked witch is dead.")
79 }
80 }
81 displayHelloWorld(s, when)
82 continue
83 }
84 switch ev := ev.(type) {
85 case *tcell.EventResize:
86 s.Sync()
87 case *tcell.EventKey:
88 switch ev.Key() {
89 case tcell.KeyEnter:
90 when = 10
91 case tcell.KeyEscape:
92 s.Fini()
93 os.Exit(0)
94 }
95 }
96 displayHelloWorld(s, when)
97 }
98 }