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