lf/eval.go

390 lines
8.3 KiB
Go
Raw Normal View History

2016-08-13 12:49:04 +00:00
package main
import (
"fmt"
"log"
"os"
2016-08-13 12:49:04 +00:00
"strconv"
"strings"
"unicode/utf8"
2016-08-13 12:49:04 +00:00
)
func (e *SetExpr) eval(app *App, args []string) {
switch e.opt {
case "hidden":
gOpts.hidden = true
app.nav.renew(app.nav.height)
case "nohidden":
gOpts.hidden = false
app.nav.renew(app.nav.height)
case "hidden!":
gOpts.hidden = !gOpts.hidden
app.nav.renew(app.nav.height)
case "preview":
gOpts.preview = true
case "nopreview":
gOpts.preview = false
case "preview!":
gOpts.preview = !gOpts.preview
case "dirfirst":
gOpts.dirfirst = true
app.nav.renew(app.nav.height)
case "nodirfirst":
gOpts.dirfirst = false
app.nav.renew(app.nav.height)
case "dirfirst!":
gOpts.dirfirst = !gOpts.dirfirst
app.nav.renew(app.nav.height)
2016-08-13 12:49:04 +00:00
case "scrolloff":
n, err := strconv.Atoi(e.val)
if err != nil {
2016-08-17 20:00:16 +00:00
msg := fmt.Sprintf("scrolloff: %s", err)
app.ui.message = msg
log.Print(msg)
2016-08-13 12:49:04 +00:00
return
}
if n < 0 {
2016-08-17 20:00:16 +00:00
msg := "scrolloff: value should be a non-negative number"
2016-08-13 12:49:04 +00:00
app.ui.message = msg
log.Print(msg)
return
}
2016-09-02 20:06:25 +00:00
max := app.ui.wins[0].h / 2
2016-08-13 12:49:04 +00:00
if n > max {
n = max
}
gOpts.scrolloff = n
case "tabstop":
n, err := strconv.Atoi(e.val)
if err != nil {
2016-08-17 20:00:16 +00:00
msg := fmt.Sprintf("tabstop: %s", err)
app.ui.message = msg
log.Print(msg)
2016-08-13 12:49:04 +00:00
return
}
if n <= 0 {
2016-08-17 20:00:16 +00:00
msg := "tabstop: value should be a positive number"
2016-08-13 12:49:04 +00:00
app.ui.message = msg
log.Print(msg)
return
}
gOpts.tabstop = n
case "ifs":
gOpts.ifs = e.val
case "previewer":
gOpts.previewer = strings.Replace(e.val, "~", envHome, -1)
case "shell":
gOpts.shell = e.val
2016-08-13 12:49:04 +00:00
case "showinfo":
if e.val != "none" && e.val != "size" && e.val != "time" {
msg := "showinfo should either be 'none', 'size' or 'time'"
app.ui.message = msg
log.Print(msg)
return
}
gOpts.showinfo = e.val
case "sortby":
if e.val != "name" && e.val != "size" && e.val != "time" {
msg := "sortby should either be 'name', 'size' or 'time'"
app.ui.message = msg
log.Print(msg)
return
}
gOpts.sortby = e.val
app.nav.renew(app.nav.height)
case "ratios":
toks := strings.Split(e.val, ":")
var rats []int
for _, s := range toks {
i, err := strconv.Atoi(s)
if err != nil {
2016-08-17 20:00:16 +00:00
msg := fmt.Sprintf("ratios: %s", err)
app.ui.message = msg
log.Print(msg)
2016-08-13 12:49:04 +00:00
return
}
rats = append(rats, i)
}
gOpts.ratios = rats
app.ui = newUI()
app.ui.loadFile(app.nav)
2016-08-13 12:49:04 +00:00
default:
msg := fmt.Sprintf("unknown option: %s", e.opt)
app.ui.message = msg
log.Print(msg)
}
}
func (e *MapExpr) eval(app *App, args []string) {
if e.expr == nil {
delete(gOpts.keys, e.keys)
return
}
2016-08-13 12:49:04 +00:00
gOpts.keys[e.keys] = e.expr
}
func (e *CmdExpr) eval(app *App, args []string) {
if e.expr == nil {
delete(gOpts.cmds, e.name)
return
}
2016-08-13 12:49:04 +00:00
gOpts.cmds[e.name] = e.expr
}
func splitKeys(s string) (keys []string) {
for i := 0; i < len(s); {
c, w := utf8.DecodeRuneInString(s[i:])
if c != '<' {
keys = append(keys, s[i:i+w])
i += w
} else {
j := i + w
for c != '>' && j < len(s) {
c, w = utf8.DecodeRuneInString(s[j:])
j += w
}
keys = append(keys, s[i:j])
i = j
}
}
return
}
2016-08-13 12:49:04 +00:00
func (e *CallExpr) eval(app *App, args []string) {
// TODO: check for extra toks in each case
switch e.name {
case "up":
app.nav.up(1)
app.ui.loadFile(app.nav)
case "half-up":
app.nav.up(app.nav.height / 2)
app.ui.loadFile(app.nav)
case "page-up":
app.nav.up(app.nav.height)
app.ui.loadFile(app.nav)
2016-08-28 14:02:58 +00:00
case "down":
app.nav.down(1)
app.ui.loadFile(app.nav)
case "half-down":
app.nav.down(app.nav.height / 2)
app.ui.loadFile(app.nav)
case "page-down":
app.nav.down(app.nav.height)
app.ui.loadFile(app.nav)
2016-08-13 12:49:04 +00:00
case "updir":
2016-08-17 20:28:42 +00:00
if err := app.nav.updir(); err != nil {
2016-08-13 12:49:04 +00:00
app.ui.message = err.Error()
log.Print(err)
return
}
app.ui.loadFile(app.nav)
2016-08-13 12:49:04 +00:00
case "open":
err := app.nav.open()
if err == nil {
app.ui.loadFile(app.nav)
return
}
if err != ErrNotDir {
app.ui.message = err.Error()
log.Print(err)
2016-08-14 12:37:22 +00:00
return
}
if gSelectionPath != "" {
out, err := os.Create(gSelectionPath)
if err != nil {
2016-08-17 20:00:16 +00:00
log.Printf("opening selection file: %s", err)
return
2016-08-14 12:37:22 +00:00
}
defer out.Close()
var path string
2016-08-14 12:37:22 +00:00
if len(app.nav.marks) != 0 {
marks := app.nav.currMarks()
path = strings.Join(marks, "\n")
} else if curr, err := app.nav.currFile(); err == nil {
path = curr.Path
} else {
return
2016-08-14 12:37:22 +00:00
}
_, err = out.WriteString(path)
if err != nil {
2016-08-17 20:00:16 +00:00
log.Printf("writing selection file: %s", err)
2016-08-14 12:37:22 +00:00
}
go func() { app.quit <- true }()
2016-08-14 12:37:22 +00:00
return
}
if cmd, ok := gOpts.cmds["open-file"]; ok {
cmd.eval(app, e.args)
2016-08-13 12:49:04 +00:00
}
2016-08-28 14:02:58 +00:00
case "quit":
go func() { app.quit <- true }()
2016-08-13 12:49:04 +00:00
case "bot":
app.nav.bot()
app.ui.loadFile(app.nav)
2016-08-13 12:49:04 +00:00
case "top":
app.nav.top()
app.ui.loadFile(app.nav)
2016-08-13 12:49:04 +00:00
case "read":
s := app.ui.prompt(app.nav, ":")
2016-08-13 12:49:04 +00:00
if len(s) == 0 {
return
}
log.Printf("command: %s", s)
p := newParser(strings.NewReader(s))
for p.parse() {
p.expr.eval(app, nil)
}
if p.err != nil {
app.ui.message = p.err.Error()
log.Print(p.err)
}
case "read-shell":
s := app.ui.prompt(app.nav, "$")
if len(s) == 0 {
return
}
2016-08-13 12:49:04 +00:00
log.Printf("shell: %s", s)
app.runShell(s, nil, false, false)
case "read-shell-wait":
s := app.ui.prompt(app.nav, "!")
if len(s) == 0 {
return
}
2016-08-13 12:49:04 +00:00
log.Printf("shell-wait: %s", s)
app.runShell(s, nil, true, false)
case "read-shell-async":
s := app.ui.prompt(app.nav, "&")
if len(s) == 0 {
return
}
2016-08-13 12:49:04 +00:00
log.Printf("shell-async: %s", s)
app.runShell(s, nil, false, true)
case "search":
s := app.ui.prompt(app.nav, "/")
if len(s) == 0 {
return
}
2016-08-13 12:49:04 +00:00
log.Printf("search: %s", s)
app.ui.message = "sorry, search is not implemented yet!"
// TODO: implement
case "search-back":
s := app.ui.prompt(app.nav, "?")
if len(s) == 0 {
return
}
2016-08-13 12:49:04 +00:00
log.Printf("search-back: %s", s)
app.ui.message = "sorry, search-back is not implemented yet!"
// TODO: implement
case "toggle":
app.nav.toggle()
case "invert":
app.nav.invert()
2016-08-13 12:49:04 +00:00
case "yank":
2016-08-17 20:28:42 +00:00
if err := app.nav.save(true); err != nil {
2016-08-13 12:49:04 +00:00
msg := fmt.Sprintf("yank: %s", err)
app.ui.message = msg
log.Printf(msg)
return
}
app.nav.marks = make(map[string]bool)
if err := sendServer("send sync"); err != nil {
msg := fmt.Sprintf("yank: %s", err)
app.ui.message = msg
log.Printf(msg)
}
2016-08-13 12:49:04 +00:00
case "delete":
2016-08-17 20:28:42 +00:00
if err := app.nav.save(false); err != nil {
2016-08-13 12:49:04 +00:00
msg := fmt.Sprintf("delete: %s", err)
app.ui.message = msg
log.Printf(msg)
return
}
app.nav.marks = make(map[string]bool)
if err := sendServer("send sync"); err != nil {
msg := fmt.Sprintf("delete: %s", err)
app.ui.message = msg
log.Printf(msg)
}
2016-11-06 15:09:18 +00:00
case "put":
if err := app.nav.put(); err != nil {
msg := fmt.Sprintf("put: %s", err)
2016-08-13 12:49:04 +00:00
app.ui.message = msg
log.Printf(msg)
return
}
app.nav.renew(app.nav.height)
app.nav.save(false)
app.nav.saves = make(map[string]bool)
2016-08-13 12:49:04 +00:00
saveFiles(nil, false)
2016-08-24 09:36:52 +00:00
case "renew":
2016-08-24 09:34:54 +00:00
app.ui.sync()
2016-08-13 12:49:04 +00:00
app.ui.renew()
app.nav.renew(app.ui.wins[0].h)
case "sync":
if err := app.nav.sync(); err != nil {
msg := fmt.Sprintf("sync: %s", err)
app.ui.message = msg
log.Printf(msg)
}
2016-08-28 14:02:58 +00:00
case "echo":
app.ui.message = strings.Join(e.args, " ")
case "cd":
path := "~"
if len(e.args) > 0 {
path = e.args[0]
}
if err := app.nav.cd(path); err != nil {
app.ui.message = err.Error()
log.Print(err)
return
}
app.ui.loadFile(app.nav)
case "push":
if len(e.args) > 0 {
app.ui.keysbuf = append(app.ui.keysbuf, splitKeys(e.args[0])...)
}
2016-08-13 12:49:04 +00:00
default:
cmd, ok := gOpts.cmds[e.name]
if !ok {
msg := fmt.Sprintf("command not found: %s", e.name)
app.ui.message = msg
log.Print(msg)
return
}
cmd.eval(app, e.args)
}
}
func (e *ExecExpr) eval(app *App, args []string) {
switch e.pref {
case "$":
log.Printf("shell: %s -- %s", e, args)
app.runShell(e.expr, args, false, false)
case "!":
log.Printf("shell-wait: %s -- %s", e, args)
app.runShell(e.expr, args, true, false)
case "&":
log.Printf("shell-async: %s -- %s", e, args)
app.runShell(e.expr, args, false, true)
case "/":
log.Printf("search: %s -- %s", e, args)
// TODO: implement
case "?":
log.Printf("search-back: %s -- %s", e, args)
// TODO: implement
default:
log.Printf("unknown execution prefix: %q", e.pref)
}
}
func (e *ListExpr) eval(app *App, args []string) {
for _, expr := range e.exprs {
expr.eval(app, nil)
}
}