1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2019 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// unicode just displays a Unicode test on your screen.
// Press ESC to exit the program.
package main
import (
"fmt"
"os"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
runewidth "github.com/mattn/go-runewidth"
)
var row = 0
var style = tcell.StyleDefault
func putln(s tcell.Screen, str string) {
puts(s, style, 1, row, str)
row++
}
func puts(s tcell.Screen, style tcell.Style, x, y int, str string) {
i := 0
var deferred []rune
dwidth := 0
zwj := false
for _, r := range str {
if r == '\u200d' {
if len(deferred) == 0 {
deferred = append(deferred, ' ')
dwidth = 1
}
deferred = append(deferred, r)
zwj = true
continue
}
if zwj {
deferred = append(deferred, r)
zwj = false
continue
}
switch runewidth.RuneWidth(r) {
case 0:
if len(deferred) == 0 {
deferred = append(deferred, ' ')
dwidth = 1
}
case 1:
if len(deferred) != 0 {
s.SetContent(x+i, y, deferred[0], deferred[1:], style)
i += dwidth
}
deferred = nil
dwidth = 1
case 2:
if len(deferred) != 0 {
s.SetContent(x+i, y, deferred[0], deferred[1:], style)
i += dwidth
}
deferred = nil
dwidth = 2
}
deferred = append(deferred, r)
}
if len(deferred) != 0 {
s.SetContent(x+i, y, deferred[0], deferred[1:], style)
i += dwidth
}
}
func main() {
s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
encoding.Register()
if e = s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
plain := tcell.StyleDefault
bold := style.Bold(true)
s.SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorBlack).
Background(tcell.ColorWhite))
s.Clear()
quit := make(chan struct{})
style = bold
putln(s, "Press ESC to Exit")
putln(s, "Character set: "+s.CharacterSet())
style = plain
putln(s, "English: October")
putln(s, "Icelandic: október")
putln(s, "Arabic: أكتوبر")
putln(s, "Russian: октября")
putln(s, "Greek: Οκτωβρίου")
putln(s, "Chinese: 十月 (note, two double wide characters)")
putln(s, "Combining: A\u030a (should look like Angstrom)")
putln(s, "Emoticon: \U0001f618 (blowing a kiss)")
putln(s, "Airplane: \u2708 (fly away)")
putln(s, "Command: \u2318 (mac clover key)")
putln(s, "Enclose: !\u20e3 (should be enclosed exclamation)")
putln(s, "ZWJ: \U0001f9db\u200d\u2640 (female vampire)")
putln(s, "ZWJ: \U0001f9db\u200d\u2642 (male vampire)")
putln(s, "Family: \U0001f469\u200d\U0001f467\u200d\U0001f467 (woman girl girl)\n")
putln(s, "Region: \U0001f1fa\U0001f1f8 (USA! USA!)\n")
putln(s, "")
putln(s, "Box:")
putln(s, string([]rune{
tcell.RuneULCorner,
tcell.RuneHLine,
tcell.RuneTTee,
tcell.RuneHLine,
tcell.RuneURCorner,
}))
putln(s, string([]rune{
tcell.RuneVLine,
tcell.RuneBullet,
tcell.RuneVLine,
tcell.RuneLantern,
tcell.RuneVLine,
})+" (bullet, lantern/section)")
putln(s, string([]rune{
tcell.RuneLTee,
tcell.RuneHLine,
tcell.RunePlus,
tcell.RuneHLine,
tcell.RuneRTee,
}))
putln(s, string([]rune{
tcell.RuneVLine,
tcell.RuneDiamond,
tcell.RuneVLine,
tcell.RuneUArrow,
tcell.RuneVLine,
})+" (diamond, up arrow)")
putln(s, string([]rune{
tcell.RuneLLCorner,
tcell.RuneHLine,
tcell.RuneBTee,
tcell.RuneHLine,
tcell.RuneLRCorner,
}))
s.Show()
go func() {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape, tcell.KeyEnter:
close(quit)
return
case tcell.KeyCtrlL:
s.Sync()
}
case *tcell.EventResize:
s.Sync()
}
}
}()
<-quit
s.Fini()
}
|