termui/helper.go

66 lines
1.2 KiB
Go
Raw Normal View History

2015-03-20 20:21:50 +00:00
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
2015-02-03 19:13:51 +00:00
package termui
2015-02-04 01:56:49 +00:00
import tm "github.com/nsf/termbox-go"
2015-03-12 07:45:30 +00:00
import rw "github.com/mattn/go-runewidth"
2015-02-04 01:56:49 +00:00
2015-03-03 18:28:09 +00:00
/* ---------------Port from termbox-go --------------------- */
// Attribute is printable cell's color and style.
2015-02-04 01:56:49 +00:00
type Attribute uint16
const (
ColorDefault Attribute = iota
ColorBlack
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
const (
AttrBold Attribute = 1 << (iota + 9)
AttrUnderline
AttrReverse
)
var (
dot = "…"
dotw = rw.StringWidth(dot)
)
2015-03-03 18:28:09 +00:00
/* ----------------------- End ----------------------------- */
2015-02-04 01:56:49 +00:00
func toTmAttr(x Attribute) tm.Attribute {
return tm.Attribute(x)
}
2015-02-03 19:13:51 +00:00
func str2runes(s string) []rune {
2015-03-12 07:45:30 +00:00
return []rune(s)
2015-02-03 19:13:51 +00:00
}
func trimStr2Runes(s string, w int) []rune {
if w <= 0 {
return []rune{}
}
sw := rw.StringWidth(s)
if sw > w {
return []rune(rw.Truncate(s, w, dot))
}
return str2runes(s) //[]rune(rw.Truncate(s, w, ""))
}
func strWidth(s string) int {
return rw.StringWidth(s)
}
func charWidth(ch rune) int {
return rw.RuneWidth(ch)
2015-02-03 19:13:51 +00:00
}