2015-03-21 04:21:50 +08: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-04 03:13:51 +08:00
|
|
|
package termui
|
|
|
|
|
2015-02-04 09:56:49 +08:00
|
|
|
import tm "github.com/nsf/termbox-go"
|
2015-03-12 15:45:30 +08:00
|
|
|
import rw "github.com/mattn/go-runewidth"
|
2015-02-04 09:56:49 +08:00
|
|
|
|
2015-03-04 02:28:09 +08:00
|
|
|
/* ---------------Port from termbox-go --------------------- */
|
|
|
|
|
2015-03-26 06:04:15 +08:00
|
|
|
// Attribute is printable cell's color and style.
|
2015-02-04 09:56:49 +08: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 15:57:14 +08:00
|
|
|
var (
|
|
|
|
dot = "…"
|
|
|
|
dotw = rw.StringWidth(dot)
|
|
|
|
)
|
|
|
|
|
2015-03-04 02:28:09 +08:00
|
|
|
/* ----------------------- End ----------------------------- */
|
|
|
|
|
2015-02-04 09:56:49 +08:00
|
|
|
func toTmAttr(x Attribute) tm.Attribute {
|
|
|
|
return tm.Attribute(x)
|
|
|
|
}
|
2015-02-04 03:13:51 +08:00
|
|
|
|
|
|
|
func str2runes(s string) []rune {
|
2015-03-12 15:45:30 +08:00
|
|
|
return []rune(s)
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|
|
|
|
|
2015-04-02 04:25:00 +08:00
|
|
|
// Here for backwards-compatibility.
|
2015-02-04 03:13:51 +08:00
|
|
|
func trimStr2Runes(s string, w int) []rune {
|
2015-04-02 04:25:00 +08: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-04 03:13:51 +08:00
|
|
|
if w <= 0 {
|
|
|
|
return []rune{}
|
|
|
|
}
|
2015-04-02 04:25:00 +08:00
|
|
|
|
2015-03-12 15:57:14 +08:00
|
|
|
sw := rw.StringWidth(s)
|
2015-03-26 06:04:15 +08:00
|
|
|
if sw > w {
|
|
|
|
return []rune(rw.Truncate(s, w, dot))
|
2015-03-12 15:57:14 +08:00
|
|
|
}
|
2015-04-02 04:25:00 +08: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-26 06:04:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func strWidth(s string) int {
|
|
|
|
return rw.StringWidth(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func charWidth(ch rune) int {
|
|
|
|
return rw.RuneWidth(ch)
|
2015-02-04 03:13:51 +08:00
|
|
|
}
|