This commit is contained in:
Gokcehan committed 2016-12-18 21:34:41 +03:00
1 parent e532db082f
commit a717c31ca1
12 files changed
+304 -305

No files matched your search

+1 -1
View File
@@ -58,7 +58,7 @@ func waitKey() error {
func (app *app) handleInp() { func (app *app) handleInp() {
clientChan := app.ui.readExpr() clientChan := app.ui.readExpr()
var serverChan chan expr var serverChan <-chan expr
c, err := net.Dial("unix", gSocketPath) c, err := net.Dial("unix", gSocketPath)
if err != nil { if err != nil {
+1 -1
View File
@@ -64,7 +64,7 @@ func client() {
app.handleInp() app.handleInp()
} }
func readExpr(c net.Conn) chan expr { func readExpr(c net.Conn) <-chan expr {
ch := make(chan expr) ch := make(chan expr)
go func() { go func() {
+1 -1
View File
@@ -206,7 +206,7 @@ func compCmd(acc []rune) (matches []string, longestAcc []rune) {
switch len(f) { switch len(f) {
case 1: case 1:
words := gCmdWords words := gCmdWords
for c, _ := range gOpts.cmds { for c := range gOpts.cmds {
words = append(words, c) words = append(words, c)
} }
sort.Strings(words) sort.Strings(words)
+5 -5
View File
@@ -7,9 +7,9 @@ import (
func TestMatchLongest(t *testing.T) { func TestMatchLongest(t *testing.T) {
tests := []struct { tests := []struct {
fst string s1 string
snd string s2 string
res string exp string
}{ }{
{"", "", ""}, {"", "", ""},
{"", "foo", ""}, {"", "foo", ""},
@@ -21,8 +21,8 @@ func TestMatchLongest(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if l := matchLongest(test.fst, test.snd); l != test.res { if got := matchLongest(test.s1, test.s2); got != test.exp {
t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.fst, test.snd, test.res, l) t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got)
} }
} }
} }
+16 -7
View File
@@ -188,15 +188,24 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
case "open": case "open":
err := app.nav.open() curr, err := app.nav.currFile()
if err == nil { if err != nil {
app.ui.loadFile(app.nav) msg := fmt.Sprintf("opening: %s", err)
app.ui.loadFileInfo(app.nav) app.ui.message = msg
log.Print(msg)
return return
} }
if err != errNotDir {
app.ui.message = err.Error() if curr.IsDir() {
log.Print(err) app.nav.open()
if err != nil {
msg := fmt.Sprintf("opening directory: %s", err)
app.ui.message = msg
log.Print(msg)
return
}
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
return return
} }
+42 -42
View File
@@ -6,10 +6,10 @@ import (
) )
// These inputs are used in scan and parse tests. // These inputs are used in scan and parse tests.
var gTests = []struct { var gEvalTests = []struct {
inp string inp string
toks []string toks []string
exprs []Expr exprs []expr
}{ }{
{ {
"", "",
@@ -26,217 +26,217 @@ var gTests = []struct {
{ {
"echo hello", "echo hello",
[]string{"echo", "hello", "\n"}, []string{"echo", "hello", "\n"},
[]Expr{&callExpr{"echo", []string{"hello"}}}, []expr{&callExpr{"echo", []string{"hello"}}},
}, },
{ {
"echo hello world", "echo hello world",
[]string{"echo", "hello", "world", "\n"}, []string{"echo", "hello", "world", "\n"},
[]Expr{&callExpr{"echo", []string{"hello", "world"}}}, []expr{&callExpr{"echo", []string{"hello", "world"}}},
}, },
{ {
"echo 'hello world'", "echo 'hello world'",
[]string{"echo", "hello world", "\n"}, []string{"echo", "hello world", "\n"},
[]Expr{&callExpr{"echo", []string{"hello world"}}}, []expr{&callExpr{"echo", []string{"hello world"}}},
}, },
{ {
`echo hello\ world`, `echo hello\ world`,
[]string{"echo", "hello world", "\n"}, []string{"echo", "hello world", "\n"},
[]Expr{&callExpr{"echo", []string{"hello world"}}}, []expr{&callExpr{"echo", []string{"hello world"}}},
}, },
{ {
`echo hello\ world`, `echo hello\ world`,
[]string{"echo", "hello\tworld", "\n"}, []string{"echo", "hello\tworld", "\n"},
[]Expr{&callExpr{"echo", []string{"hello\tworld"}}}, []expr{&callExpr{"echo", []string{"hello\tworld"}}},
}, },
{ {
"echo hello\\\nworld", "echo hello\\\nworld",
[]string{"echo", "hello\nworld", "\n"}, []string{"echo", "hello\nworld", "\n"},
[]Expr{&callExpr{"echo", []string{"hello\nworld"}}}, []expr{&callExpr{"echo", []string{"hello\nworld"}}},
}, },
{ {
`echo hello\aworld`, `echo hello\aworld`,
[]string{"echo", "helloworld", "\n"}, []string{"echo", "helloworld", "\n"},
[]Expr{&callExpr{"echo", []string{"helloworld"}}}, []expr{&callExpr{"echo", []string{"helloworld"}}},
}, },
{ {
"set hidden # trailing comments are allowed", "set hidden # trailing comments are allowed",
[]string{"set", "hidden", "\n"}, []string{"set", "hidden", "\n"},
[]Expr{&setExpr{"hidden", ""}}, []expr{&setExpr{"hidden", ""}},
}, },
{ {
"set hidden; set preview", "set hidden; set preview",
[]string{"set", "hidden", ";", "set", "preview", "\n"}, []string{"set", "hidden", ";", "set", "preview", "\n"},
[]Expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, []expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}},
}, },
{ {
"set hidden\nset preview", "set hidden\nset preview",
[]string{"set", "hidden", "\n", "set", "preview", "\n"}, []string{"set", "hidden", "\n", "set", "preview", "\n"},
[]Expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, []expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}},
}, },
{ {
"set ratios 1:2:3", "set ratios 1:2:3",
[]string{"set", "ratios", "1:2:3", "\n"}, []string{"set", "ratios", "1:2:3", "\n"},
[]Expr{&setExpr{"ratios", "1:2:3"}}, []expr{&setExpr{"ratios", "1:2:3"}},
}, },
{ {
"set ratios 1:2:3;", "set ratios 1:2:3;",
[]string{"set", "ratios", "1:2:3", ";"}, []string{"set", "ratios", "1:2:3", ";"},
[]Expr{&setExpr{"ratios", "1:2:3"}}, []expr{&setExpr{"ratios", "1:2:3"}},
}, },
{ {
":set ratios 1:2:3", ":set ratios 1:2:3",
[]string{":", "set", "ratios", "1:2:3", "\n", "\n"}, []string{":", "set", "ratios", "1:2:3", "\n", "\n"},
[]Expr{&listExpr{[]Expr{&setExpr{"ratios", "1:2:3"}}}}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}}},
}, },
{ {
":set ratios 1:2:3\nset hidden", ":set ratios 1:2:3\nset hidden",
[]string{":", "set", "ratios", "1:2:3", "\n", "\n", "set", "hidden", "\n"}, []string{":", "set", "ratios", "1:2:3", "\n", "\n", "set", "hidden", "\n"},
[]Expr{&listExpr{[]Expr{&setExpr{"ratios", "1:2:3"}}}, &setExpr{"hidden", ""}}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}}, &setExpr{"hidden", ""}},
}, },
{ {
":set ratios 1:2:3;", ":set ratios 1:2:3;",
[]string{":", "set", "ratios", "1:2:3", ";", "\n"}, []string{":", "set", "ratios", "1:2:3", ";", "\n"},
[]Expr{&listExpr{[]Expr{&setExpr{"ratios", "1:2:3"}}}}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}}},
}, },
{ {
":set ratios 1:2:3;\nset hidden", ":set ratios 1:2:3;\nset hidden",
[]string{":", "set", "ratios", "1:2:3", ";", "\n", "set", "hidden", "\n"}, []string{":", "set", "ratios", "1:2:3", ";", "\n", "set", "hidden", "\n"},
[]Expr{&listExpr{[]Expr{&setExpr{"ratios", "1:2:3"}}}, &setExpr{"hidden", ""}}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}}, &setExpr{"hidden", ""}},
}, },
{ {
"map gh cd ~", "map gh cd ~",
[]string{"map", "gh", "cd", "~", "\n"}, []string{"map", "gh", "cd", "~", "\n"},
[]Expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}}}}, []expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}}}},
}, },
{ {
"map gh cd ~;", "map gh cd ~;",
[]string{"map", "gh", "cd", "~", ";"}, []string{"map", "gh", "cd", "~", ";"},
[]Expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}}}}, []expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}}}},
}, },
{ {
"map gh :cd ~", "map gh :cd ~",
[]string{"map", "gh", ":", "cd", "~", "\n", "\n"}, []string{"map", "gh", ":", "cd", "~", "\n", "\n"},
[]Expr{&mapExpr{"gh", &listExpr{[]Expr{&callExpr{"cd", []string{"~"}}}}}}, []expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}}}}}},
}, },
{ {
"map gh :cd ~;", "map gh :cd ~;",
[]string{"map", "gh", ":", "cd", "~", ";", "\n"}, []string{"map", "gh", ":", "cd", "~", ";", "\n"},
[]Expr{&mapExpr{"gh", &listExpr{[]Expr{&callExpr{"cd", []string{"~"}}}}}}, []expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}}}}}},
}, },
{ {
"cmd usage $du -h . | less", "cmd usage $du -h . | less",
[]string{"cmd", "usage", "$", "du -h . | less", "\n"}, []string{"cmd", "usage", "$", "du -h . | less", "\n"},
[]Expr{&cmdExpr{"usage", &execExpr{"$", "du -h . | less"}}}, []expr{&cmdExpr{"usage", &execExpr{"$", "du -h . | less"}}},
}, },
{ {
"cmd 世界 $echo 世界", "cmd 世界 $echo 世界",
[]string{"cmd", "世界", "$", "echo 世界", "\n"}, []string{"cmd", "世界", "$", "echo 世界", "\n"},
[]Expr{&cmdExpr{"世界", &execExpr{"$", "echo 世界"}}}, []expr{&cmdExpr{"世界", &execExpr{"$", "echo 世界"}}},
}, },
{ {
"map u usage", "map u usage",
[]string{"map", "u", "usage", "\n"}, []string{"map", "u", "usage", "\n"},
[]Expr{&mapExpr{"u", &callExpr{"usage", nil}}}, []expr{&mapExpr{"u", &callExpr{"usage", nil}}},
}, },
{ {
"map u usage;", "map u usage;",
[]string{"map", "u", "usage", ";"}, []string{"map", "u", "usage", ";"},
[]Expr{&mapExpr{"u", &callExpr{"usage", nil}}}, []expr{&mapExpr{"u", &callExpr{"usage", nil}}},
}, },
{ {
"map u :usage", "map u :usage",
[]string{"map", "u", ":", "usage", "\n", "\n"}, []string{"map", "u", ":", "usage", "\n", "\n"},
[]Expr{&mapExpr{"u", &listExpr{[]Expr{&callExpr{"usage", nil}}}}}, []expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil}}}}},
}, },
{ {
"map u :usage;", "map u :usage;",
[]string{"map", "u", ":", "usage", ";", "\n"}, []string{"map", "u", ":", "usage", ";", "\n"},
[]Expr{&mapExpr{"u", &listExpr{[]Expr{&callExpr{"usage", nil}}}}}, []expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil}}}}},
}, },
{ {
"map r push :rename<space>", "map r push :rename<space>",
[]string{"map", "r", "push", ":rename<space>", "\n"}, []string{"map", "r", "push", ":rename<space>", "\n"},
[]Expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}}}}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}}}},
}, },
{ {
"map r push :rename<space>;", "map r push :rename<space>;",
[]string{"map", "r", "push", ":rename<space>;", "\n"}, []string{"map", "r", "push", ":rename<space>;", "\n"},
[]Expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>;"}}}}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>;"}}}},
}, },
{ {
"map r push :rename<space> # trailing comments are allowed after a space", "map r push :rename<space> # trailing comments are allowed after a space",
[]string{"map", "r", "push", ":rename<space>", "\n"}, []string{"map", "r", "push", ":rename<space>", "\n"},
[]Expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}}}}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename<space>"}}}},
}, },
{ {
"map r :push :rename<space>", "map r :push :rename<space>",
[]string{"map", "r", ":", "push", ":rename<space>", "\n", "\n"}, []string{"map", "r", ":", "push", ":rename<space>", "\n", "\n"},
[]Expr{&mapExpr{"r", &listExpr{[]Expr{&callExpr{"push", []string{":rename<space>"}}}}}}, []expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename<space>"}}}}}},
}, },
{ {
"map r :push :rename<space> ; set hidden", "map r :push :rename<space> ; set hidden",
[]string{"map", "r", ":", "push", ":rename<space>", ";", "set", "hidden", "\n", "\n"}, []string{"map", "r", ":", "push", ":rename<space>", ";", "set", "hidden", "\n", "\n"},
[]Expr{&mapExpr{"r", &listExpr{[]Expr{&callExpr{"push", []string{":rename<space>"}}, &setExpr{"hidden", ""}}}}}, []expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename<space>"}}, &setExpr{"hidden", ""}}}}},
}, },
{ {
"map u $du -h . | less", "map u $du -h . | less",
[]string{"map", "u", "$", "du -h . | less", "\n"}, []string{"map", "u", "$", "du -h . | less", "\n"},
[]Expr{&mapExpr{"u", &execExpr{"$", "du -h . | less"}}}, []expr{&mapExpr{"u", &execExpr{"$", "du -h . | less"}}},
}, },
{ {
"cmd usage $du -h \"$1\" | less", "cmd usage $du -h \"$1\" | less",
[]string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"}, []string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"},
[]Expr{&cmdExpr{"usage", &execExpr{"$", `du -h "$1" | less`}}}, []expr{&cmdExpr{"usage", &execExpr{"$", `du -h "$1" | less`}}},
}, },
{ {
"map u usage /", "map u usage /",
[]string{"map", "u", "usage", "/", "\n"}, []string{"map", "u", "usage", "/", "\n"},
[]Expr{&mapExpr{"u", &callExpr{"usage", []string{"/"}}}}, []expr{&mapExpr{"u", &callExpr{"usage", []string{"/"}}}},
}, },
{ {
"map ss :set sortby size; set showinfo size", "map ss :set sortby size; set showinfo size",
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", "\n", "\n"}, []string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", "\n", "\n"},
[]Expr{&mapExpr{"ss", &listExpr{[]Expr{&setExpr{"sortby", "size"}, &setExpr{"showinfo", "size"}}}}}, []expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"showinfo", "size"}}}}},
}, },
{ {
"map ss :set sortby size; set showinfo size;", "map ss :set sortby size; set showinfo size;",
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", ";", "\n"}, []string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", ";", "\n"},
[]Expr{&mapExpr{"ss", &listExpr{[]Expr{&setExpr{"sortby", "size"}, &setExpr{"showinfo", "size"}}}}}, []expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"showinfo", "size"}}}}},
}, },
{ {
@@ -248,7 +248,7 @@ var gTests = []struct {
"cd", "~", "\n", "cd", "~", "\n",
"set", "hidden", "\n", "set", "hidden", "\n",
"}}", "\n"}, "}}", "\n"},
[]Expr{&cmdExpr{"gohome", &listExpr{[]Expr{ []expr{&cmdExpr{"gohome", &listExpr{[]expr{
&callExpr{"cd", []string{"~"}}, &callExpr{"cd", []string{"~"}},
&setExpr{"hidden", ""}}}, &setExpr{"hidden", ""}}},
}}, }},
@@ -263,7 +263,7 @@ var gTests = []struct {
"cd", "~", "\n", "cd", "~", "\n",
"set", "hidden", "\n", "set", "hidden", "\n",
"}}", "\n"}, "}}", "\n"},
[]Expr{&mapExpr{"gh", &listExpr{[]Expr{ []expr{&mapExpr{"gh", &listExpr{[]expr{
&callExpr{"cd", []string{"~"}}, &callExpr{"cd", []string{"~"}},
&setExpr{"hidden", ""}}}, &setExpr{"hidden", ""}}},
}}, }},
@@ -282,7 +282,7 @@ var gTests = []struct {
tar -czvf "foo.tar.gz" foo tar -czvf "foo.tar.gz" foo
rm -rf foo rm -rf foo
`, "}}", "\n"}, `, "}}", "\n"},
[]Expr{&mapExpr{"c", &execExpr{"$", ` []expr{&mapExpr{"c", &execExpr{"$", `
mkdir foo mkdir foo
IFS=':'; cp ${fs} foo IFS=':'; cp ${fs} foo
tar -czvf "foo.tar.gz" foo tar -czvf "foo.tar.gz" foo
@@ -303,7 +303,7 @@ var gTests = []struct {
tar -czvf "$1.tar.gz" "$1" tar -czvf "$1.tar.gz" "$1"
rm -rf "$1" rm -rf "$1"
`, "}}", "\n"}, `, "}}", "\n"},
[]Expr{&cmdExpr{"compress", &execExpr{"$", ` []expr{&cmdExpr{"compress", &execExpr{"$", `
mkdir "$1" mkdir "$1"
IFS=':'; cp ${fs} "$1" IFS=':'; cp ${fs} "$1"
tar -czvf "$1.tar.gz" "$1" tar -czvf "$1.tar.gz" "$1"
+53 -53
View File
@@ -28,9 +28,9 @@ func TestIsRoot(t *testing.T) {
} }
func TestRuneWidth(t *testing.T) { func TestRuneWidth(t *testing.T) {
chars := []struct { tests := []struct {
r rune r rune
w int exp int
}{ }{
{' ', 1}, {' ', 1},
{'a', 1}, {'a', 1},
@@ -40,17 +40,17 @@ func TestRuneWidth(t *testing.T) {
{'界', 2}, {'界', 2},
} }
for _, char := range chars { for _, test := range tests {
if w := runeWidth(char.r); w != char.w { if got := runeWidth(test.r); got != test.exp {
t.Errorf("at input '%c' expected '%d' but got '%d'", char.r, char.w, w) t.Errorf("at input '%c' expected '%d' but got '%d'", test.r, test.exp, got)
} }
} }
} }
func TestRuneSliceWidth(t *testing.T) { func TestRuneSliceWidth(t *testing.T) {
slices := []struct { tests := []struct {
s []rune rs []rune
w int exp int
}{ }{
{[]rune{'a', 'b'}, 2}, {[]rune{'a', 'b'}, 2},
{[]rune{'ı', 'ş'}, 2}, {[]rune{'ı', 'ş'}, 2},
@@ -58,19 +58,19 @@ func TestRuneSliceWidth(t *testing.T) {
{[]rune{'世', 'a', '界', 'ı'}, 6}, {[]rune{'世', 'a', '界', 'ı'}, 6},
} }
for _, slice := range slices { for _, test := range tests {
if w := runeSliceWidth(slice.s); w != slice.w { if got := runeSliceWidth(test.rs); got != test.exp {
t.Errorf("at input '%v' expected '%d' but got '%d'", slice.s, slice.w, w) t.Errorf("at input '%v' expected '%d' but got '%d'", test.rs, test.exp, got)
} }
} }
} }
func TestRuneSliceWidthRange(t *testing.T) { func TestRuneSliceWidthRange(t *testing.T) {
slices := []struct { tests := []struct {
s []rune rs []rune
i int beg int
j int end int
r []rune exp []rune
}{ }{
{[]rune{'a', 'b', 'c', 'd'}, 1, 3, []rune{'b', 'c'}}, {[]rune{'a', 'b', 'c', 'd'}, 1, 3, []rune{'b', 'c'}},
{[]rune{'a', 'ı', 'b', 'ş'}, 1, 3, []rune{'ı', 'b'}}, {[]rune{'a', 'ı', 'b', 'ş'}, 1, 3, []rune{'ı', 'b'}},
@@ -84,17 +84,17 @@ func TestRuneSliceWidthRange(t *testing.T) {
{[]rune{'世', 'a', '界', 'ı'}, 3, 4, []rune{}}, {[]rune{'世', 'a', '界', 'ı'}, 3, 4, []rune{}},
} }
for _, slice := range slices { for _, test := range tests {
if r := runeSliceWidthRange(slice.s, slice.i, slice.j); !reflect.DeepEqual(r, slice.r) { if got := runeSliceWidthRange(test.rs, test.beg, test.end); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", slice.s, slice.r, r) t.Errorf("at input '%v' expected '%v' but got '%v'", test.rs, test.rs, got)
} }
} }
} }
func TestEscape(t *testing.T) { func TestEscape(t *testing.T) {
strs := []struct { tests := []struct {
s string s string
e string exp string
}{ }{
{"", ""}, {"", ""},
{"foo", "foo"}, {"foo", "foo"},
@@ -104,17 +104,17 @@ func TestEscape(t *testing.T) {
{`foo\bar`, `foo\bar`}, {`foo\bar`, `foo\bar`},
} }
for _, str := range strs { for _, test := range tests {
if e := escape(str.s); !reflect.DeepEqual(e, str.e) { if got := escape(test.s); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.e, e) t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
} }
} }
} }
func TestUnescape(t *testing.T) { func TestUnescape(t *testing.T) {
strs := []struct { tests := []struct {
s string s string
u string exp string
}{ }{
{"", ""}, {"", ""},
{"foo", "foo"}, {"foo", "foo"},
@@ -123,17 +123,17 @@ func TestUnescape(t *testing.T) {
{`foo\ \ bar`, "foo bar"}, {`foo\ \ bar`, "foo bar"},
} }
for _, str := range strs { for _, test := range tests {
if u := unescape(str.s); !reflect.DeepEqual(u, str.u) { if got := unescape(test.s); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.u, u) t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
} }
} }
} }
func TestTokenize(t *testing.T) { func TestTokenize(t *testing.T) {
strs := []struct { tests := []struct {
s string s string
toks []string exp []string
}{ }{
{"", []string{""}}, {"", []string{""}},
{"foo", []string{"foo"}}, {"foo", []string{"foo"}},
@@ -141,15 +141,15 @@ func TestTokenize(t *testing.T) {
{`:rename foo\ bar`, []string{":rename", `foo\ bar`}}, {`:rename foo\ bar`, []string{":rename", `foo\ bar`}},
} }
for _, str := range strs { for _, test := range tests {
if toks := tokenize(str.s); !reflect.DeepEqual(toks, str.toks) { if got := tokenize(test.s); !reflect.DeepEqual(got, test.exp) {
t.Errorf("at input '%v' expected '%v' but got '%v'", str.s, str.toks, toks) t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
} }
} }
} }
func TestSplitWord(t *testing.T) { func TestSplitWord(t *testing.T) {
strs := []struct { tests := []struct {
s string s string
word string word string
rest string rest string
@@ -165,17 +165,17 @@ func TestSplitWord(t *testing.T) {
{" foo bar baz", "foo", "bar baz"}, {" foo bar baz", "foo", "bar baz"},
} }
for _, str := range strs { for _, test := range tests {
if w, r := splitWord(str.s); w != str.word || r != str.rest { if w, r := splitWord(test.s); w != test.word || r != test.rest {
t.Errorf("at input '%s' expected '%s' and '%s' but got '%s' and '%s'", str.s, str.word, str.rest, w, r) t.Errorf("at input '%s' expected '%s' and '%s' but got '%s' and '%s'", test.s, test.word, test.rest, w, r)
} }
} }
} }
func TestHumanize(t *testing.T) { func TestHumanize(t *testing.T) {
nums := []struct { tests := []struct {
i int64 i int64
s string exp string
}{ }{
{0, "0"}, {0, "0"},
{9, "9"}, {9, "9"},
@@ -194,18 +194,18 @@ func TestHumanize(t *testing.T) {
{1000000, "1.0M"}, {1000000, "1.0M"},
} }
for _, num := range nums { for _, test := range tests {
if h := humanize(num.i); h != num.s { if got := humanize(test.i); got != test.exp {
t.Errorf("at input '%d' expected '%s' but got '%s'", num.i, num.s, h) t.Errorf("at input '%d' expected '%s' but got '%s'", test.i, test.exp, got)
} }
} }
} }
func TestNaturalLess(t *testing.T) { func TestNaturalLess(t *testing.T) {
tests := []struct { tests := []struct {
s1 string s1 string
s2 string s2 string
expected bool exp bool
}{ }{
{"foo", "bar", false}, {"foo", "bar", false},
{"bar", "baz", true}, {"bar", "baz", true},
@@ -222,8 +222,8 @@ func TestNaturalLess(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if b := naturalLess(test.s1, test.s2); b != test.expected { if got := naturalLess(test.s1, test.s2); got != test.exp {
t.Errorf("at input '%s' and '%s' expected '%t' but got '%t'", test.s1, test.s2, test.expected, b) t.Errorf("at input '%s' and '%s' expected '%t' but got '%t'", test.s1, test.s2, test.exp, got)
} }
} }
} }
+3 -7
View File
@@ -11,7 +11,7 @@ import (
"strings" "strings"
) )
type linkState int8 type linkState byte
const ( const (
notLink linkState = iota notLink linkState = iota
@@ -298,16 +298,12 @@ func (nav *nav) updir() error {
return nil return nil
} }
var errNotDir = fmt.Errorf("not a directory")
func (nav *nav) open() error { func (nav *nav) open() error {
curr, err := nav.currFile() curr, err := nav.currFile()
if err != nil { if err != nil {
return err return fmt.Errorf("open: %s", err)
}
if !curr.IsDir() {
return errNotDir
} }
path := curr.Path path := curr.Path
dir := newDir(path) dir := newDir(path)
+1 -1
View File
@@ -7,7 +7,7 @@ import (
) )
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
for _, test := range gTests { for _, test := range gEvalTests {
p := newParser(strings.NewReader(test.inp)) p := newParser(strings.NewReader(test.inp))
for _, expr := range test.exprs { for _, expr := range test.exprs {
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"unicode" "unicode"
) )
type tokenType int type tokenType byte
const ( const (
tokenEOF tokenType = iota tokenEOF tokenType = iota
+1 -1
View File
@@ -6,7 +6,7 @@ import (
) )
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
for _, test := range gTests { for _, test := range gEvalTests {
s := newScanner(strings.NewReader(test.inp)) s := newScanner(strings.NewReader(test.inp))
for _, tok := range test.toks { for _, tok := range test.toks {
+179 -185
View File
@@ -21,6 +21,28 @@ import (
const gEscapeCode = 27 const gEscapeCode = 27
var gAnsiCodes = map[int]termbox.Attribute{
1: termbox.AttrBold,
4: termbox.AttrUnderline,
7: termbox.AttrReverse,
30: termbox.ColorBlack,
31: termbox.ColorRed,
32: termbox.ColorGreen,
33: termbox.ColorYellow,
34: termbox.ColorBlue,
35: termbox.ColorMagenta,
36: termbox.ColorCyan,
37: termbox.ColorWhite,
40: termbox.ColorBlack,
41: termbox.ColorRed,
42: termbox.ColorGreen,
43: termbox.ColorYellow,
44: termbox.ColorBlue,
45: termbox.ColorMagenta,
46: termbox.ColorCyan,
47: termbox.ColorWhite,
}
var gKeyVal = map[termbox.Key][]rune{ var gKeyVal = map[termbox.Key][]rune{
termbox.KeyF1: []rune{'<', 'f', '-', '1', '>'}, termbox.KeyF1: []rune{'<', 'f', '-', '1', '>'},
termbox.KeyF2: []rune{'<', 'f', '-', '2', '>'}, termbox.KeyF2: []rune{'<', 'f', '-', '2', '>'},
@@ -114,7 +136,6 @@ func (win *win) print(x, y int, fg, bg termbox.Attribute, s string) {
if r == gEscapeCode && i+1 < len(s) && s[i+1] == '[' { if r == gEscapeCode && i+1 < len(s) && s[i+1] == '[' {
j := strings.IndexByte(s[i:min(len(s), i+32)], 'm') j := strings.IndexByte(s[i:min(len(s), i+32)], 'm')
if j == -1 { if j == -1 {
continue continue
} }
@@ -146,45 +167,16 @@ func (win *win) print(x, y int, fg, bg termbox.Attribute, s string) {
} }
for _, n := range nums { for _, n := range nums {
switch n { attr, ok := gAnsiCodes[n]
case 1: if !ok {
fg = fg | termbox.AttrBold log.Printf("unknown ansi code: %d", n)
case 4: continue
fg = fg | termbox.AttrUnderline }
case 7: if 1 <= n && n <= 37 {
fg = fg | termbox.AttrReverse fg = fg | attr
case 30: }
fg = fg | termbox.ColorBlack if 40 <= n && n <= 47 {
case 31: bg = bg | attr
fg = fg | termbox.ColorRed
case 32:
fg = fg | termbox.ColorGreen
case 33:
fg = fg | termbox.ColorYellow
case 34:
fg = fg | termbox.ColorBlue
case 35:
fg = fg | termbox.ColorMagenta
case 36:
fg = fg | termbox.ColorCyan
case 37:
fg = fg | termbox.ColorWhite
case 40:
bg = bg | termbox.ColorBlack
case 41:
bg = bg | termbox.ColorRed
case 42:
bg = bg | termbox.ColorGreen
case 43:
bg = bg | termbox.ColorYellow
case 44:
bg = bg | termbox.ColorBlue
case 45:
bg = bg | termbox.ColorMagenta
case 46:
bg = bg | termbox.ColorCyan
case 47:
bg = bg | termbox.ColorWhite
} }
} }
@@ -225,7 +217,7 @@ func (win *win) printd(dir *dir, marks, saves map[string]bool) {
if len(dir.fi) == 0 { if len(dir.fi) == 0 {
fg = termbox.AttrBold fg = termbox.AttrBold
win.print(0, 0, fg, bg, "empty") win.print(2, 0, fg, bg, "empty")
return return
} }
@@ -236,15 +228,13 @@ func (win *win) printd(dir *dir, marks, saves map[string]bool) {
for i, f := range dir.fi[beg:end] { for i, f := range dir.fi[beg:end] {
switch { switch {
case f.LinkState != notLink: case f.LinkState == working:
if f.LinkState == working { fg = termbox.ColorCyan
fg = termbox.ColorCyan if f.Mode().IsDir() {
if f.Mode().IsDir() { fg |= termbox.AttrBold
fg |= termbox.AttrBold
}
} else {
fg = termbox.ColorMagenta
} }
case f.LinkState == broken:
fg = termbox.ColorMagenta
case f.Mode().IsRegular(): case f.Mode().IsRegular():
if f.Mode()&0111 != 0 { if f.Mode()&0111 != 0 {
fg = termbox.AttrBold | termbox.ColorGreen fg = termbox.AttrBold | termbox.ColorGreen
@@ -343,11 +333,13 @@ type ui struct {
dirprev *dir dirprev *dir
keychan chan string keychan chan string
evschan chan termbox.Event evschan chan termbox.Event
menubuf *bytes.Buffer
cmdpref string cmdpref string
cmdlacc []rune cmdlacc []rune
cmdracc []rune cmdracc []rune
cmdbuf []rune cmdbuf []rune
menubuf *bytes.Buffer keyacc []rune
keycnt []rune
} }
func getWidths(wtot int) []int { func getWidths(wtot int) []int {
@@ -481,7 +473,7 @@ func (ui *ui) loadFile(nav *nav) {
for i := 0; i < nav.height && buf.Scan(); i++ { for i := 0; i < nav.height && buf.Scan(); i++ {
for _, r := range buf.Text() { for _, r := range buf.Text() {
if r == 0 { if r == 0 {
ui.regprev = []string{"binary"} ui.regprev = []string{"\033[1mbinary\033[0m"}
return return
} }
} }
@@ -556,8 +548,8 @@ func (ui *ui) draw(nav *nav) {
ui.menuwin.printl(0, 0, termbox.AttrBold, termbox.AttrBold, lines[0]) ui.menuwin.printl(0, 0, termbox.AttrBold, termbox.AttrBold, lines[0])
for i, line := range lines[1:] { for i, line := range lines[1:] {
ui.menuwin.printl(0, i+1, termbox.ColorDefault, termbox.ColorDefault, "") ui.menuwin.printl(0, i+1, fg, bg, "")
ui.menuwin.print(0, i+1, termbox.ColorDefault, termbox.ColorDefault, line) ui.menuwin.print(0, i+1, fg, bg, line)
} }
} }
@@ -565,7 +557,7 @@ func (ui *ui) draw(nav *nav) {
if ui.cmdpref == "" { if ui.cmdpref == "" {
// leave the cursor at the beginning of the current file for screen readers // leave the cursor at the beginning of the current file for screen readers
fmt.Printf("[%d;%dH", ui.wins[woff+length-1].y+nav.dirs[doff+length-1].pos+1, ui.wins[woff+length-1].x+1) fmt.Printf("\033[%d;%dH", ui.wins[woff+length-1].y+nav.dirs[doff+length-1].pos+1, ui.wins[woff+length-1].x+1)
} }
} }
@@ -638,148 +630,150 @@ type multiExpr struct {
count int count int
} }
// This function is used to read expressions on the client side. Digits are func readCmdEvent(ch chan<- multiExpr, ev termbox.Event) {
// interpreted as command counts but this is only done for digits preceding any if ev.Ch != 0 {
// non-digit characters (e.g. "42y2k" as 42 times "y2k"). ch <- multiExpr{&callExpr{"cmd-insert", []string{string(ev.Ch)}}, 1}
func (ui *ui) readExpr() chan multiExpr { } else {
ch := make(chan multiExpr) // TODO: rest of the keys
switch ev.Key {
case termbox.KeyEsc:
ch <- multiExpr{&callExpr{"cmd-escape", nil}, 1}
case termbox.KeySpace:
ch <- multiExpr{&callExpr{"cmd-insert", []string{" "}}, 1}
case termbox.KeyTab:
ch <- multiExpr{&callExpr{"cmd-comp", nil}, 1}
case termbox.KeyEnter, termbox.KeyCtrlJ:
ch <- multiExpr{&callExpr{"cmd-enter", nil}, 1}
case termbox.KeyBackspace, termbox.KeyBackspace2:
ch <- multiExpr{&callExpr{"cmd-delete-back", nil}, 1}
case termbox.KeyDelete, termbox.KeyCtrlD:
ch <- multiExpr{&callExpr{"cmd-delete", nil}, 1}
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
ch <- multiExpr{&callExpr{"cmd-left", nil}, 1}
case termbox.KeyArrowRight, termbox.KeyCtrlF:
ch <- multiExpr{&callExpr{"cmd-right", nil}, 1}
case termbox.KeyHome, termbox.KeyCtrlA:
ch <- multiExpr{&callExpr{"cmd-beg", nil}, 1}
case termbox.KeyEnd, termbox.KeyCtrlE:
ch <- multiExpr{&callExpr{"cmd-end", nil}, 1}
case termbox.KeyCtrlK:
ch <- multiExpr{&callExpr{"cmd-delete-end", nil}, 1}
case termbox.KeyCtrlU:
ch <- multiExpr{&callExpr{"cmd-delete-beg", nil}, 1}
case termbox.KeyCtrlW:
ch <- multiExpr{&callExpr{"cmd-delete-word", nil}, 1}
case termbox.KeyCtrlY:
ch <- multiExpr{&callExpr{"cmd-put", nil}, 1}
case termbox.KeyCtrlT:
ch <- multiExpr{&callExpr{"cmd-transpose", nil}, 1}
}
}
}
func (ui *ui) readEvent(ch chan<- multiExpr, ev termbox.Event) {
renew := &callExpr{"renew", nil} renew := &callExpr{"renew", nil}
count := 1 count := 1
var acc []rune switch ev.Type {
var cnt []rune case termbox.EventKey:
if ev.Ch != 0 {
switch {
case ev.Ch == '<':
ui.keyacc = append(ui.keyacc, '<', 'l', 't', '>')
case ev.Ch == '>':
ui.keyacc = append(ui.keyacc, '<', 'g', 't', '>')
case unicode.IsDigit(ev.Ch) && len(ui.keyacc) == 0:
ui.keycnt = append(ui.keycnt, ev.Ch)
default:
ui.keyacc = append(ui.keyacc, ev.Ch)
}
} else {
val := gKeyVal[ev.Key]
if string(val) == "<esc>" {
ch <- multiExpr{renew, 1}
ui.keyacc = nil
ui.keycnt = nil
}
ui.keyacc = append(ui.keyacc, val...)
}
binds, ok := findBinds(gOpts.keys, string(ui.keyacc))
switch len(binds) {
case 0:
ui.message = fmt.Sprintf("unknown mapping: %s", string(ui.keyacc))
ch <- multiExpr{renew, 1}
ui.keyacc = nil
ui.keycnt = nil
case 1:
if ok {
if len(ui.keycnt) > 0 {
c, err := strconv.Atoi(string(ui.keycnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
} else {
count = 1
}
expr := gOpts.keys[string(ui.keyacc)]
ch <- multiExpr{expr, count}
ui.keyacc = nil
ui.keycnt = nil
}
if len(ui.keyacc) > 0 {
ui.menubuf = listBinds(binds)
ch <- multiExpr{renew, 1}
} else if ui.menubuf != nil {
ui.menubuf = nil
}
default:
if ok {
// TODO: use a delay
if len(ui.keycnt) > 0 {
c, err := strconv.Atoi(string(ui.keycnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
} else {
count = 1
}
expr := gOpts.keys[string(ui.keyacc)]
ch <- multiExpr{expr, count}
ui.keyacc = nil
ui.keycnt = nil
}
if len(ui.keyacc) > 0 {
ui.menubuf = listBinds(binds)
ch <- multiExpr{renew, 1}
} else {
ui.menubuf = nil
}
}
case termbox.EventResize:
ch <- multiExpr{renew, 1}
default:
// TODO: handle other events
}
}
// This function is used to read expressions on the client side. Digits are
// interpreted as command counts but this is only done for digits preceding any
// non-digit characters (e.g. "42y2k" as 42 times "y2k").
func (ui *ui) readExpr() <-chan multiExpr {
ch := make(chan multiExpr)
go func() { go func() {
for { for {
ev := ui.pollEvent() ev := ui.pollEvent()
if ui.cmdpref != "" { if ui.cmdpref != "" && ev.Type == termbox.EventKey {
switch ev.Type { readCmdEvent(ch, ev)
case termbox.EventKey: continue
if ev.Ch != 0 {
ch <- multiExpr{&callExpr{"cmd-insert", []string{string(ev.Ch)}}, 1}
} else {
// TODO: rest of the keys
switch ev.Key {
case termbox.KeyEsc:
ch <- multiExpr{&callExpr{"cmd-escape", nil}, 1}
case termbox.KeySpace:
ch <- multiExpr{&callExpr{"cmd-insert", []string{" "}}, 1}
case termbox.KeyTab:
ch <- multiExpr{&callExpr{"cmd-comp", nil}, 1}
case termbox.KeyEnter, termbox.KeyCtrlJ:
ch <- multiExpr{&callExpr{"cmd-enter", nil}, 1}
case termbox.KeyBackspace, termbox.KeyBackspace2:
ch <- multiExpr{&callExpr{"cmd-delete-back", nil}, 1}
case termbox.KeyDelete, termbox.KeyCtrlD:
ch <- multiExpr{&callExpr{"cmd-delete", nil}, 1}
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
ch <- multiExpr{&callExpr{"cmd-left", nil}, 1}
case termbox.KeyArrowRight, termbox.KeyCtrlF:
ch <- multiExpr{&callExpr{"cmd-right", nil}, 1}
case termbox.KeyHome, termbox.KeyCtrlA:
ch <- multiExpr{&callExpr{"cmd-beg", nil}, 1}
case termbox.KeyEnd, termbox.KeyCtrlE:
ch <- multiExpr{&callExpr{"cmd-end", nil}, 1}
case termbox.KeyCtrlK:
ch <- multiExpr{&callExpr{"cmd-delete-end", nil}, 1}
case termbox.KeyCtrlU:
ch <- multiExpr{&callExpr{"cmd-delete-beg", nil}, 1}
case termbox.KeyCtrlW:
ch <- multiExpr{&callExpr{"cmd-delete-word", nil}, 1}
case termbox.KeyCtrlY:
ch <- multiExpr{&callExpr{"cmd-put", nil}, 1}
case termbox.KeyCtrlT:
ch <- multiExpr{&callExpr{"cmd-transpose", nil}, 1}
}
}
continue
}
} }
switch ev.Type { ui.readEvent(ch, ev)
case termbox.EventKey:
if ev.Ch != 0 {
switch {
case ev.Ch == '<':
acc = append(acc, '<', 'l', 't', '>')
case ev.Ch == '>':
acc = append(acc, '<', 'g', 't', '>')
case unicode.IsDigit(ev.Ch) && len(acc) == 0:
cnt = append(cnt, ev.Ch)
default:
acc = append(acc, ev.Ch)
}
} else {
val := gKeyVal[ev.Key]
if string(val) == "<esc>" {
ch <- multiExpr{renew, 1}
acc = nil
cnt = nil
}
acc = append(acc, val...)
}
binds, ok := findBinds(gOpts.keys, string(acc))
switch len(binds) {
case 0:
ui.message = fmt.Sprintf("unknown mapping: %s", string(acc))
ch <- multiExpr{renew, 1}
acc = nil
cnt = nil
case 1:
if ok {
if len(cnt) > 0 {
c, err := strconv.Atoi(string(cnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
} else {
count = 1
}
expr := gOpts.keys[string(acc)]
ch <- multiExpr{expr, count}
acc = nil
cnt = nil
}
if len(acc) > 0 {
ui.menubuf = listBinds(binds)
ch <- multiExpr{renew, 1}
} else if ui.menubuf != nil {
ui.menubuf = nil
}
default:
if ok {
// TODO: use a delay
if len(cnt) > 0 {
c, err := strconv.Atoi(string(cnt))
if err != nil {
log.Printf("converting command count: %s", err)
}
count = c
} else {
count = 1
}
expr := gOpts.keys[string(acc)]
ch <- multiExpr{expr, count}
acc = nil
cnt = nil
}
if len(acc) > 0 {
ui.menubuf = listBinds(binds)
ch <- multiExpr{renew, 1}
} else {
ui.menubuf = nil
}
}
case termbox.EventResize:
ch <- multiExpr{renew, 1}
default:
// TODO: handle other events
}
} }
}() }()