2016-10-24 03:45:45 +00:00
|
|
|
// +build tcell windows
|
|
|
|
|
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
|
2016-11-25 15:36:38 +00:00
|
|
|
// https://github.com/gdamore/tcell/pull/135
|
|
|
|
"github.com/junegunn/tcell"
|
|
|
|
"github.com/junegunn/tcell/encoding"
|
2016-11-06 17:15:34 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/go-runewidth"
|
2016-10-24 03:45:45 +00:00
|
|
|
)
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (p ColorPair) style() tcell.Style {
|
|
|
|
style := tcell.StyleDefault
|
|
|
|
return style.Foreground(tcell.Color(p.Fg())).Background(tcell.Color(p.Bg()))
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
type Attr tcell.Style
|
|
|
|
|
|
|
|
type TcellWindow struct {
|
|
|
|
color bool
|
|
|
|
top int
|
|
|
|
left int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
lastX int
|
|
|
|
lastY int
|
|
|
|
moveCursor bool
|
|
|
|
border bool
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Top() int {
|
|
|
|
return w.top
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Left() int {
|
|
|
|
return w.left
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Width() int {
|
|
|
|
return w.width
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *TcellWindow) Height() int {
|
|
|
|
return w.height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *TcellWindow) Refresh() {
|
|
|
|
if w.moveCursor {
|
|
|
|
_screen.ShowCursor(w.left+w.lastX, w.top+w.lastY)
|
|
|
|
w.moveCursor = false
|
|
|
|
}
|
|
|
|
w.lastX = 0
|
|
|
|
w.lastY = 0
|
|
|
|
if w.border {
|
|
|
|
w.drawBorder()
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) FinishFill() {
|
|
|
|
// NO-OP
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-11-15 14:57:32 +00:00
|
|
|
Bold Attr = Attr(tcell.AttrBold)
|
|
|
|
Dim = Attr(tcell.AttrDim)
|
|
|
|
Blink = Attr(tcell.AttrBlink)
|
|
|
|
Reverse = Attr(tcell.AttrReverse)
|
|
|
|
Underline = Attr(tcell.AttrUnderline)
|
2016-11-23 15:13:10 +00:00
|
|
|
Italic = Attr(tcell.AttrNone) // Not supported
|
2016-10-24 03:45:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AttrRegular Attr = 0
|
|
|
|
)
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) defaultTheme() *ColorTheme {
|
2016-10-24 03:45:45 +00:00
|
|
|
if _screen.Colors() >= 256 {
|
|
|
|
return Dark256
|
|
|
|
}
|
|
|
|
return Default16
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_colorToAttribute = []tcell.Color{
|
|
|
|
tcell.ColorBlack,
|
|
|
|
tcell.ColorRed,
|
|
|
|
tcell.ColorGreen,
|
|
|
|
tcell.ColorYellow,
|
|
|
|
tcell.ColorBlue,
|
|
|
|
tcell.ColorDarkMagenta,
|
|
|
|
tcell.ColorLightCyan,
|
|
|
|
tcell.ColorWhite,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c Color) Style() tcell.Color {
|
|
|
|
if c <= colDefault {
|
|
|
|
return tcell.ColorDefault
|
|
|
|
} else if c >= colBlack && c <= colWhite {
|
|
|
|
return _colorToAttribute[int(c)]
|
|
|
|
} else {
|
|
|
|
return tcell.Color(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Attr) Merge(b Attr) Attr {
|
|
|
|
return a | b
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_screen tcell.Screen
|
|
|
|
)
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) initScreen() {
|
2016-10-24 03:45:45 +00:00
|
|
|
s, e := tcell.NewScreen()
|
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
2017-01-10 17:12:32 +00:00
|
|
|
errorExit()
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
if e = s.Init(); e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
2017-01-10 17:12:32 +00:00
|
|
|
errorExit()
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
if r.mouse {
|
2016-11-06 17:15:34 +00:00
|
|
|
s.EnableMouse()
|
|
|
|
} else {
|
|
|
|
s.DisableMouse()
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
_screen = s
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Init() {
|
2016-11-06 17:15:34 +00:00
|
|
|
encoding.Register()
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
r.initScreen()
|
|
|
|
initTheme(r.theme, r.defaultTheme(), r.forceBlack)
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) MaxX() int {
|
2016-10-24 03:45:45 +00:00
|
|
|
ncols, _ := _screen.Size()
|
|
|
|
return int(ncols)
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) MaxY() int {
|
2016-10-24 03:45:45 +00:00
|
|
|
_, nlines := _screen.Size()
|
|
|
|
return int(nlines)
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) X() int {
|
|
|
|
return w.lastX
|
2016-12-04 17:13:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) DoesAutoWrap() bool {
|
2016-12-04 17:13:47 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Clear() {
|
2016-11-06 17:15:34 +00:00
|
|
|
_screen.Sync()
|
2016-10-24 03:45:45 +00:00
|
|
|
_screen.Clear()
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Refresh() {
|
2016-10-24 03:45:45 +00:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) GetChar() Event {
|
2016-10-24 03:45:45 +00:00
|
|
|
ev := _screen.PollEvent()
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventResize:
|
2016-11-22 16:58:46 +00:00
|
|
|
return Event{Resize, 0, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
// process mouse events:
|
|
|
|
case *tcell.EventMouse:
|
|
|
|
x, y := ev.Position()
|
|
|
|
button := ev.Buttons()
|
|
|
|
mod := ev.Modifiers() != 0
|
|
|
|
if button&tcell.WheelDown != 0 {
|
|
|
|
return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, mod}}
|
|
|
|
} else if button&tcell.WheelUp != 0 {
|
|
|
|
return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, mod}}
|
|
|
|
} else if runtime.GOOS != "windows" {
|
|
|
|
// double and single taps on Windows don't quite work due to
|
|
|
|
// the console acting on the events and not allowing us
|
|
|
|
// to consume them.
|
|
|
|
|
|
|
|
down := button&tcell.Button1 != 0 // left
|
|
|
|
double := false
|
|
|
|
if down {
|
|
|
|
now := time.Now()
|
2017-01-07 16:30:31 +00:00
|
|
|
if now.Sub(r.prevDownTime) < doubleClickDuration {
|
|
|
|
r.clickY = append(r.clickY, x)
|
2016-10-24 03:45:45 +00:00
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
r.clickY = []int{x}
|
|
|
|
r.prevDownTime = now
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] &&
|
|
|
|
time.Now().Sub(r.prevDownTime) < doubleClickDuration {
|
2016-10-24 03:45:45 +00:00
|
|
|
double = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Event{Mouse, 0, &MouseEvent{y, x, 0, down, double, mod}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process keyboard:
|
|
|
|
case *tcell.EventKey:
|
2016-11-06 17:15:34 +00:00
|
|
|
alt := (ev.Modifiers() & tcell.ModAlt) > 0
|
2016-10-24 03:45:45 +00:00
|
|
|
switch ev.Key() {
|
|
|
|
case tcell.KeyCtrlA:
|
|
|
|
return Event{CtrlA, 0, nil}
|
|
|
|
case tcell.KeyCtrlB:
|
|
|
|
return Event{CtrlB, 0, nil}
|
|
|
|
case tcell.KeyCtrlC:
|
|
|
|
return Event{CtrlC, 0, nil}
|
|
|
|
case tcell.KeyCtrlD:
|
|
|
|
return Event{CtrlD, 0, nil}
|
|
|
|
case tcell.KeyCtrlE:
|
|
|
|
return Event{CtrlE, 0, nil}
|
|
|
|
case tcell.KeyCtrlF:
|
|
|
|
return Event{CtrlF, 0, nil}
|
|
|
|
case tcell.KeyCtrlG:
|
|
|
|
return Event{CtrlG, 0, nil}
|
|
|
|
case tcell.KeyCtrlJ:
|
|
|
|
return Event{CtrlJ, 0, nil}
|
|
|
|
case tcell.KeyCtrlK:
|
|
|
|
return Event{CtrlK, 0, nil}
|
|
|
|
case tcell.KeyCtrlL:
|
|
|
|
return Event{CtrlL, 0, nil}
|
|
|
|
case tcell.KeyCtrlM:
|
2016-11-06 17:15:34 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltEnter, 0, nil}
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
return Event{CtrlM, 0, nil}
|
|
|
|
case tcell.KeyCtrlN:
|
|
|
|
return Event{CtrlN, 0, nil}
|
|
|
|
case tcell.KeyCtrlO:
|
|
|
|
return Event{CtrlO, 0, nil}
|
|
|
|
case tcell.KeyCtrlP:
|
|
|
|
return Event{CtrlP, 0, nil}
|
|
|
|
case tcell.KeyCtrlQ:
|
|
|
|
return Event{CtrlQ, 0, nil}
|
|
|
|
case tcell.KeyCtrlR:
|
|
|
|
return Event{CtrlR, 0, nil}
|
|
|
|
case tcell.KeyCtrlS:
|
|
|
|
return Event{CtrlS, 0, nil}
|
|
|
|
case tcell.KeyCtrlT:
|
|
|
|
return Event{CtrlT, 0, nil}
|
|
|
|
case tcell.KeyCtrlU:
|
|
|
|
return Event{CtrlU, 0, nil}
|
|
|
|
case tcell.KeyCtrlV:
|
|
|
|
return Event{CtrlV, 0, nil}
|
|
|
|
case tcell.KeyCtrlW:
|
|
|
|
return Event{CtrlW, 0, nil}
|
|
|
|
case tcell.KeyCtrlX:
|
|
|
|
return Event{CtrlX, 0, nil}
|
|
|
|
case tcell.KeyCtrlY:
|
|
|
|
return Event{CtrlY, 0, nil}
|
|
|
|
case tcell.KeyCtrlZ:
|
|
|
|
return Event{CtrlZ, 0, nil}
|
|
|
|
case tcell.KeyBackspace, tcell.KeyBackspace2:
|
2016-11-06 17:15:34 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltBS, 0, nil}
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
return Event{BSpace, 0, nil}
|
|
|
|
|
|
|
|
case tcell.KeyUp:
|
|
|
|
return Event{Up, 0, nil}
|
|
|
|
case tcell.KeyDown:
|
|
|
|
return Event{Down, 0, nil}
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
return Event{Left, 0, nil}
|
|
|
|
case tcell.KeyRight:
|
|
|
|
return Event{Right, 0, nil}
|
|
|
|
|
|
|
|
case tcell.KeyHome:
|
|
|
|
return Event{Home, 0, nil}
|
|
|
|
case tcell.KeyDelete:
|
|
|
|
return Event{Del, 0, nil}
|
|
|
|
case tcell.KeyEnd:
|
|
|
|
return Event{End, 0, nil}
|
2016-11-06 17:15:34 +00:00
|
|
|
case tcell.KeyPgUp:
|
2016-10-24 03:45:45 +00:00
|
|
|
return Event{PgUp, 0, nil}
|
2016-11-06 17:15:34 +00:00
|
|
|
case tcell.KeyPgDn:
|
|
|
|
return Event{PgDn, 0, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
case tcell.KeyTab:
|
|
|
|
return Event{Tab, 0, nil}
|
2016-11-06 17:15:34 +00:00
|
|
|
case tcell.KeyBacktab:
|
|
|
|
return Event{BTab, 0, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
case tcell.KeyF1:
|
|
|
|
return Event{F1, 0, nil}
|
|
|
|
case tcell.KeyF2:
|
|
|
|
return Event{F2, 0, nil}
|
|
|
|
case tcell.KeyF3:
|
|
|
|
return Event{F3, 0, nil}
|
|
|
|
case tcell.KeyF4:
|
|
|
|
return Event{F4, 0, nil}
|
|
|
|
case tcell.KeyF5:
|
|
|
|
return Event{F5, 0, nil}
|
|
|
|
case tcell.KeyF6:
|
|
|
|
return Event{F6, 0, nil}
|
|
|
|
case tcell.KeyF7:
|
|
|
|
return Event{F7, 0, nil}
|
|
|
|
case tcell.KeyF8:
|
|
|
|
return Event{F8, 0, nil}
|
|
|
|
case tcell.KeyF9:
|
|
|
|
return Event{F9, 0, nil}
|
|
|
|
case tcell.KeyF10:
|
|
|
|
return Event{F10, 0, nil}
|
|
|
|
case tcell.KeyF11:
|
2016-11-19 13:40:28 +00:00
|
|
|
return Event{F11, 0, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
case tcell.KeyF12:
|
2016-11-19 13:40:28 +00:00
|
|
|
return Event{F12, 0, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
// ev.Ch doesn't work for some reason for space:
|
|
|
|
case tcell.KeyRune:
|
2016-11-06 17:15:34 +00:00
|
|
|
r := ev.Rune()
|
|
|
|
if alt {
|
|
|
|
switch r {
|
|
|
|
case ' ':
|
|
|
|
return Event{AltSpace, 0, nil}
|
|
|
|
case '/':
|
|
|
|
return Event{AltSlash, 0, nil}
|
|
|
|
}
|
|
|
|
if r >= 'a' && r <= 'z' {
|
|
|
|
return Event{AltA + int(r) - 'a', 0, nil}
|
|
|
|
}
|
2016-11-19 13:40:28 +00:00
|
|
|
if r >= '0' && r <= '9' {
|
|
|
|
return Event{Alt0 + int(r) - '0', 0, nil}
|
|
|
|
}
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
|
|
|
return Event{Rune, r, nil}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
case tcell.KeyEsc:
|
|
|
|
return Event{ESC, 0, nil}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Pause() {
|
2016-11-06 17:15:34 +00:00
|
|
|
_screen.Fini()
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Resume() bool {
|
|
|
|
r.initScreen()
|
2016-11-06 17:15:34 +00:00
|
|
|
return true
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) Close() {
|
2016-10-24 03:45:45 +00:00
|
|
|
_screen.Fini()
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) RefreshWindows(windows []Window) {
|
2016-10-24 03:45:45 +00:00
|
|
|
// TODO
|
|
|
|
for _, w := range windows {
|
2017-01-07 16:30:31 +00:00
|
|
|
w.Refresh()
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
_screen.Show()
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, border bool) Window {
|
2016-10-24 03:45:45 +00:00
|
|
|
// TODO
|
2017-01-07 16:30:31 +00:00
|
|
|
return &TcellWindow{
|
|
|
|
color: r.theme != nil,
|
|
|
|
top: top,
|
|
|
|
left: left,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
border: border}
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Close() {
|
2016-10-24 03:45:45 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
func fill(x, y, w, h int, r rune) {
|
|
|
|
for ly := 0; ly <= h; ly++ {
|
|
|
|
for lx := 0; lx <= w; lx++ {
|
|
|
|
_screen.SetContent(x+lx, y+ly, r, nil, ColDefault.style())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Erase() {
|
2016-10-24 03:45:45 +00:00
|
|
|
// TODO
|
2017-01-07 16:30:31 +00:00
|
|
|
fill(w.left, w.top, w.width, w.height, ' ')
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Enclose(y int, x int) bool {
|
|
|
|
return x >= w.left && x < (w.left+w.width) &&
|
|
|
|
y >= w.top && y < (w.top+w.height)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Move(y int, x int) {
|
|
|
|
w.lastX = x
|
|
|
|
w.lastY = y
|
|
|
|
w.moveCursor = true
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) MoveAndClear(y int, x int) {
|
2016-10-24 03:45:45 +00:00
|
|
|
w.Move(y, x)
|
2017-01-07 16:30:31 +00:00
|
|
|
for i := w.lastX; i < w.width; i++ {
|
|
|
|
_screen.SetContent(i+w.left, w.lastY+w.top, rune(' '), nil, ColDefault.style())
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
w.lastX = x
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Print(text string) {
|
|
|
|
w.printString(text, ColDefault, 0)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) printString(text string, pair ColorPair, a Attr) {
|
2016-10-24 03:45:45 +00:00
|
|
|
t := text
|
|
|
|
lx := 0
|
|
|
|
|
2016-11-08 16:45:06 +00:00
|
|
|
var style tcell.Style
|
2017-01-07 16:30:31 +00:00
|
|
|
if w.color {
|
2016-11-08 16:45:06 +00:00
|
|
|
style = pair.style().
|
|
|
|
Reverse(a&Attr(tcell.AttrReverse) != 0).
|
|
|
|
Underline(a&Attr(tcell.AttrUnderline) != 0)
|
|
|
|
} else {
|
|
|
|
style = ColDefault.style().
|
|
|
|
Reverse(a&Attr(tcell.AttrReverse) != 0 || pair == ColCurrent || pair == ColCurrentMatch).
|
|
|
|
Underline(a&Attr(tcell.AttrUnderline) != 0 || pair == ColMatch || pair == ColCurrentMatch)
|
|
|
|
}
|
|
|
|
style = style.
|
2016-10-24 03:45:45 +00:00
|
|
|
Blink(a&Attr(tcell.AttrBlink) != 0).
|
|
|
|
Bold(a&Attr(tcell.AttrBold) != 0).
|
2016-11-08 16:45:06 +00:00
|
|
|
Dim(a&Attr(tcell.AttrDim) != 0)
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
if len(t) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r, size := utf8.DecodeRuneInString(t)
|
|
|
|
t = t[size:]
|
|
|
|
|
|
|
|
if r < rune(' ') { // ignore control characters
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r == '\n' {
|
2017-01-07 16:30:31 +00:00
|
|
|
w.lastY++
|
2016-10-24 03:45:45 +00:00
|
|
|
lx = 0
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if r == '\u000D' { // skip carriage return
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
var xPos = w.left + w.lastX + lx
|
|
|
|
var yPos = w.top + w.lastY
|
|
|
|
if xPos < (w.left+w.width) && yPos < (w.top+w.height) {
|
2016-10-24 03:45:45 +00:00
|
|
|
_screen.SetContent(xPos, yPos, r, nil, style)
|
|
|
|
}
|
2016-11-06 17:15:34 +00:00
|
|
|
lx += runewidth.RuneWidth(r)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
w.lastX += lx
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) CPrint(pair ColorPair, attr Attr, text string) {
|
|
|
|
w.printString(text, pair, attr)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) fillString(text string, pair ColorPair, a Attr) bool {
|
2016-10-24 03:45:45 +00:00
|
|
|
lx := 0
|
|
|
|
|
2016-11-08 16:45:06 +00:00
|
|
|
var style tcell.Style
|
2017-01-07 16:30:31 +00:00
|
|
|
if w.color {
|
2016-11-08 16:45:06 +00:00
|
|
|
style = pair.style()
|
|
|
|
} else {
|
|
|
|
style = ColDefault.style()
|
|
|
|
}
|
|
|
|
style = style.
|
2016-10-24 03:45:45 +00:00
|
|
|
Blink(a&Attr(tcell.AttrBlink) != 0).
|
|
|
|
Bold(a&Attr(tcell.AttrBold) != 0).
|
|
|
|
Dim(a&Attr(tcell.AttrDim) != 0).
|
|
|
|
Reverse(a&Attr(tcell.AttrReverse) != 0).
|
|
|
|
Underline(a&Attr(tcell.AttrUnderline) != 0)
|
|
|
|
|
|
|
|
for _, r := range text {
|
|
|
|
if r == '\n' {
|
2017-01-07 16:30:31 +00:00
|
|
|
w.lastY++
|
|
|
|
w.lastX = 0
|
2016-10-24 03:45:45 +00:00
|
|
|
lx = 0
|
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
var xPos = w.left + w.lastX + lx
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
// word wrap:
|
2017-01-07 16:30:31 +00:00
|
|
|
if xPos >= (w.left + w.width) {
|
|
|
|
w.lastY++
|
|
|
|
w.lastX = 0
|
2016-10-24 03:45:45 +00:00
|
|
|
lx = 0
|
2017-01-07 16:30:31 +00:00
|
|
|
xPos = w.left
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
var yPos = w.top + w.lastY
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
if yPos >= (w.top + w.height) {
|
2016-10-24 03:45:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_screen.SetContent(xPos, yPos, r, nil, style)
|
2016-11-06 17:15:34 +00:00
|
|
|
lx += runewidth.RuneWidth(r)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
w.lastX += lx
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) Fill(str string) bool {
|
|
|
|
return w.fillString(str, ColDefault, 0)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) bool {
|
|
|
|
return w.fillString(str, ColorPair{fg, bg, -1}, a)
|
2016-10-24 03:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *TcellWindow) drawBorder() {
|
|
|
|
left := w.left
|
|
|
|
right := left + w.width
|
|
|
|
top := w.top
|
|
|
|
bot := top + w.height
|
2016-10-24 03:45:45 +00:00
|
|
|
|
2016-11-08 16:45:06 +00:00
|
|
|
var style tcell.Style
|
2017-01-07 16:30:31 +00:00
|
|
|
if w.color {
|
2016-11-08 16:45:06 +00:00
|
|
|
style = ColBorder.style()
|
|
|
|
} else {
|
|
|
|
style = ColDefault.style()
|
|
|
|
}
|
2016-10-24 03:45:45 +00:00
|
|
|
|
|
|
|
for x := left; x < right; x++ {
|
|
|
|
_screen.SetContent(x, top, tcell.RuneHLine, nil, style)
|
|
|
|
_screen.SetContent(x, bot-1, tcell.RuneHLine, nil, style)
|
|
|
|
}
|
|
|
|
|
|
|
|
for y := top; y < bot; y++ {
|
|
|
|
_screen.SetContent(left, y, tcell.RuneVLine, nil, style)
|
|
|
|
_screen.SetContent(right-1, y, tcell.RuneVLine, nil, style)
|
|
|
|
}
|
|
|
|
|
|
|
|
_screen.SetContent(left, top, tcell.RuneULCorner, nil, style)
|
|
|
|
_screen.SetContent(right-1, top, tcell.RuneURCorner, nil, style)
|
|
|
|
_screen.SetContent(left, bot-1, tcell.RuneLLCorner, nil, style)
|
|
|
|
_screen.SetContent(right-1, bot-1, tcell.RuneLRCorner, nil, style)
|
|
|
|
}
|