use lowercase initials for type names
This commit is contained in:
1 parent
8ae5f838c7
commit
e532db082f
10 files changed
+292
-292
No files matched your search
@@ -10,18 +10,18 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type app struct {
|
||||||
ui *UI
|
ui *ui
|
||||||
nav *Nav
|
nav *nav
|
||||||
quit chan bool
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newApp() *App {
|
func newApp() *app {
|
||||||
ui := newUI()
|
ui := newUI()
|
||||||
nav := newNav(ui.wins[0].h)
|
nav := newNav(ui.wins[0].h)
|
||||||
quit := make(chan bool, 1)
|
quit := make(chan bool, 1)
|
||||||
|
|
||||||
return &App{
|
return &app{
|
||||||
ui: ui,
|
ui: ui,
|
||||||
nav: nav,
|
nav: nav,
|
||||||
quit: quit,
|
quit: quit,
|
||||||
@@ -55,10 +55,10 @@ func waitKey() error {
|
|||||||
// This is the main event loop of the application. There are two channels to
|
// This is the main event loop of the application. There are two channels to
|
||||||
// read expressions from client and server. Reading and evaluation are done on
|
// read expressions from client and server. Reading and evaluation are done on
|
||||||
// separate goroutines.
|
// separate goroutines.
|
||||||
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 {
|
||||||
@@ -102,7 +102,7 @@ func (app *App) handleInp() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) exportVars() {
|
func (app *app) exportVars() {
|
||||||
var envFile string
|
var envFile string
|
||||||
if f, err := app.nav.currFile(); err == nil {
|
if f, err := app.nav.currFile(); err == nil {
|
||||||
envFile = f.Path
|
envFile = f.Path
|
||||||
@@ -121,7 +121,7 @@ func (app *App) exportVars() {
|
|||||||
os.Setenv("fx", envFiles)
|
os.Setenv("fx", envFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Setenv("id", strconv.Itoa(gClientId))
|
os.Setenv("id", strconv.Itoa(gClientID))
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is used to run a command in shell. Following modes are used:
|
// This function is used to run a command in shell. Following modes are used:
|
||||||
@@ -132,7 +132,7 @@ func (app *App) exportVars() {
|
|||||||
// & No Yes No Do nothing
|
// & No Yes No Do nothing
|
||||||
//
|
//
|
||||||
// Waiting async commands are not used for now.
|
// Waiting async commands are not used for now.
|
||||||
func (app *App) runShell(s string, args []string, wait bool, async bool) {
|
func (app *app) runShell(s string, args []string, wait bool, async bool) {
|
||||||
app.exportVars()
|
app.exportVars()
|
||||||
|
|
||||||
if len(gOpts.ifs) != 0 {
|
if len(gOpts.ifs) != 0 {
|
||||||
|
|||||||
@@ -64,11 +64,11 @@ 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() {
|
||||||
fmt.Fprintf(c, "conn %d\n", gClientId)
|
fmt.Fprintf(c, "conn %d\n", gClientID)
|
||||||
|
|
||||||
s := bufio.NewScanner(c)
|
s := bufio.NewScanner(c)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *SetExpr) eval(app *App, args []string) {
|
func (e *setExpr) eval(app *app, args []string) {
|
||||||
switch e.opt {
|
switch e.opt {
|
||||||
case "dirfirst":
|
case "dirfirst":
|
||||||
gOpts.dirfirst = true
|
gOpts.dirfirst = true
|
||||||
@@ -117,7 +117,7 @@ func (e *SetExpr) eval(app *App, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *MapExpr) eval(app *App, args []string) {
|
func (e *mapExpr) eval(app *app, args []string) {
|
||||||
if e.expr == nil {
|
if e.expr == nil {
|
||||||
delete(gOpts.keys, e.keys)
|
delete(gOpts.keys, e.keys)
|
||||||
return
|
return
|
||||||
@@ -125,7 +125,7 @@ func (e *MapExpr) eval(app *App, args []string) {
|
|||||||
gOpts.keys[e.keys] = e.expr
|
gOpts.keys[e.keys] = e.expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *CmdExpr) eval(app *App, args []string) {
|
func (e *cmdExpr) eval(app *app, args []string) {
|
||||||
if e.expr == nil {
|
if e.expr == nil {
|
||||||
delete(gOpts.cmds, e.name)
|
delete(gOpts.cmds, e.name)
|
||||||
return
|
return
|
||||||
@@ -152,7 +152,7 @@ func splitKeys(s string) (keys []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *CallExpr) eval(app *App, args []string) {
|
func (e *callExpr) eval(app *app, args []string) {
|
||||||
// TODO: check for extra toks in each case
|
// TODO: check for extra toks in each case
|
||||||
switch e.name {
|
switch e.name {
|
||||||
case "up":
|
case "up":
|
||||||
@@ -194,7 +194,7 @@ func (e *CallExpr) eval(app *App, args []string) {
|
|||||||
app.ui.loadFileInfo(app.nav)
|
app.ui.loadFileInfo(app.nav)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != ErrNotDir {
|
if err != errNotDir {
|
||||||
app.ui.message = err.Error()
|
app.ui.message = err.Error()
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return
|
return
|
||||||
@@ -445,7 +445,7 @@ func (e *CallExpr) eval(app *App, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ExecExpr) eval(app *App, args []string) {
|
func (e *execExpr) eval(app *app, args []string) {
|
||||||
switch e.pref {
|
switch e.pref {
|
||||||
case "$":
|
case "$":
|
||||||
log.Printf("shell: %s -- %s", e, args)
|
log.Printf("shell: %s -- %s", e, args)
|
||||||
@@ -467,7 +467,7 @@ func (e *ExecExpr) eval(app *App, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ListExpr) eval(app *App, args []string) {
|
func (e *listExpr) eval(app *app, args []string) {
|
||||||
for _, expr := range e.exprs {
|
for _, expr := range e.exprs {
|
||||||
expr.eval(app, nil)
|
expr.eval(app, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-44
@@ -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,9 +248,9 @@ 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,9 +263,9 @@ 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"
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gClientId int
|
gClientID int
|
||||||
gLastDirPath string
|
gLastDirPath string
|
||||||
gSelectionPath string
|
gSelectionPath string
|
||||||
gSocketPath string
|
gSocketPath string
|
||||||
@@ -51,11 +51,11 @@ func init() {
|
|||||||
|
|
||||||
gSocketPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.sock", envUser))
|
gSocketPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.sock", envUser))
|
||||||
|
|
||||||
gClientId = 1000
|
gClientID = 1000
|
||||||
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientId))
|
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
|
||||||
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
|
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
|
||||||
gClientId++
|
gClientID++
|
||||||
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientId))
|
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
|
||||||
}
|
}
|
||||||
|
|
||||||
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
|
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
|
||||||
|
|||||||
@@ -11,35 +11,35 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinkState int8
|
type linkState int8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NotLink LinkState = iota
|
notLink linkState = iota
|
||||||
Working
|
working
|
||||||
Broken
|
broken
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type file struct {
|
||||||
os.FileInfo
|
os.FileInfo
|
||||||
LinkState LinkState
|
LinkState linkState
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
type FilesSortable struct {
|
type filesSortable struct {
|
||||||
files []*File
|
files []*file
|
||||||
less func(i, j int) bool
|
less func(i, j int) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FilesSortable) Len() int { return len(f.files) }
|
func (f filesSortable) Len() int { return len(f.files) }
|
||||||
func (f FilesSortable) Swap(i, j int) { f.files[i], f.files[j] = f.files[j], f.files[i] }
|
func (f filesSortable) Swap(i, j int) { f.files[i], f.files[j] = f.files[j], f.files[i] }
|
||||||
func (f FilesSortable) Less(i, j int) bool { return f.less(i, j) }
|
func (f filesSortable) Less(i, j int) bool { return f.less(i, j) }
|
||||||
|
|
||||||
// TODO: Replace with `sort.SliceStable` once available
|
// TODO: Replace with `sort.SliceStable` once available
|
||||||
func sortFilesStable(files []*File, less func(i, j int) bool) {
|
func sortFilesStable(files []*file, less func(i, j int) bool) {
|
||||||
sort.Stable(FilesSortable{files: files, less: less})
|
sort.Stable(filesSortable{files: files, less: less})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilesSorted(path string) []*File {
|
func getFilesSorted(path string) []*file {
|
||||||
fi, err := readdir(path)
|
fi, err := readdir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("reading directory: %s", err)
|
log.Printf("reading directory: %s", err)
|
||||||
@@ -78,14 +78,14 @@ func getFilesSorted(path string) []*File {
|
|||||||
return fi
|
return fi
|
||||||
}
|
}
|
||||||
|
|
||||||
func readdir(path string) ([]*File, error) {
|
func readdir(path string) ([]*file, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
names, err := f.Readdirnames(-1)
|
names, err := f.Readdirnames(-1)
|
||||||
fi := make([]*File, 0, len(names))
|
fi := make([]*file, 0, len(names))
|
||||||
for _, filename := range names {
|
for _, filename := range names {
|
||||||
if !gOpts.hidden && filename[0] == '.' {
|
if !gOpts.hidden && filename[0] == '.' {
|
||||||
continue
|
continue
|
||||||
@@ -101,20 +101,20 @@ func readdir(path string) ([]*File, error) {
|
|||||||
return fi, lerr
|
return fi, lerr
|
||||||
}
|
}
|
||||||
|
|
||||||
var linkState LinkState
|
var linkState linkState
|
||||||
|
|
||||||
if lstat.Mode()&os.ModeSymlink != 0 {
|
if lstat.Mode()&os.ModeSymlink != 0 {
|
||||||
stat, serr := os.Stat(fpath)
|
stat, serr := os.Stat(fpath)
|
||||||
if serr == nil {
|
if serr == nil {
|
||||||
linkState = Working
|
linkState = working
|
||||||
lstat = stat
|
lstat = stat
|
||||||
} else {
|
} else {
|
||||||
linkState = Broken
|
linkState = broken
|
||||||
log.Printf("getting link destination info: %s", serr)
|
log.Printf("getting link destination info: %s", serr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fi = append(fi, &File{
|
fi = append(fi, &file{
|
||||||
FileInfo: lstat,
|
FileInfo: lstat,
|
||||||
LinkState: linkState,
|
LinkState: linkState,
|
||||||
Path: fpath,
|
Path: fpath,
|
||||||
@@ -123,21 +123,21 @@ func readdir(path string) ([]*File, error) {
|
|||||||
return fi, err
|
return fi, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Dir struct {
|
type dir struct {
|
||||||
ind int // which entry is highlighted
|
ind int // which entry is highlighted
|
||||||
pos int // which line in the ui highlighted entry is
|
pos int // which line in the ui highlighted entry is
|
||||||
path string
|
path string
|
||||||
fi []*File
|
fi []*file
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDir(path string) *Dir {
|
func newDir(path string) *dir {
|
||||||
return &Dir{
|
return &dir{
|
||||||
path: path,
|
path: path,
|
||||||
fi: getFilesSorted(path),
|
fi: getFilesSorted(path),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dir *Dir) renew(height int) {
|
func (dir *dir) renew(height int) {
|
||||||
var name string
|
var name string
|
||||||
if len(dir.fi) != 0 {
|
if len(dir.fi) != 0 {
|
||||||
name = dir.fi[dir.ind].Name()
|
name = dir.fi[dir.ind].Name()
|
||||||
@@ -148,7 +148,7 @@ func (dir *Dir) renew(height int) {
|
|||||||
dir.load(dir.ind, dir.pos, height, name)
|
dir.load(dir.ind, dir.pos, height, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dir *Dir) load(ind, pos, height int, name string) {
|
func (dir *dir) load(ind, pos, height int, name string) {
|
||||||
if len(dir.fi) == 0 {
|
if len(dir.fi) == 0 {
|
||||||
dir.ind, dir.pos = 0, 0
|
dir.ind, dir.pos = 0, 0
|
||||||
return
|
return
|
||||||
@@ -172,8 +172,8 @@ func (dir *Dir) load(ind, pos, height int, name string) {
|
|||||||
dir.pos = pos
|
dir.pos = pos
|
||||||
}
|
}
|
||||||
|
|
||||||
type Nav struct {
|
type nav struct {
|
||||||
dirs []*Dir
|
dirs []*dir
|
||||||
inds map[string]int
|
inds map[string]int
|
||||||
poss map[string]int
|
poss map[string]int
|
||||||
names map[string]string
|
names map[string]string
|
||||||
@@ -182,8 +182,8 @@ type Nav struct {
|
|||||||
height int
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDirs(wd string, height int) []*Dir {
|
func getDirs(wd string, height int) []*dir {
|
||||||
var dirs []*Dir
|
var dirs []*dir
|
||||||
|
|
||||||
for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) {
|
for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) {
|
||||||
dir := newDir(curr)
|
dir := newDir(curr)
|
||||||
@@ -205,7 +205,7 @@ func getDirs(wd string, height int) []*Dir {
|
|||||||
return dirs
|
return dirs
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNav(height int) *Nav {
|
func newNav(height int) *nav {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("getting current directory: %s", err)
|
log.Printf("getting current directory: %s", err)
|
||||||
@@ -213,7 +213,7 @@ func newNav(height int) *Nav {
|
|||||||
|
|
||||||
dirs := getDirs(wd, height)
|
dirs := getDirs(wd, height)
|
||||||
|
|
||||||
return &Nav{
|
return &nav{
|
||||||
dirs: dirs,
|
dirs: dirs,
|
||||||
inds: make(map[string]int),
|
inds: make(map[string]int),
|
||||||
poss: make(map[string]int),
|
poss: make(map[string]int),
|
||||||
@@ -224,7 +224,7 @@ func newNav(height int) *Nav {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) renew(height int) {
|
func (nav *nav) renew(height int) {
|
||||||
nav.height = height
|
nav.height = height
|
||||||
for _, d := range nav.dirs {
|
for _, d := range nav.dirs {
|
||||||
d.renew(nav.height)
|
d.renew(nav.height)
|
||||||
@@ -237,7 +237,7 @@ func (nav *Nav) renew(height int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) up(dist int) {
|
func (nav *nav) up(dist int) {
|
||||||
dir := nav.currDir()
|
dir := nav.currDir()
|
||||||
|
|
||||||
if dir.ind == 0 {
|
if dir.ind == 0 {
|
||||||
@@ -252,7 +252,7 @@ func (nav *Nav) up(dist int) {
|
|||||||
dir.pos = max(dir.pos, edge)
|
dir.pos = max(dir.pos, edge)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) down(dist int) {
|
func (nav *nav) down(dist int) {
|
||||||
dir := nav.currDir()
|
dir := nav.currDir()
|
||||||
|
|
||||||
maxind := len(dir.fi) - 1
|
maxind := len(dir.fi) - 1
|
||||||
@@ -275,7 +275,7 @@ func (nav *Nav) down(dist int) {
|
|||||||
dir.pos = min(dir.pos, maxind)
|
dir.pos = min(dir.pos, maxind)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) updir() error {
|
func (nav *nav) updir() error {
|
||||||
if len(nav.dirs) <= 1 {
|
if len(nav.dirs) <= 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -298,15 +298,15 @@ func (nav *Nav) updir() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNotDir = fmt.Errorf("not a directory")
|
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 err
|
||||||
}
|
}
|
||||||
if !curr.IsDir() {
|
if !curr.IsDir() {
|
||||||
return ErrNotDir
|
return errNotDir
|
||||||
}
|
}
|
||||||
path := curr.Path
|
path := curr.Path
|
||||||
|
|
||||||
@@ -323,21 +323,21 @@ func (nav *Nav) open() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) bot() {
|
func (nav *nav) bot() {
|
||||||
dir := nav.currDir()
|
dir := nav.currDir()
|
||||||
|
|
||||||
dir.ind = len(dir.fi) - 1
|
dir.ind = len(dir.fi) - 1
|
||||||
dir.pos = min(dir.ind, nav.height-1)
|
dir.pos = min(dir.ind, nav.height-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) top() {
|
func (nav *nav) top() {
|
||||||
dir := nav.currDir()
|
dir := nav.currDir()
|
||||||
|
|
||||||
dir.ind = 0
|
dir.ind = 0
|
||||||
dir.pos = 0
|
dir.pos = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) cd(wd string) error {
|
func (nav *nav) cd(wd string) error {
|
||||||
wd = strings.Replace(wd, "~", envHome, -1)
|
wd = strings.Replace(wd, "~", envHome, -1)
|
||||||
wd = filepath.Clean(wd)
|
wd = filepath.Clean(wd)
|
||||||
|
|
||||||
@@ -356,7 +356,7 @@ func (nav *Nav) cd(wd string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) toggleMark(path string) {
|
func (nav *nav) toggleMark(path string) {
|
||||||
if nav.marks[path] {
|
if nav.marks[path] {
|
||||||
delete(nav.marks, path)
|
delete(nav.marks, path)
|
||||||
} else {
|
} else {
|
||||||
@@ -364,7 +364,7 @@ func (nav *Nav) toggleMark(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) toggle() {
|
func (nav *nav) toggle() {
|
||||||
curr, err := nav.currFile()
|
curr, err := nav.currFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -375,7 +375,7 @@ func (nav *Nav) toggle() {
|
|||||||
nav.down(1)
|
nav.down(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) invert() {
|
func (nav *nav) invert() {
|
||||||
last := nav.currDir()
|
last := nav.currDir()
|
||||||
for _, f := range last.fi {
|
for _, f := range last.fi {
|
||||||
path := filepath.Join(last.path, f.Name())
|
path := filepath.Join(last.path, f.Name())
|
||||||
@@ -383,7 +383,7 @@ func (nav *Nav) invert() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) save(copy bool) error {
|
func (nav *nav) save(copy bool) error {
|
||||||
if len(nav.marks) == 0 {
|
if len(nav.marks) == 0 {
|
||||||
curr, err := nav.currFile()
|
curr, err := nav.currFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -415,7 +415,7 @@ func (nav *Nav) save(copy bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) put() error {
|
func (nav *nav) put() error {
|
||||||
list, copy, err := loadFiles()
|
list, copy, err := loadFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -452,7 +452,7 @@ func (nav *Nav) put() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) sync() error {
|
func (nav *nav) sync() error {
|
||||||
list, copy, err := loadFiles()
|
list, copy, err := loadFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -466,11 +466,11 @@ func (nav *Nav) sync() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) currDir() *Dir {
|
func (nav *nav) currDir() *dir {
|
||||||
return nav.dirs[len(nav.dirs)-1]
|
return nav.dirs[len(nav.dirs)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) currFile() (*File, error) {
|
func (nav *nav) currFile() (*file, error) {
|
||||||
last := nav.dirs[len(nav.dirs)-1]
|
last := nav.dirs[len(nav.dirs)-1]
|
||||||
|
|
||||||
if len(last.fi) == 0 {
|
if len(last.fi) == 0 {
|
||||||
@@ -479,7 +479,7 @@ func (nav *Nav) currFile() (*File, error) {
|
|||||||
return last.fi[last.ind], nil
|
return last.fi[last.ind], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nav *Nav) currMarks() []string {
|
func (nav *nav) currMarks() []string {
|
||||||
marks := make([]string, 0, len(nav.marks))
|
marks := make([]string, 0, len(nav.marks))
|
||||||
for m := range nav.marks {
|
for m := range nav.marks {
|
||||||
marks = append(marks, m)
|
marks = append(marks, m)
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ var gOpts struct {
|
|||||||
sortby string
|
sortby string
|
||||||
timefmt string
|
timefmt string
|
||||||
ratios []int
|
ratios []int
|
||||||
keys map[string]Expr
|
keys map[string]expr
|
||||||
cmds map[string]Expr
|
cmds map[string]expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -31,35 +31,35 @@ func init() {
|
|||||||
gOpts.timefmt = time.ANSIC
|
gOpts.timefmt = time.ANSIC
|
||||||
gOpts.ratios = []int{1, 2, 3}
|
gOpts.ratios = []int{1, 2, 3}
|
||||||
|
|
||||||
gOpts.keys = make(map[string]Expr)
|
gOpts.keys = make(map[string]expr)
|
||||||
|
|
||||||
gOpts.keys["k"] = &CallExpr{"up", nil}
|
gOpts.keys["k"] = &callExpr{"up", nil}
|
||||||
gOpts.keys["<up>"] = &CallExpr{"up", nil}
|
gOpts.keys["<up>"] = &callExpr{"up", nil}
|
||||||
gOpts.keys["<c-u>"] = &CallExpr{"half-up", nil}
|
gOpts.keys["<c-u>"] = &callExpr{"half-up", nil}
|
||||||
gOpts.keys["<c-b>"] = &CallExpr{"page-up", nil}
|
gOpts.keys["<c-b>"] = &callExpr{"page-up", nil}
|
||||||
gOpts.keys["j"] = &CallExpr{"down", nil}
|
gOpts.keys["j"] = &callExpr{"down", nil}
|
||||||
gOpts.keys["<down>"] = &CallExpr{"down", nil}
|
gOpts.keys["<down>"] = &callExpr{"down", nil}
|
||||||
gOpts.keys["<c-d>"] = &CallExpr{"half-down", nil}
|
gOpts.keys["<c-d>"] = &callExpr{"half-down", nil}
|
||||||
gOpts.keys["<c-f>"] = &CallExpr{"page-down", nil}
|
gOpts.keys["<c-f>"] = &callExpr{"page-down", nil}
|
||||||
gOpts.keys["h"] = &CallExpr{"updir", nil}
|
gOpts.keys["h"] = &callExpr{"updir", nil}
|
||||||
gOpts.keys["<left>"] = &CallExpr{"updir", nil}
|
gOpts.keys["<left>"] = &callExpr{"updir", nil}
|
||||||
gOpts.keys["l"] = &CallExpr{"open", nil}
|
gOpts.keys["l"] = &callExpr{"open", nil}
|
||||||
gOpts.keys["<right>"] = &CallExpr{"open", nil}
|
gOpts.keys["<right>"] = &callExpr{"open", nil}
|
||||||
gOpts.keys["q"] = &CallExpr{"quit", nil}
|
gOpts.keys["q"] = &callExpr{"quit", nil}
|
||||||
gOpts.keys["G"] = &CallExpr{"bot", nil}
|
gOpts.keys["G"] = &callExpr{"bot", nil}
|
||||||
gOpts.keys["gg"] = &CallExpr{"top", nil}
|
gOpts.keys["gg"] = &callExpr{"top", nil}
|
||||||
gOpts.keys[":"] = &CallExpr{"read", nil}
|
gOpts.keys[":"] = &callExpr{"read", nil}
|
||||||
gOpts.keys["$"] = &CallExpr{"read-shell", nil}
|
gOpts.keys["$"] = &callExpr{"read-shell", nil}
|
||||||
gOpts.keys["!"] = &CallExpr{"read-shell-wait", nil}
|
gOpts.keys["!"] = &callExpr{"read-shell-wait", nil}
|
||||||
gOpts.keys["&"] = &CallExpr{"read-shell-async", nil}
|
gOpts.keys["&"] = &callExpr{"read-shell-async", nil}
|
||||||
gOpts.keys["/"] = &CallExpr{"search", nil}
|
gOpts.keys["/"] = &callExpr{"search", nil}
|
||||||
gOpts.keys["?"] = &CallExpr{"search-back", nil}
|
gOpts.keys["?"] = &callExpr{"search-back", nil}
|
||||||
gOpts.keys["<space>"] = &CallExpr{"toggle", nil}
|
gOpts.keys["<space>"] = &callExpr{"toggle", nil}
|
||||||
gOpts.keys["v"] = &CallExpr{"invert", nil}
|
gOpts.keys["v"] = &callExpr{"invert", nil}
|
||||||
gOpts.keys["y"] = &CallExpr{"yank", nil}
|
gOpts.keys["y"] = &callExpr{"yank", nil}
|
||||||
gOpts.keys["d"] = &CallExpr{"delete", nil}
|
gOpts.keys["d"] = &callExpr{"delete", nil}
|
||||||
gOpts.keys["p"] = &CallExpr{"put", nil}
|
gOpts.keys["p"] = &callExpr{"put", nil}
|
||||||
gOpts.keys["<c-l>"] = &CallExpr{"renew", nil}
|
gOpts.keys["<c-l>"] = &callExpr{"renew", nil}
|
||||||
|
|
||||||
gOpts.cmds = make(map[string]Expr)
|
gOpts.cmds = make(map[string]expr)
|
||||||
}
|
}
|
||||||
@@ -33,53 +33,53 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Expr interface {
|
type expr interface {
|
||||||
String() string
|
String() string
|
||||||
|
|
||||||
eval(app *App, args []string)
|
eval(app *app, args []string)
|
||||||
// TODO: add a bind method to avoid passing args in eval
|
// TODO: add a bind method to avoid passing args in eval
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetExpr struct {
|
type setExpr struct {
|
||||||
opt string
|
opt string
|
||||||
val string
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *SetExpr) String() string { return fmt.Sprintf("set %s %s", e.opt, e.val) }
|
func (e *setExpr) String() string { return fmt.Sprintf("set %s %s", e.opt, e.val) }
|
||||||
|
|
||||||
type MapExpr struct {
|
type mapExpr struct {
|
||||||
keys string
|
keys string
|
||||||
expr Expr
|
expr expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *MapExpr) String() string { return fmt.Sprintf("map %s %s", e.keys, e.expr) }
|
func (e *mapExpr) String() string { return fmt.Sprintf("map %s %s", e.keys, e.expr) }
|
||||||
|
|
||||||
type CmdExpr struct {
|
type cmdExpr struct {
|
||||||
name string
|
name string
|
||||||
expr Expr
|
expr expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *CmdExpr) String() string { return fmt.Sprintf("cmd %s %s", e.name, e.expr) }
|
func (e *cmdExpr) String() string { return fmt.Sprintf("cmd %s %s", e.name, e.expr) }
|
||||||
|
|
||||||
type CallExpr struct {
|
type callExpr struct {
|
||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *CallExpr) String() string { return fmt.Sprintf("%s -- %s", e.name, e.args) }
|
func (e *callExpr) String() string { return fmt.Sprintf("%s -- %s", e.name, e.args) }
|
||||||
|
|
||||||
type ExecExpr struct {
|
type execExpr struct {
|
||||||
pref string
|
pref string
|
||||||
expr string
|
expr string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ExecExpr) String() string { return fmt.Sprintf("%s %s", e.pref, e.expr) }
|
func (e *execExpr) String() string { return fmt.Sprintf("%s %s", e.pref, e.expr) }
|
||||||
|
|
||||||
type ListExpr struct {
|
type listExpr struct {
|
||||||
exprs []Expr
|
exprs []expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ListExpr) String() string {
|
func (e *listExpr) String() string {
|
||||||
buf := []byte{':', '{', '{', ' '}
|
buf := []byte{':', '{', '{', ' '}
|
||||||
for _, expr := range e.exprs {
|
for _, expr := range e.exprs {
|
||||||
buf = append(buf, expr.String()...)
|
buf = append(buf, expr.String()...)
|
||||||
@@ -89,95 +89,95 @@ func (e *ListExpr) String() string {
|
|||||||
return string(buf)
|
return string(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Parser struct {
|
type parser struct {
|
||||||
scanner *Scanner
|
scanner *scanner
|
||||||
expr Expr
|
expr expr
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func newParser(r io.Reader) *Parser {
|
func newParser(r io.Reader) *parser {
|
||||||
scanner := newScanner(r)
|
scanner := newScanner(r)
|
||||||
|
|
||||||
scanner.scan()
|
scanner.scan()
|
||||||
|
|
||||||
return &Parser{
|
return &parser{
|
||||||
scanner: scanner,
|
scanner: scanner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) parseExpr() Expr {
|
func (p *parser) parseExpr() expr {
|
||||||
s := p.scanner
|
s := p.scanner
|
||||||
|
|
||||||
var result Expr
|
var result expr
|
||||||
|
|
||||||
switch s.typ {
|
switch s.typ {
|
||||||
case TokenEOF:
|
case tokenEOF:
|
||||||
return nil
|
return nil
|
||||||
case TokenIdent:
|
case tokenIdent:
|
||||||
switch s.tok {
|
switch s.tok {
|
||||||
case "set":
|
case "set":
|
||||||
var val string
|
var val string
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
if s.typ != TokenIdent {
|
if s.typ != tokenIdent {
|
||||||
p.err = fmt.Errorf("expected identifier: %s", s.tok)
|
p.err = fmt.Errorf("expected identifier: %s", s.tok)
|
||||||
}
|
}
|
||||||
opt := s.tok
|
opt := s.tok
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
if s.typ != TokenSemicolon {
|
if s.typ != tokenSemicolon {
|
||||||
val = s.tok
|
val = s.tok
|
||||||
s.scan()
|
s.scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
result = &SetExpr{opt, val}
|
result = &setExpr{opt, val}
|
||||||
case "map":
|
case "map":
|
||||||
var expr Expr
|
var expr expr
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
keys := s.tok
|
keys := s.tok
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
if s.typ != TokenSemicolon {
|
if s.typ != tokenSemicolon {
|
||||||
expr = p.parseExpr()
|
expr = p.parseExpr()
|
||||||
} else {
|
} else {
|
||||||
s.scan()
|
s.scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
result = &MapExpr{keys, expr}
|
result = &mapExpr{keys, expr}
|
||||||
case "cmd":
|
case "cmd":
|
||||||
var expr Expr
|
var expr expr
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
name := s.tok
|
name := s.tok
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
if s.typ != TokenSemicolon {
|
if s.typ != tokenSemicolon {
|
||||||
expr = p.parseExpr()
|
expr = p.parseExpr()
|
||||||
} else {
|
} else {
|
||||||
s.scan()
|
s.scan()
|
||||||
}
|
}
|
||||||
|
|
||||||
result = &CmdExpr{name, expr}
|
result = &cmdExpr{name, expr}
|
||||||
default:
|
default:
|
||||||
name := s.tok
|
name := s.tok
|
||||||
|
|
||||||
var args []string
|
var args []string
|
||||||
for s.scan() && s.typ != TokenSemicolon {
|
for s.scan() && s.typ != tokenSemicolon {
|
||||||
args = append(args, s.tok)
|
args = append(args, s.tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
result = &CallExpr{name, args}
|
result = &callExpr{name, args}
|
||||||
}
|
}
|
||||||
case TokenColon:
|
case tokenColon:
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
var exprs []Expr
|
var exprs []expr
|
||||||
if s.typ == TokenLBraces {
|
if s.typ == tokenLBraces {
|
||||||
s.scan()
|
s.scan()
|
||||||
for {
|
for {
|
||||||
e := p.parseExpr()
|
e := p.parseExpr()
|
||||||
@@ -185,7 +185,7 @@ func (p *Parser) parseExpr() Expr {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
exprs = append(exprs, e)
|
exprs = append(exprs, e)
|
||||||
if s.typ == TokenRBraces {
|
if s.typ == tokenRBraces {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,14 +205,14 @@ func (p *Parser) parseExpr() Expr {
|
|||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
result = &ListExpr{exprs}
|
result = &listExpr{exprs}
|
||||||
case TokenPrefix:
|
case tokenPrefix:
|
||||||
var expr string
|
var expr string
|
||||||
|
|
||||||
pref := s.tok
|
pref := s.tok
|
||||||
|
|
||||||
s.scan()
|
s.scan()
|
||||||
if s.typ == TokenLBraces {
|
if s.typ == tokenLBraces {
|
||||||
s.scan()
|
s.scan()
|
||||||
expr = s.tok
|
expr = s.tok
|
||||||
s.scan()
|
s.scan()
|
||||||
@@ -223,7 +223,7 @@ func (p *Parser) parseExpr() Expr {
|
|||||||
s.scan()
|
s.scan()
|
||||||
s.scan()
|
s.scan()
|
||||||
|
|
||||||
result = &ExecExpr{pref, expr}
|
result = &execExpr{pref, expr}
|
||||||
default:
|
default:
|
||||||
p.err = fmt.Errorf("unexpected token: %s", s.tok)
|
p.err = fmt.Errorf("unexpected token: %s", s.tok)
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ func (p *Parser) parseExpr() Expr {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) parse() bool {
|
func (p *parser) parse() bool {
|
||||||
if p.expr = p.parseExpr(); p.expr == nil {
|
if p.expr = p.parseExpr(); p.expr == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,22 +7,22 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TokenType int
|
type tokenType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TokenEOF TokenType = iota
|
tokenEOF tokenType = iota
|
||||||
// no explicit keyword type
|
// no explicit keyword type
|
||||||
TokenIdent // e.g. set, ratios, 1:2:3
|
tokenIdent // e.g. set, ratios, 1:2:3
|
||||||
TokenColon // :
|
tokenColon // :
|
||||||
TokenPrefix // $, !, &, / or ?
|
tokenPrefix // $, !, &, / or ?
|
||||||
TokenLBraces // {{
|
tokenLBraces // {{
|
||||||
TokenRBraces // }}
|
tokenRBraces // }}
|
||||||
TokenCommand // in between a prefix to \n or between {{ and }}
|
tokenCommand // in between a prefix to \n or between {{ and }}
|
||||||
TokenSemicolon // ;
|
tokenSemicolon // ;
|
||||||
// comments are stripped
|
// comments are stripped
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scanner struct {
|
type scanner struct {
|
||||||
buf []byte // input buffer
|
buf []byte // input buffer
|
||||||
off int // current offset in buf
|
off int // current offset in buf
|
||||||
chr byte // current character in buf
|
chr byte // current character in buf
|
||||||
@@ -32,12 +32,12 @@ type Scanner struct {
|
|||||||
key bool // scanning keys
|
key bool // scanning keys
|
||||||
blk bool // scanning block
|
blk bool // scanning block
|
||||||
cmd bool // scanning command
|
cmd bool // scanning command
|
||||||
typ TokenType // scanned token type
|
typ tokenType // scanned token type
|
||||||
tok string // scanned token value
|
tok string // scanned token value
|
||||||
// TODO: pos
|
// TODO: pos
|
||||||
}
|
}
|
||||||
|
|
||||||
func newScanner(r io.Reader) *Scanner {
|
func newScanner(r io.Reader) *scanner {
|
||||||
buf, err := ioutil.ReadAll(r)
|
buf, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("scanning: %s", err)
|
log.Printf("scanning: %s", err)
|
||||||
@@ -53,14 +53,14 @@ func newScanner(r io.Reader) *Scanner {
|
|||||||
chr = buf[0]
|
chr = buf[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Scanner{
|
return &scanner{
|
||||||
buf: buf,
|
buf: buf,
|
||||||
eof: eof,
|
eof: eof,
|
||||||
chr: chr,
|
chr: chr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) next() {
|
func (s *scanner) next() {
|
||||||
if s.off+1 < len(s.buf) {
|
if s.off+1 < len(s.buf) {
|
||||||
s.off++
|
s.off++
|
||||||
s.chr = s.buf[s.off]
|
s.chr = s.buf[s.off]
|
||||||
@@ -72,7 +72,7 @@ func (s *Scanner) next() {
|
|||||||
s.eof = true
|
s.eof = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) peek() byte {
|
func (s *scanner) peek() byte {
|
||||||
if s.off+1 < len(s.buf) {
|
if s.off+1 < len(s.buf) {
|
||||||
return s.buf[s.off+1]
|
return s.buf[s.off+1]
|
||||||
}
|
}
|
||||||
@@ -89,24 +89,24 @@ func isPrefix(b byte) bool {
|
|||||||
return b == '$' || b == '!' || b == '&' // || b == '/' || b == '?'
|
return b == '$' || b == '!' || b == '&' // || b == '/' || b == '?'
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) scan() bool {
|
func (s *scanner) scan() bool {
|
||||||
scan:
|
scan:
|
||||||
switch {
|
switch {
|
||||||
case s.eof:
|
case s.eof:
|
||||||
s.next()
|
s.next()
|
||||||
if s.sem {
|
if s.sem {
|
||||||
s.typ = TokenSemicolon
|
s.typ = tokenSemicolon
|
||||||
s.tok = "\n"
|
s.tok = "\n"
|
||||||
s.sem = false
|
s.sem = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if s.nln {
|
if s.nln {
|
||||||
s.typ = TokenSemicolon
|
s.typ = tokenSemicolon
|
||||||
s.tok = "\n"
|
s.tok = "\n"
|
||||||
s.nln = false
|
s.nln = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
s.typ = TokenEOF
|
s.typ = tokenEOF
|
||||||
s.tok = "EOF"
|
s.tok = "EOF"
|
||||||
return false
|
return false
|
||||||
case s.key:
|
case s.key:
|
||||||
@@ -114,7 +114,7 @@ scan:
|
|||||||
for !s.eof && !isSpace(s.chr) {
|
for !s.eof && !isSpace(s.chr) {
|
||||||
s.next()
|
s.next()
|
||||||
}
|
}
|
||||||
s.typ = TokenIdent
|
s.typ = tokenIdent
|
||||||
s.tok = string(s.buf[beg:s.off])
|
s.tok = string(s.buf[beg:s.off])
|
||||||
s.key = false
|
s.key = false
|
||||||
case s.blk:
|
case s.blk:
|
||||||
@@ -123,7 +123,7 @@ scan:
|
|||||||
if !s.cmd {
|
if !s.cmd {
|
||||||
s.next()
|
s.next()
|
||||||
s.next()
|
s.next()
|
||||||
s.typ = TokenRBraces
|
s.typ = tokenRBraces
|
||||||
s.tok = "}}"
|
s.tok = "}}"
|
||||||
s.blk = false
|
s.blk = false
|
||||||
s.sem = true
|
s.sem = true
|
||||||
@@ -136,7 +136,7 @@ scan:
|
|||||||
s.next()
|
s.next()
|
||||||
if s.chr == '}' {
|
if s.chr == '}' {
|
||||||
if !s.eof && s.peek() == '}' {
|
if !s.eof && s.peek() == '}' {
|
||||||
s.typ = TokenCommand
|
s.typ = tokenCommand
|
||||||
s.tok = string(s.buf[beg:s.off])
|
s.tok = string(s.buf[beg:s.off])
|
||||||
s.cmd = false
|
s.cmd = false
|
||||||
return true
|
return true
|
||||||
@@ -144,7 +144,7 @@ scan:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.typ = TokenEOF
|
s.typ = tokenEOF
|
||||||
s.tok = "EOF"
|
s.tok = "EOF"
|
||||||
return false
|
return false
|
||||||
case s.cmd:
|
case s.cmd:
|
||||||
@@ -156,7 +156,7 @@ scan:
|
|||||||
if s.peek() == '{' {
|
if s.peek() == '{' {
|
||||||
s.next()
|
s.next()
|
||||||
s.next()
|
s.next()
|
||||||
s.typ = TokenLBraces
|
s.typ = tokenLBraces
|
||||||
s.tok = "{{"
|
s.tok = "{{"
|
||||||
s.blk = true
|
s.blk = true
|
||||||
return true
|
return true
|
||||||
@@ -169,20 +169,20 @@ scan:
|
|||||||
s.next()
|
s.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
s.typ = TokenCommand
|
s.typ = tokenCommand
|
||||||
s.tok = string(s.buf[beg:s.off])
|
s.tok = string(s.buf[beg:s.off])
|
||||||
s.cmd = false
|
s.cmd = false
|
||||||
s.sem = true
|
s.sem = true
|
||||||
case s.chr == '\n':
|
case s.chr == '\n':
|
||||||
if s.sem {
|
if s.sem {
|
||||||
s.typ = TokenSemicolon
|
s.typ = tokenSemicolon
|
||||||
s.tok = "\n"
|
s.tok = "\n"
|
||||||
s.sem = false
|
s.sem = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
s.next()
|
s.next()
|
||||||
if s.nln {
|
if s.nln {
|
||||||
s.typ = TokenSemicolon
|
s.typ = tokenSemicolon
|
||||||
s.tok = "\n"
|
s.tok = "\n"
|
||||||
s.nln = false
|
s.nln = false
|
||||||
return true
|
return true
|
||||||
@@ -194,7 +194,7 @@ scan:
|
|||||||
}
|
}
|
||||||
goto scan
|
goto scan
|
||||||
case s.chr == ';':
|
case s.chr == ';':
|
||||||
s.typ = TokenSemicolon
|
s.typ = tokenSemicolon
|
||||||
s.tok = ";"
|
s.tok = ";"
|
||||||
s.sem = false
|
s.sem = false
|
||||||
s.next()
|
s.next()
|
||||||
@@ -209,29 +209,29 @@ scan:
|
|||||||
for !s.eof && s.chr != '\'' {
|
for !s.eof && s.chr != '\'' {
|
||||||
s.next()
|
s.next()
|
||||||
}
|
}
|
||||||
s.typ = TokenIdent
|
s.typ = tokenIdent
|
||||||
s.tok = string(s.buf[beg:s.off])
|
s.tok = string(s.buf[beg:s.off])
|
||||||
s.next()
|
s.next()
|
||||||
case s.chr == ':':
|
case s.chr == ':':
|
||||||
s.typ = TokenColon
|
s.typ = tokenColon
|
||||||
s.tok = ":"
|
s.tok = ":"
|
||||||
s.nln = true
|
s.nln = true
|
||||||
s.next()
|
s.next()
|
||||||
case s.chr == '{' && s.peek() == '{':
|
case s.chr == '{' && s.peek() == '{':
|
||||||
s.next()
|
s.next()
|
||||||
s.next()
|
s.next()
|
||||||
s.typ = TokenLBraces
|
s.typ = tokenLBraces
|
||||||
s.tok = "{{"
|
s.tok = "{{"
|
||||||
s.sem = false
|
s.sem = false
|
||||||
s.nln = false
|
s.nln = false
|
||||||
case s.chr == '}' && s.peek() == '}':
|
case s.chr == '}' && s.peek() == '}':
|
||||||
s.next()
|
s.next()
|
||||||
s.next()
|
s.next()
|
||||||
s.typ = TokenRBraces
|
s.typ = tokenRBraces
|
||||||
s.tok = "}}"
|
s.tok = "}}"
|
||||||
s.sem = true
|
s.sem = true
|
||||||
case isPrefix(s.chr):
|
case isPrefix(s.chr):
|
||||||
s.typ = TokenPrefix
|
s.typ = tokenPrefix
|
||||||
s.tok = string(s.chr)
|
s.tok = string(s.chr)
|
||||||
s.cmd = true
|
s.cmd = true
|
||||||
s.next()
|
s.next()
|
||||||
@@ -251,7 +251,7 @@ scan:
|
|||||||
s.next()
|
s.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
s.typ = TokenIdent
|
s.typ = tokenIdent
|
||||||
s.tok = string(buf)
|
s.tok = string(buf)
|
||||||
s.sem = true
|
s.sem = true
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import (
|
|||||||
"github.com/nsf/termbox-go"
|
"github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
const EscapeCode = 27
|
const gEscapeCode = 27
|
||||||
|
|
||||||
var gKeyVal = map[termbox.Key][]rune{
|
var gKeyVal = map[termbox.Key][]rune{
|
||||||
termbox.KeyF1: []rune{'<', 'f', '-', '1', '>'},
|
termbox.KeyF1: []rune{'<', 'f', '-', '1', '>'},
|
||||||
@@ -89,30 +89,30 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Win struct {
|
type win struct {
|
||||||
w int
|
w int
|
||||||
h int
|
h int
|
||||||
x int
|
x int
|
||||||
y int
|
y int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWin(w, h, x, y int) *Win {
|
func newWin(w, h, x, y int) *win {
|
||||||
return &Win{w, h, x, y}
|
return &win{w, h, x, y}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) renew(w, h, x, y int) {
|
func (win *win) renew(w, h, x, y int) {
|
||||||
win.w = w
|
win.w = w
|
||||||
win.h = h
|
win.h = h
|
||||||
win.x = x
|
win.x = x
|
||||||
win.y = y
|
win.y = y
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) print(x, y int, fg, bg termbox.Attribute, s string) {
|
func (win *win) print(x, y int, fg, bg termbox.Attribute, s string) {
|
||||||
off := x
|
off := x
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
r, w := utf8.DecodeRuneInString(s[i:])
|
r, w := utf8.DecodeRuneInString(s[i:])
|
||||||
|
|
||||||
if r == EscapeCode && 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 {
|
||||||
@@ -208,15 +208,15 @@ func (win *Win) print(x, y int, fg, bg termbox.Attribute, s string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) printf(x, y int, fg, bg termbox.Attribute, format string, a ...interface{}) {
|
func (win *win) printf(x, y int, fg, bg termbox.Attribute, format string, a ...interface{}) {
|
||||||
win.print(x, y, fg, bg, fmt.Sprintf(format, a...))
|
win.print(x, y, fg, bg, fmt.Sprintf(format, a...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) printl(x, y int, fg, bg termbox.Attribute, s string) {
|
func (win *win) printl(x, y int, fg, bg termbox.Attribute, s string) {
|
||||||
win.printf(x, y, fg, bg, "%s%*s", s, win.w-len(s), "")
|
win.printf(x, y, fg, bg, "%s%*s", s, win.w-len(s), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) printd(dir *Dir, marks, saves map[string]bool) {
|
func (win *win) printd(dir *dir, marks, saves map[string]bool) {
|
||||||
if win.w < 3 {
|
if win.w < 3 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -236,8 +236,8 @@ 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 != notLink:
|
||||||
if 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
|
||||||
@@ -323,7 +323,7 @@ func (win *Win) printd(dir *Dir, marks, saves map[string]bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (win *Win) printr(reg []string) {
|
func (win *win) printr(reg []string) {
|
||||||
fg, bg := termbox.ColorDefault, termbox.ColorDefault
|
fg, bg := termbox.ColorDefault, termbox.ColorDefault
|
||||||
|
|
||||||
for i, l := range reg {
|
for i, l := range reg {
|
||||||
@@ -333,14 +333,14 @@ func (win *Win) printr(reg []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type UI struct {
|
type ui struct {
|
||||||
wins []*Win
|
wins []*win
|
||||||
pwdwin *Win
|
pwdwin *win
|
||||||
msgwin *Win
|
msgwin *win
|
||||||
menuwin *Win
|
menuwin *win
|
||||||
message string
|
message string
|
||||||
regprev []string
|
regprev []string
|
||||||
dirprev *Dir
|
dirprev *dir
|
||||||
keychan chan string
|
keychan chan string
|
||||||
evschan chan termbox.Event
|
evschan chan termbox.Event
|
||||||
cmdpref string
|
cmdpref string
|
||||||
@@ -369,10 +369,10 @@ func getWidths(wtot int) []int {
|
|||||||
return widths
|
return widths
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUI() *UI {
|
func newUI() *ui {
|
||||||
wtot, htot := termbox.Size()
|
wtot, htot := termbox.Size()
|
||||||
|
|
||||||
var wins []*Win
|
var wins []*win
|
||||||
|
|
||||||
widths := getWidths(wtot)
|
widths := getWidths(wtot)
|
||||||
|
|
||||||
@@ -392,7 +392,7 @@ func newUI() *UI {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return &UI{
|
return &ui{
|
||||||
wins: wins,
|
wins: wins,
|
||||||
pwdwin: newWin(wtot, 1, 0, 0),
|
pwdwin: newWin(wtot, 1, 0, 0),
|
||||||
msgwin: newWin(wtot, 1, 0, htot-1),
|
msgwin: newWin(wtot, 1, 0, htot-1),
|
||||||
@@ -402,7 +402,7 @@ func newUI() *UI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) renew() {
|
func (ui *ui) renew() {
|
||||||
wtot, htot := termbox.Size()
|
wtot, htot := termbox.Size()
|
||||||
|
|
||||||
widths := getWidths(wtot)
|
widths := getWidths(wtot)
|
||||||
@@ -417,7 +417,7 @@ func (ui *UI) renew() {
|
|||||||
ui.msgwin.renew(wtot, 1, 0, htot-1)
|
ui.msgwin.renew(wtot, 1, 0, htot-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) loadFileInfo(nav *Nav) {
|
func (ui *ui) loadFileInfo(nav *nav) {
|
||||||
curr, err := nav.currFile()
|
curr, err := nav.currFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -426,7 +426,7 @@ func (ui *UI) loadFileInfo(nav *Nav) {
|
|||||||
ui.message = fmt.Sprintf("%v %v %v", curr.Mode(), humanize(curr.Size()), curr.ModTime().Format(gOpts.timefmt))
|
ui.message = fmt.Sprintf("%v %v %v", curr.Mode(), humanize(curr.Size()), curr.ModTime().Format(gOpts.timefmt))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) loadFile(nav *Nav) {
|
func (ui *ui) loadFile(nav *nav) {
|
||||||
curr, err := nav.currFile()
|
curr, err := nav.currFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -496,7 +496,7 @@ func (ui *UI) loadFile(nav *Nav) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) draw(nav *Nav) {
|
func (ui *ui) draw(nav *nav) {
|
||||||
fg, bg := termbox.ColorDefault, termbox.ColorDefault
|
fg, bg := termbox.ColorDefault, termbox.ColorDefault
|
||||||
|
|
||||||
termbox.Clear(fg, bg)
|
termbox.Clear(fg, bg)
|
||||||
@@ -569,8 +569,8 @@ func (ui *UI) draw(nav *Nav) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func findBinds(keys map[string]Expr, prefix string) (binds map[string]Expr, ok bool) {
|
func findBinds(keys map[string]expr, prefix string) (binds map[string]expr, ok bool) {
|
||||||
binds = make(map[string]Expr)
|
binds = make(map[string]expr)
|
||||||
for key, expr := range keys {
|
for key, expr := range keys {
|
||||||
if strings.HasPrefix(key, prefix) {
|
if strings.HasPrefix(key, prefix) {
|
||||||
binds[key] = expr
|
binds[key] = expr
|
||||||
@@ -582,7 +582,7 @@ func findBinds(keys map[string]Expr, prefix string) (binds map[string]Expr, ok b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func listBinds(binds map[string]Expr) *bytes.Buffer {
|
func listBinds(binds map[string]expr) *bytes.Buffer {
|
||||||
t := new(tabwriter.Writer)
|
t := new(tabwriter.Writer)
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
|
|
||||||
@@ -602,7 +602,7 @@ func listBinds(binds map[string]Expr) *bytes.Buffer {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) pollEvent() termbox.Event {
|
func (ui *ui) pollEvent() termbox.Event {
|
||||||
select {
|
select {
|
||||||
case key := <-ui.keychan:
|
case key := <-ui.keychan:
|
||||||
ev := termbox.Event{Type: termbox.EventKey}
|
ev := termbox.Event{Type: termbox.EventKey}
|
||||||
@@ -633,18 +633,18 @@ func (ui *UI) pollEvent() termbox.Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MultiExpr struct {
|
type multiExpr struct {
|
||||||
expr Expr
|
expr expr
|
||||||
count int
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is used to read expressions on the client side. Digits are
|
// 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
|
// interpreted as command counts but this is only done for digits preceding any
|
||||||
// non-digit characters (e.g. "42y2k" as 42 times "y2k").
|
// non-digit characters (e.g. "42y2k" as 42 times "y2k").
|
||||||
func (ui *UI) readExpr() chan MultiExpr {
|
func (ui *ui) readExpr() chan multiExpr {
|
||||||
ch := make(chan MultiExpr)
|
ch := make(chan multiExpr)
|
||||||
|
|
||||||
renew := &CallExpr{"renew", nil}
|
renew := &callExpr{"renew", nil}
|
||||||
count := 1
|
count := 1
|
||||||
|
|
||||||
var acc []rune
|
var acc []rune
|
||||||
@@ -658,40 +658,40 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
switch ev.Type {
|
switch ev.Type {
|
||||||
case termbox.EventKey:
|
case termbox.EventKey:
|
||||||
if ev.Ch != 0 {
|
if ev.Ch != 0 {
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-insert", []string{string(ev.Ch)}}, 1}
|
ch <- multiExpr{&callExpr{"cmd-insert", []string{string(ev.Ch)}}, 1}
|
||||||
} else {
|
} else {
|
||||||
// TODO: rest of the keys
|
// TODO: rest of the keys
|
||||||
switch ev.Key {
|
switch ev.Key {
|
||||||
case termbox.KeyEsc:
|
case termbox.KeyEsc:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-escape", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-escape", nil}, 1}
|
||||||
case termbox.KeySpace:
|
case termbox.KeySpace:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-insert", []string{" "}}, 1}
|
ch <- multiExpr{&callExpr{"cmd-insert", []string{" "}}, 1}
|
||||||
case termbox.KeyTab:
|
case termbox.KeyTab:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-comp", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-comp", nil}, 1}
|
||||||
case termbox.KeyEnter, termbox.KeyCtrlJ:
|
case termbox.KeyEnter, termbox.KeyCtrlJ:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-enter", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-enter", nil}, 1}
|
||||||
case termbox.KeyBackspace, termbox.KeyBackspace2:
|
case termbox.KeyBackspace, termbox.KeyBackspace2:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-delete-back", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-delete-back", nil}, 1}
|
||||||
case termbox.KeyDelete, termbox.KeyCtrlD:
|
case termbox.KeyDelete, termbox.KeyCtrlD:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-delete", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-delete", nil}, 1}
|
||||||
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
|
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-left", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-left", nil}, 1}
|
||||||
case termbox.KeyArrowRight, termbox.KeyCtrlF:
|
case termbox.KeyArrowRight, termbox.KeyCtrlF:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-right", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-right", nil}, 1}
|
||||||
case termbox.KeyHome, termbox.KeyCtrlA:
|
case termbox.KeyHome, termbox.KeyCtrlA:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-beg", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-beg", nil}, 1}
|
||||||
case termbox.KeyEnd, termbox.KeyCtrlE:
|
case termbox.KeyEnd, termbox.KeyCtrlE:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-end", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-end", nil}, 1}
|
||||||
case termbox.KeyCtrlK:
|
case termbox.KeyCtrlK:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-delete-end", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-delete-end", nil}, 1}
|
||||||
case termbox.KeyCtrlU:
|
case termbox.KeyCtrlU:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-delete-beg", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-delete-beg", nil}, 1}
|
||||||
case termbox.KeyCtrlW:
|
case termbox.KeyCtrlW:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-delete-word", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-delete-word", nil}, 1}
|
||||||
case termbox.KeyCtrlY:
|
case termbox.KeyCtrlY:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-put", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-put", nil}, 1}
|
||||||
case termbox.KeyCtrlT:
|
case termbox.KeyCtrlT:
|
||||||
ch <- MultiExpr{&CallExpr{"cmd-transpose", nil}, 1}
|
ch <- multiExpr{&callExpr{"cmd-transpose", nil}, 1}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@@ -714,7 +714,7 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
} else {
|
} else {
|
||||||
val := gKeyVal[ev.Key]
|
val := gKeyVal[ev.Key]
|
||||||
if string(val) == "<esc>" {
|
if string(val) == "<esc>" {
|
||||||
ch <- MultiExpr{renew, 1}
|
ch <- multiExpr{renew, 1}
|
||||||
acc = nil
|
acc = nil
|
||||||
cnt = nil
|
cnt = nil
|
||||||
}
|
}
|
||||||
@@ -726,7 +726,7 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
switch len(binds) {
|
switch len(binds) {
|
||||||
case 0:
|
case 0:
|
||||||
ui.message = fmt.Sprintf("unknown mapping: %s", string(acc))
|
ui.message = fmt.Sprintf("unknown mapping: %s", string(acc))
|
||||||
ch <- MultiExpr{renew, 1}
|
ch <- multiExpr{renew, 1}
|
||||||
acc = nil
|
acc = nil
|
||||||
cnt = nil
|
cnt = nil
|
||||||
case 1:
|
case 1:
|
||||||
@@ -741,13 +741,13 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
expr := gOpts.keys[string(acc)]
|
expr := gOpts.keys[string(acc)]
|
||||||
ch <- MultiExpr{expr, count}
|
ch <- multiExpr{expr, count}
|
||||||
acc = nil
|
acc = nil
|
||||||
cnt = nil
|
cnt = nil
|
||||||
}
|
}
|
||||||
if len(acc) > 0 {
|
if len(acc) > 0 {
|
||||||
ui.menubuf = listBinds(binds)
|
ui.menubuf = listBinds(binds)
|
||||||
ch <- MultiExpr{renew, 1}
|
ch <- multiExpr{renew, 1}
|
||||||
} else if ui.menubuf != nil {
|
} else if ui.menubuf != nil {
|
||||||
ui.menubuf = nil
|
ui.menubuf = nil
|
||||||
}
|
}
|
||||||
@@ -764,19 +764,19 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
expr := gOpts.keys[string(acc)]
|
expr := gOpts.keys[string(acc)]
|
||||||
ch <- MultiExpr{expr, count}
|
ch <- multiExpr{expr, count}
|
||||||
acc = nil
|
acc = nil
|
||||||
cnt = nil
|
cnt = nil
|
||||||
}
|
}
|
||||||
if len(acc) > 0 {
|
if len(acc) > 0 {
|
||||||
ui.menubuf = listBinds(binds)
|
ui.menubuf = listBinds(binds)
|
||||||
ch <- MultiExpr{renew, 1}
|
ch <- multiExpr{renew, 1}
|
||||||
} else {
|
} else {
|
||||||
ui.menubuf = nil
|
ui.menubuf = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case termbox.EventResize:
|
case termbox.EventResize:
|
||||||
ch <- MultiExpr{renew, 1}
|
ch <- multiExpr{renew, 1}
|
||||||
default:
|
default:
|
||||||
// TODO: handle other events
|
// TODO: handle other events
|
||||||
}
|
}
|
||||||
@@ -786,17 +786,17 @@ func (ui *UI) readExpr() chan MultiExpr {
|
|||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) pause() {
|
func (ui *ui) pause() {
|
||||||
termbox.Close()
|
termbox.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) resume() {
|
func (ui *ui) resume() {
|
||||||
if err := termbox.Init(); err != nil {
|
if err := termbox.Init(); err != nil {
|
||||||
log.Fatalf("initializing termbox: %s", err)
|
log.Fatalf("initializing termbox: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) sync() {
|
func (ui *ui) sync() {
|
||||||
if err := termbox.Sync(); err != nil {
|
if err := termbox.Sync(); err != nil {
|
||||||
log.Printf("syncing termbox: %s", err)
|
log.Printf("syncing termbox: %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user