| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
} |