2015-03-20 14:21:50 -06: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 12:13:51 -07:00
|
|
|
package termui
|
|
|
|
|
2015-02-03 18:56:49 -07:00
|
|
|
import 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-02-03 18:56:49 -07:00
|
|
|
type Attribute uint16
|
|
|
|
|
|
|
|
const (
|
|
|
|
ColorDefault Attribute = iota
|
|
|
|
ColorBlack
|
|
|
|
ColorRed
|
|
|
|
ColorGreen
|
|
|
|
ColorYellow
|
|
|
|
ColorBlue
|
|
|
|
ColorMagenta
|
|
|
|
ColorCyan
|
|
|
|
ColorWhite
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func trimStr2Runes(s string, w int) []rune {
|
|
|
|
if w <= 0 {
|
|
|
|
return []rune{}
|
|
|
|
}
|
2015-03-12 01:57:14 -06:00
|
|
|
sw := rw.StringWidth(s)
|
|
|
|
if sw+dotw >= w {
|
|
|
|
return []rune(rw.Truncate(s, w, "…"))
|
2015-03-13 11:20:17 -06:00
|
|
|
} else {
|
|
|
|
return []rune(rw.Truncate(s, w, ""))
|
2015-03-12 01:57:14 -06:00
|
|
|
}
|
2015-02-03 12:13:51 -07:00
|
|
|
}
|