tcell/_demos/unicode.go

173 lines
3.6 KiB
Go

// Copyright 2015 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"
"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
for _, r := range str {
switch runewidth.RuneWidth(r) {
case 0:
if len(deferred) == 0 {
deferred = append(deferred, ' ')
dwidth = 1
}
case 1:
if len(deferred) != 0 {
s.SetCell(x+i, y, style, deferred...)
i += dwidth
}
deferred = deferred[0:0]
dwidth = 1
case 2:
if len(deferred) != 0 {
s.SetCell(x+i, y, style, deferred...)
i += dwidth
}
deferred = deferred[0:0]
dwidth = 2
}
deferred = append(deferred, r)
}
if len(deferred) != 0 {
s.SetCell(x+i, y, style, deferred...)
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, "")
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()
}