aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/peterh/liner/width.go
blob: 02cfb5e1bf1d70ae00c3c4799a41a282104826d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package liner

import "unicode"

// These character classes are mostly zero width (when combined).
// A few might not be, depending on the user's font. Fixing this
// is non-trivial, given that some terminals don't support
// ANSI DSR/CPR
var zeroWidth = []*unicode.RangeTable{
    unicode.Mn,
    unicode.Me,
    unicode.Cc,
    unicode.Cf,
}

func countGlyphs(s []rune) int {
    n := 0
    for _, r := range s {
        if !unicode.IsOneOf(zeroWidth, r) {
            n++
        }
    }
    return n
}

func getPrefixGlyphs(s []rune, num int) []rune {
    p := 0
    for n := 0; n < num && p < len(s); p++ {
        if !unicode.IsOneOf(zeroWidth, s[p]) {
            n++
        }
    }
    for p < len(s) && unicode.IsOneOf(zeroWidth, s[p]) {
        p++
    }
    return s[:p]
}

func getSuffixGlyphs(s []rune, num int) []rune {
    p := len(s)
    for n := 0; n < num && p > 0; p-- {
        if !unicode.IsOneOf(zeroWidth, s[p-1]) {
            n++
        }
    }
    return s[p:]
}