2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-20 14:21:50 -06:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-03 12:13:51 -07:00
|
|
|
package termui
|
|
|
|
|
2015-04-03 07:14:39 -06:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
tm "github.com/nsf/termbox-go"
|
|
|
|
)
|
2015-03-12 01:45:30 -06:00
|
|
|
import rw "github.com/mattn/go-runewidth"
|
2015-02-03 18:56:49 -07:00
|
|
|
|
2015-03-03 11:28:09 -07:00
|
|
|
/* ---------------Port from termbox-go --------------------- */
|
|
|
|
|
2015-03-25 16:04:15 -06:00
|
|
|
// Attribute is printable cell's color and style.
|
2015-02-03 18:56:49 -07:00
|
|
|
type Attribute uint16
|
|
|
|
|
2015-05-13 06:57:11 -06:00
|
|
|
// 8 basic clolrs
|
2015-02-03 18:56:49 -07:00
|
|
|
const (
|
|
|
|
ColorDefault Attribute = iota
|
|
|
|
ColorBlack
|
|
|
|
ColorRed
|
|
|
|
ColorGreen
|
|
|
|
ColorYellow
|
|
|
|
ColorBlue
|
|
|
|
ColorMagenta
|
|
|
|
ColorCyan
|
|
|
|
ColorWhite
|
|
|
|
)
|
|
|
|
|
2015-05-13 06:57:11 -06:00
|
|
|
//Have a constant that defines number of colors
|
|
|
|
const NumberofColors = 8
|
|
|
|
|
|
|
|
// Text style
|
2015-02-03 18:56:49 -07:00
|
|
|
const (
|
|
|
|
AttrBold Attribute = 1 << (iota + 9)
|
|
|
|
AttrUnderline
|
|
|
|
AttrReverse
|
|
|
|
)
|
|
|
|
|
2015-03-12 01:57:14 -06:00
|
|
|
var (
|
|
|
|
dot = "…"
|
|
|
|
dotw = rw.StringWidth(dot)
|
|
|
|
)
|
|
|
|
|
2015-03-03 11:28:09 -07:00
|
|
|
/* ----------------------- End ----------------------------- */
|
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
func toTmAttr(x Attribute) tm.Attribute {
|
|
|
|
return tm.Attribute(x)
|
|
|
|
}
|
2015-02-03 12:13:51 -07:00
|
|
|
|
|
|
|
func str2runes(s string) []rune {
|
2015-03-12 01:45:30 -06:00
|
|
|
return []rune(s)
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|
|
|
|
|
2015-04-01 14:25:00 -06:00
|
|
|
// Here for backwards-compatibility.
|
2015-02-03 12:13:51 -07:00
|
|
|
func trimStr2Runes(s string, w int) []rune {
|
2015-04-01 14:25:00 -06:00
|
|
|
return TrimStr2Runes(s, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrimStr2Runes trims string to w[-1 rune], appends …, and returns the runes
|
|
|
|
// of that string if string is grather then n. If string is small then w,
|
|
|
|
// return the runes.
|
|
|
|
func TrimStr2Runes(s string, w int) []rune {
|
2015-02-03 12:13:51 -07:00
|
|
|
if w <= 0 {
|
|
|
|
return []rune{}
|
|
|
|
}
|
2015-04-01 14:25:00 -06:00
|
|
|
|
2015-03-12 01:57:14 -06:00
|
|
|
sw := rw.StringWidth(s)
|
2015-03-25 16:04:15 -06:00
|
|
|
if sw > w {
|
|
|
|
return []rune(rw.Truncate(s, w, dot))
|
2015-03-12 01:57:14 -06:00
|
|
|
}
|
2015-04-01 14:25:00 -06:00
|
|
|
return str2runes(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrimStrIfAppropriate trim string to "s[:-1] + …"
|
|
|
|
// if string > width otherwise return string
|
|
|
|
func TrimStrIfAppropriate(s string, w int) string {
|
|
|
|
if w <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
sw := rw.StringWidth(s)
|
|
|
|
if sw > w {
|
|
|
|
return rw.Truncate(s, w, dot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2015-03-25 16:04:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func strWidth(s string) int {
|
|
|
|
return rw.StringWidth(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func charWidth(ch rune) int {
|
|
|
|
return rw.RuneWidth(ch)
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|
2015-04-03 07:14:39 -06:00
|
|
|
|
|
|
|
var whiteSpaceRegex = regexp.MustCompile(`\s`)
|
|
|
|
|
2017-04-03 23:47:18 -06:00
|
|
|
// StringToAttribute converts text to a termui attribute. You may specify more
|
2015-04-03 07:14:39 -06:00
|
|
|
// then one attribute like that: "BLACK, BOLD, ...". All whitespaces
|
|
|
|
// are ignored.
|
|
|
|
func StringToAttribute(text string) Attribute {
|
|
|
|
text = whiteSpaceRegex.ReplaceAllString(strings.ToLower(text), "")
|
|
|
|
attributes := strings.Split(text, ",")
|
|
|
|
result := Attribute(0)
|
|
|
|
|
|
|
|
for _, theAttribute := range attributes {
|
|
|
|
var match Attribute
|
|
|
|
switch theAttribute {
|
|
|
|
case "reset", "default":
|
|
|
|
match = ColorDefault
|
|
|
|
|
|
|
|
case "black":
|
|
|
|
match = ColorBlack
|
|
|
|
|
|
|
|
case "red":
|
|
|
|
match = ColorRed
|
|
|
|
|
|
|
|
case "green":
|
|
|
|
match = ColorGreen
|
|
|
|
|
|
|
|
case "yellow":
|
|
|
|
match = ColorYellow
|
|
|
|
|
|
|
|
case "blue":
|
|
|
|
match = ColorBlue
|
|
|
|
|
|
|
|
case "magenta":
|
|
|
|
match = ColorMagenta
|
|
|
|
|
|
|
|
case "cyan":
|
|
|
|
match = ColorCyan
|
|
|
|
|
|
|
|
case "white":
|
|
|
|
match = ColorWhite
|
|
|
|
|
|
|
|
case "bold":
|
|
|
|
match = AttrBold
|
|
|
|
|
|
|
|
case "underline":
|
|
|
|
match = AttrUnderline
|
|
|
|
|
|
|
|
case "reverse":
|
|
|
|
match = AttrReverse
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= match
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2015-04-21 07:56:10 -06:00
|
|
|
|
|
|
|
// TextCells returns a coloured text cells []Cell
|
|
|
|
func TextCells(s string, fg, bg Attribute) []Cell {
|
|
|
|
cs := make([]Cell, 0, len(s))
|
|
|
|
|
|
|
|
// sequence := MarkdownTextRendererFactory{}.TextRenderer(s).Render(fg, bg)
|
|
|
|
// runes := []rune(sequence.NormalizedText)
|
|
|
|
runes := str2runes(s)
|
|
|
|
|
|
|
|
for n := range runes {
|
|
|
|
// point, _ := sequence.PointAt(n, 0, 0)
|
|
|
|
// cs = append(cs, Cell{point.Ch, point.Fg, point.Bg})
|
|
|
|
cs = append(cs, Cell{runes[n], fg, bg})
|
|
|
|
}
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width returns the actual screen space the cell takes (usually 1 or 2).
|
|
|
|
func (c Cell) Width() int {
|
|
|
|
return charWidth(c.Ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy return a copy of c
|
|
|
|
func (c Cell) Copy() Cell {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrimTxCells trims the overflowed text cells sequence.
|
|
|
|
func TrimTxCells(cs []Cell, w int) []Cell {
|
|
|
|
if len(cs) <= w {
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
return cs[:w]
|
|
|
|
}
|
|
|
|
|
|
|
|
// DTrimTxCls trims the overflowed text cells sequence and append dots at the end.
|
|
|
|
func DTrimTxCls(cs []Cell, w int) []Cell {
|
|
|
|
l := len(cs)
|
|
|
|
if l <= 0 {
|
|
|
|
return []Cell{}
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := make([]Cell, 0, w)
|
|
|
|
csw := 0
|
|
|
|
for i := 0; i < l && csw <= w; i++ {
|
|
|
|
c := cs[i]
|
|
|
|
cw := c.Width()
|
|
|
|
|
2015-10-08 20:11:26 -06:00
|
|
|
if cw+csw < w {
|
2015-04-21 07:56:10 -06:00
|
|
|
rt = append(rt, c)
|
2015-10-08 20:11:26 -06:00
|
|
|
csw += cw
|
2015-04-21 07:56:10 -06:00
|
|
|
} else {
|
|
|
|
rt = append(rt, Cell{'…', c.Fg, c.Bg})
|
2015-10-08 20:11:26 -06:00
|
|
|
break
|
2015-04-21 07:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt
|
|
|
|
}
|
2016-03-10 13:49:31 -07:00
|
|
|
|
|
|
|
func CellsToStr(cs []Cell) string {
|
|
|
|
str := ""
|
|
|
|
for _, c := range cs {
|
|
|
|
str += string(c.Ch)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|