Merge pull request #2 from mattn/fix-runewidth

Fix runewidth
This commit is contained in:
Zack Guo 2015-03-13 00:24:36 -04:00
commit dd3b43990c
7 changed files with 46 additions and 27 deletions

7
box.go
View File

@ -1,12 +1,5 @@
package termui package termui
const TOP_RIGHT = '┐'
const VERTICAL_LINE = '│'
const HORIZONTAL_LINE = '─'
const TOP_LEFT = '┌'
const BOTTOM_RIGHT = '┘'
const BOTTOM_LEFT = '└'
type border struct { type border struct {
X int X int
Y int Y int

10
box_others.go Normal file
View File

@ -0,0 +1,10 @@
// +build !windows
package termui
const TOP_RIGHT = '┐'
const VERTICAL_LINE = '│'
const HORIZONTAL_LINE = '─'
const TOP_LEFT = '┌'
const BOTTOM_RIGHT = '┘'
const BOTTOM_LEFT = '└'

10
box_windows.go Normal file
View File

@ -0,0 +1,10 @@
// +build windows
package termui
const TOP_RIGHT = '+'
const VERTICAL_LINE = '|'
const HORIZONTAL_LINE = '-'
const TOP_LEFT = '+'
const BOTTOM_RIGHT = '+'
const BOTTOM_LEFT = '+'

View File

@ -2,10 +2,6 @@ package termui
import "fmt" import "fmt"
const VDASH = '┊'
const HDASH = '┈'
const ORIGIN = '└'
// only 16 possible combinations, why bother // only 16 possible combinations, why bother
var braillePatterns = map[[2]int]rune{ var braillePatterns = map[[2]int]rune{
[2]int{0, 0}: '⣀', [2]int{0, 0}: '⣀',

7
chart_others.go Normal file
View File

@ -0,0 +1,7 @@
// +build !windows
package termui
const VDASH = '┊'
const HDASH = '┈'
const ORIGIN = '└'

7
chart_windows.go Normal file
View File

@ -0,0 +1,7 @@
// +build windows
package termui
const VDASH = '|'
const HDASH = '-'
const ORIGIN = '+'

View File

@ -1,8 +1,7 @@
package termui package termui
import "unicode/utf8"
import "strings"
import tm "github.com/nsf/termbox-go" import tm "github.com/nsf/termbox-go"
import rw "github.com/mattn/go-runewidth"
/* ---------------Port from termbox-go --------------------- */ /* ---------------Port from termbox-go --------------------- */
@ -26,6 +25,11 @@ const (
AttrReverse AttrReverse
) )
var (
dot = "…"
dotw = rw.StringWidth(dot)
)
/* ----------------------- End ----------------------------- */ /* ----------------------- End ----------------------------- */
func toTmAttr(x Attribute) tm.Attribute { func toTmAttr(x Attribute) tm.Attribute {
@ -33,25 +37,17 @@ func toTmAttr(x Attribute) tm.Attribute {
} }
func str2runes(s string) []rune { func str2runes(s string) []rune {
n := utf8.RuneCountInString(s) return []rune(s)
ss := strings.Split(s, "")
rs := make([]rune, n)
for i := 0; i < n; i++ {
r, _ := utf8.DecodeRuneInString(ss[i])
rs[i] = r
}
return rs
} }
func trimStr2Runes(s string, w int) []rune { func trimStr2Runes(s string, w int) []rune {
rs := str2runes(s)
if w <= 0 { if w <= 0 {
return []rune{} return []rune{}
} }
if len(rs) > w { sw := rw.StringWidth(s)
rs = rs[:w] if sw+dotw >= w {
rs[w-1] = '…' return []rune(rw.Truncate(s, w, ""))
} else {
return []rune(rw.Truncate(s, w, "…"))
} }
return rs
} }