1 parent
2345e16794
commit
667253906b
3 files changed
+119
-10
No files matched your search
@@ -7,10 +7,48 @@ import (
|
||||
"strconv"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/width"
|
||||
)
|
||||
|
||||
func isRoot(name string) bool { return filepath.Dir(name) == name }
|
||||
|
||||
func runeWidth(r rune) int {
|
||||
k := width.LookupRune(r).Kind()
|
||||
if k == width.EastAsianFullwidth || k == width.EastAsianWide {
|
||||
return 2
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func runeSliceWidth(rs []rune) int {
|
||||
w := 0
|
||||
for _, r := range rs {
|
||||
w += runeWidth(r)
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func runeSliceWidthRange(rs []rune, beg int, end int) []rune {
|
||||
curr := 0
|
||||
b := 0
|
||||
for i, r := range rs {
|
||||
w := runeWidth(r)
|
||||
switch {
|
||||
case curr == beg:
|
||||
b = i
|
||||
case curr < beg && curr+w > beg:
|
||||
b = i + 1
|
||||
case curr == end:
|
||||
return rs[b:i]
|
||||
case curr > end:
|
||||
return rs[b : i-1]
|
||||
}
|
||||
curr += w
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// This function converts a size in bytes to a human readable form. For this
|
||||
// purpose metric suffixes are used (e.g. 1K = 1000). For values less than 10
|
||||
// the first significant digit is shown, otherwise it is hidden. Numbers are
|
||||
|
||||
@@ -27,6 +27,70 @@ func TestIsRoot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuneWidth(t *testing.T) {
|
||||
chars := []struct {
|
||||
r rune
|
||||
w int
|
||||
}{
|
||||
{' ', 1},
|
||||
{'a', 1},
|
||||
{'ı', 1},
|
||||
{'ş', 1},
|
||||
{'世', 2},
|
||||
{'界', 2},
|
||||
}
|
||||
|
||||
for _, char := range chars {
|
||||
if w := runeWidth(char.r); w != char.w {
|
||||
t.Errorf("at input '%c' expected '%d' but got '%d'", char.r, char.w, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuneSliceWidth(t *testing.T) {
|
||||
slices := []struct {
|
||||
s []rune
|
||||
w int
|
||||
}{
|
||||
{[]rune{'a', 'b'}, 2},
|
||||
{[]rune{'ı', 'ş'}, 2},
|
||||
{[]rune{'世', '界'}, 4},
|
||||
{[]rune{'世', 'a', '界', 'ı'}, 6},
|
||||
}
|
||||
|
||||
for _, slice := range slices {
|
||||
if w := runeSliceWidth(slice.s); w != slice.w {
|
||||
t.Errorf("at input '%v' expected '%d' but got '%d'", slice.s, slice.w, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuneSliceWidthRange(t *testing.T) {
|
||||
slices := []struct {
|
||||
s []rune
|
||||
i int
|
||||
j int
|
||||
r []rune
|
||||
}{
|
||||
{[]rune{'a', 'b', 'c', 'd'}, 1, 3, []rune{'b', 'c'}},
|
||||
{[]rune{'a', 'ı', 'b', 'ş'}, 1, 3, []rune{'ı', 'b'}},
|
||||
{[]rune{'世', '界', '世', '界'}, 2, 6, []rune{'界', '世'}},
|
||||
{[]rune{'世', '界', '世', '界'}, 3, 6, []rune{'世'}},
|
||||
{[]rune{'世', '界', '世', '界'}, 2, 5, []rune{'界'}},
|
||||
{[]rune{'世', '界', '世', '界'}, 3, 5, []rune{}},
|
||||
{[]rune{'世', 'a', '界', 'ı'}, 2, 5, []rune{'a', '界'}},
|
||||
{[]rune{'世', 'a', '界', 'ı'}, 2, 4, []rune{'a'}},
|
||||
{[]rune{'世', 'a', '界', 'ı'}, 3, 5, []rune{'界'}},
|
||||
{[]rune{'世', 'a', '界', 'ı'}, 3, 4, []rune{}},
|
||||
}
|
||||
|
||||
for _, slice := range slices {
|
||||
if r := runeSliceWidthRange(slice.s, slice.i, slice.j); !reflect.DeepEqual(r, slice.r) {
|
||||
t.Errorf("at input '%v' expected '%v' but got '%v'", slice.s, slice.r, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanize(t *testing.T) {
|
||||
nums := []struct {
|
||||
i int64
|
||||
|
||||
@@ -203,7 +203,7 @@ func (win *Win) print(x, y int, fg, bg termbox.Attribute, s string) {
|
||||
if r == '\t' {
|
||||
x += gOpts.tabstop - (x-off)%gOpts.tabstop
|
||||
} else {
|
||||
x++
|
||||
x += runeWidth(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,16 +264,19 @@ func (win *Win) printd(dir *Dir, marks map[string]bool) {
|
||||
fg = fg | termbox.AttrReverse
|
||||
}
|
||||
|
||||
var s []byte
|
||||
var s []rune
|
||||
|
||||
s = append(s, ' ')
|
||||
|
||||
s = append(s, f.Name()...)
|
||||
for _, r := range f.Name() {
|
||||
s = append(s, r)
|
||||
}
|
||||
|
||||
if len(s) > win.w-2 {
|
||||
s = s[:win.w-2]
|
||||
w := runeSliceWidth(s)
|
||||
if w > win.w-2 {
|
||||
s = runeSliceWidthRange(s, 0, win.w-2)
|
||||
} else {
|
||||
s = append(s, make([]byte, win.w-2-len(s))...)
|
||||
s = append(s, make([]rune, win.w-2-w)...)
|
||||
}
|
||||
|
||||
switch gOpts.showinfo {
|
||||
@@ -282,16 +285,20 @@ func (win *Win) printd(dir *Dir, marks map[string]bool) {
|
||||
case "size":
|
||||
if win.w > 8 {
|
||||
h := humanize(f.Size())
|
||||
s = append(s[:win.w-3-len(h)])
|
||||
s = runeSliceWidthRange(s, 0, win.w-3-len(h))
|
||||
s = append(s, ' ')
|
||||
s = append(s, h...)
|
||||
for _, r := range h {
|
||||
s = append(s, r)
|
||||
}
|
||||
}
|
||||
case "time":
|
||||
if win.w > 24 {
|
||||
t := f.ModTime().Format("Jan _2 15:04")
|
||||
s = append(s[:win.w-3-len(t)])
|
||||
s = runeSliceWidthRange(s, 0, win.w-3-len(t))
|
||||
s = append(s, ' ')
|
||||
s = append(s, t...)
|
||||
for _, r := range t {
|
||||
s = append(s, r)
|
||||
}
|
||||
}
|
||||
default:
|
||||
log.Printf("unknown showinfo type: %s", gOpts.showinfo)
|
||||
|
||||
Reference in new issue
Block a user