1 package main
2
3 import (
4 "fmt"
5 "os"
6
7 "codeberg.org/tslocum/cbind"
8 "github.com/gdamore/tcell/v3"
9 )
10
11 func main() {
12 tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
13 s, e := tcell.NewScreen()
14 if e != nil {
15 fmt.Fprintf(os.Stderr, "%v\n", e)
16 os.Exit(1)
17 }
18 if e = s.Init(); e != nil {
19 fmt.Fprintf(os.Stderr, "%v\n", e)
20 os.Exit(1)
21 }
22
23 quit := make(chan struct{})
24
25 quitApp := func(ev *tcell.EventKey) *tcell.EventKey {
26 quit <- struct{}{}
27 return nil
28 }
29
30 configuration := cbind.NewConfiguration()
31 configuration.SetKey(tcell.ModNone, tcell.KeyEscape, quitApp)
32 configuration.SetRune(tcell.ModCtrl, 'c', quitApp)
33
34 s.SetStyle(tcell.StyleDefault.
35 Foreground(tcell.ColorWhite).
36 Background(tcell.ColorBlack))
37 s.Clear()
38
39 go func() {
40 for ev := range s.EventQ() {
41 switch ev := ev.(type) {
42 case *tcell.EventResize:
43 s.Sync()
44 case *tcell.EventKey:
45 s.SetStyle(tcell.StyleDefault.
46 Foreground(tcell.ColorWhite).
47 Background(tcell.ColorBlack))
48 s.Clear()
49
50 putln(s, 0, fmt.Sprintf("Event: %d %d %s", ev.Modifiers(), ev.Key(), ev.Str()))
51
52 str, err := cbind.Encode(ev.Modifiers(), ev.Key(), ev.Str())
53 if err != nil {
54 str = fmt.Sprintf("error: %s", err)
55 }
56 putln(s, 2, str)
57
58 mod, key, ch, err := cbind.Decode(str)
59 if err != nil {
60 putln(s, 4, err.Error())
61 } else {
62 putln(s, 4, fmt.Sprintf("Re-encoded as: %d %d %s", mod, key, ch))
63 }
64
65 configuration.Capture(ev)
66
67 s.Sync()
68 }
69 }
70 }()
71 s.Show()
72
73 <-quit
74 s.Fini()
75 }
76
77
78
79
80 func putln(s tcell.Screen, y int, str string) {
81 puts(s, tcell.StyleDefault, 0, y, str)
82 }
83
84 func puts(s tcell.Screen, style tcell.Style, x, y int, str string) {
85 i := 0
86 var deferred []rune
87 dwidth := 0
88 zwj := false
89 for _, r := range str {
90 if r == '\u200d' {
91 if len(deferred) == 0 {
92 deferred = append(deferred, ' ')
93 dwidth = 1
94 }
95 deferred = append(deferred, r)
96 zwj = true
97 continue
98 }
99 if zwj {
100 deferred = append(deferred, r)
101 zwj = false
102 continue
103 }
104 if len(deferred) != 0 {
105 s.SetContent(x+i, y, deferred[0], deferred[1:], style)
106 i += dwidth
107 }
108 deferred = nil
109 dwidth = 1
110 deferred = append(deferred, r)
111 }
112 if len(deferred) != 0 {
113 s.SetContent(x+i, y, deferred[0], deferred[1:], style)
114 i += dwidth
115 }
116 }
117
View as plain text