diff --git a/app.go b/app.go index 4f26324..52850e1 100644 --- a/app.go +++ b/app.go @@ -212,9 +212,7 @@ func (app *app) loop() { } defer f.Close() - dir := app.nav.currDir() - - _, err = f.WriteString(dir.path) + _, err = f.WriteString(app.nav.currDir().path) if err != nil { log.Printf("writing last dir file: %s", err) } @@ -326,8 +324,8 @@ func (app *app) loop() { func (app *app) exportFiles() { var currFile string - if f, err := app.nav.currFile(); err == nil { - currFile = f.path + if curr, err := app.nav.currFile(); err == nil { + currFile = curr.path } currSelections := app.nav.currSelections() diff --git a/eval.go b/eval.go index 4a906be..95a0e14 100644 --- a/eval.go +++ b/eval.go @@ -385,9 +385,9 @@ func update(app *app) { case gOpts.incsearch && app.ui.cmdPrefix == "/": app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) - last := app.nav.currDir() - last.ind = app.nav.searchInd - last.pos = app.nav.searchPos + dir := app.nav.currDir() + dir.ind = app.nav.searchInd + dir.pos = app.nav.searchPos if err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) @@ -399,9 +399,9 @@ func update(app *app) { case gOpts.incsearch && app.ui.cmdPrefix == "?": app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) - last := app.nav.currDir() - last.ind = app.nav.searchInd - last.pos = app.nav.searchPos + dir := app.nav.currDir() + dir.ind = app.nav.searchInd + dir.pos = app.nav.searchPos if err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) @@ -711,10 +711,10 @@ func (e *callExpr) eval(app *app, args []string) { if len(e.args) == 0 { app.nav.toggle() } else { - curr := app.nav.currDir() + dir := app.nav.currDir() for _, path := range e.args { if !filepath.IsAbs(path) { - path = filepath.Join(curr.path, path) + path = filepath.Join(dir.path, path) } if _, err := os.Lstat(path); !os.IsNotExist(err) { app.nav.toggleSelection(path) @@ -767,16 +767,16 @@ func (e *callExpr) eval(app *app, args []string) { return } } else { - fileOrSelections, err := app.nav.currFileOrSelections() + list, err := app.nav.currFileOrSelections() if err != nil { app.ui.echoerrf("delete: %s", err) return } - if selections := len(fileOrSelections); selections == 1 { - app.ui.cmdPrefix = "delete '" + fileOrSelections[0] + "' ? [y/N] " + if len(list) == 1 { + app.ui.cmdPrefix = "delete '" + list[0] + "' ? [y/N] " } else { - app.ui.cmdPrefix = "delete " + strconv.Itoa(selections) + " items? [y/N] " + app.ui.cmdPrefix = "delete " + strconv.Itoa(len(list)) + " items? [y/N] " } } app.ui.loadFile(app.nav) @@ -851,16 +851,16 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFileInfo(app.nav) case "search": app.ui.cmdPrefix = "/" - last := app.nav.currDir() - app.nav.searchInd = last.ind - app.nav.searchPos = last.pos + dir := app.nav.currDir() + app.nav.searchInd = dir.ind + app.nav.searchPos = dir.pos app.nav.searchBack = false app.ui.loadFileInfo(app.nav) case "search-back": app.ui.cmdPrefix = "?" - last := app.nav.currDir() - app.nav.searchInd = last.ind - app.nav.searchPos = last.pos + dir := app.nav.currDir() + app.nav.searchInd = dir.ind + app.nav.searchPos = dir.pos app.nav.searchBack = true app.ui.loadFileInfo(app.nav) case "search-next": @@ -1034,9 +1034,9 @@ func (e *callExpr) eval(app *app, args []string) { return } if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { - last := app.nav.currDir() - last.ind = app.nav.searchInd - last.pos = app.nav.searchPos + dir := app.nav.currDir() + dir.ind = app.nav.searchInd + dir.pos = app.nav.searchPos app.ui.loadFile(app.nav) app.ui.loadFileInfo(app.nav) @@ -1104,9 +1104,9 @@ func (e *callExpr) eval(app *app, args []string) { app.runShell(s, nil, "&") case "/": if gOpts.incsearch { - last := app.nav.currDir() - last.ind = app.nav.searchInd - last.pos = app.nav.searchPos + dir := app.nav.currDir() + dir.ind = app.nav.searchInd + dir.pos = app.nav.searchPos } log.Printf("search: %s", s) app.ui.cmdPrefix = "" @@ -1119,9 +1119,9 @@ func (e *callExpr) eval(app *app, args []string) { } case "?": if gOpts.incsearch { - last := app.nav.currDir() - last.ind = app.nav.searchInd - last.pos = app.nav.searchPos + dir := app.nav.currDir() + dir.ind = app.nav.searchInd + dir.pos = app.nav.searchPos } log.Printf("search-back: %s", s) app.ui.cmdPrefix = "" diff --git a/nav.go b/nav.go index 394a603..68e4a48 100644 --- a/nav.go +++ b/nav.go @@ -620,9 +620,9 @@ func (nav *nav) toggle() { } func (nav *nav) invert() { - last := nav.currDir() - for _, f := range last.files { - path := filepath.Join(last.path, f.Name()) + dir := nav.currDir() + for _, f := range dir.files { + path := filepath.Join(dir.path, f.Name()) nav.toggleSelection(path) } } @@ -939,25 +939,27 @@ func (nav *nav) sel(path string) error { } func (nav *nav) globSel(pattern string, invert bool) error { - curDir := nav.currDir() - anyMatches := false + dir := nav.currDir() + anyMatched := false - for i := 0; i < len(curDir.files); i++ { - match, err := filepath.Match(pattern, curDir.files[i].Name()) + for i := 0; i < len(dir.files); i++ { + matched, err := filepath.Match(pattern, dir.files[i].Name()) if err != nil { return fmt.Errorf("glob-select: %s", err) } - if match { - anyMatches = true - fpath := filepath.Join(curDir.path, curDir.files[i].Name()) + if matched { + anyMatched = true + fpath := filepath.Join(dir.path, dir.files[i].Name()) if _, ok := nav.selections[fpath]; ok == invert { nav.toggleSelection(fpath) } } } - if !anyMatches { + + if !anyMatched { return fmt.Errorf("glob-select: pattern not found: %s", pattern) } + return nil } @@ -985,9 +987,9 @@ func findMatch(name, pattern string) bool { func (nav *nav) findSingle() int { count := 0 index := 0 - last := nav.currDir() - for i := 0; i < len(last.files); i++ { - if findMatch(last.files[i].Name(), nav.find) { + dir := nav.currDir() + for i := 0; i < len(dir.files); i++ { + if findMatch(dir.files[i].Name(), nav.find) { count++ if count > 1 { return count @@ -996,27 +998,27 @@ func (nav *nav) findSingle() int { } } if count == 1 { - if index > last.ind { - nav.down(index - last.ind) + if index > dir.ind { + nav.down(index - dir.ind) } else { - nav.up(last.ind - index) + nav.up(dir.ind - index) } } return count } func (nav *nav) findNext() bool { - last := nav.currDir() - for i := last.ind + 1; i < len(last.files); i++ { - if findMatch(last.files[i].Name(), nav.find) { - nav.down(i - last.ind) + dir := nav.currDir() + for i := dir.ind + 1; i < len(dir.files); i++ { + if findMatch(dir.files[i].Name(), nav.find) { + nav.down(i - dir.ind) return true } } if gOpts.wrapscan { - for i := 0; i < last.ind; i++ { - if findMatch(last.files[i].Name(), nav.find) { - nav.up(last.ind - i) + for i := 0; i < dir.ind; i++ { + if findMatch(dir.files[i].Name(), nav.find) { + nav.up(dir.ind - i) return true } } @@ -1025,17 +1027,17 @@ func (nav *nav) findNext() bool { } func (nav *nav) findPrev() bool { - last := nav.currDir() - for i := last.ind - 1; i >= 0; i-- { - if findMatch(last.files[i].Name(), nav.find) { - nav.up(last.ind - i) + dir := nav.currDir() + for i := dir.ind - 1; i >= 0; i-- { + if findMatch(dir.files[i].Name(), nav.find) { + nav.up(dir.ind - i) return true } } if gOpts.wrapscan { - for i := len(last.files) - 1; i > last.ind; i-- { - if findMatch(last.files[i].Name(), nav.find) { - nav.down(i - last.ind) + for i := len(dir.files) - 1; i > dir.ind; i-- { + if findMatch(dir.files[i].Name(), nav.find) { + nav.down(i - dir.ind) return true } } @@ -1065,25 +1067,25 @@ func searchMatch(name, pattern string) (matched bool, err error) { } func (nav *nav) searchNext() error { - last := nav.currDir() - for i := last.ind + 1; i < len(last.files); i++ { - matched, err := searchMatch(last.files[i].Name(), nav.search) + dir := nav.currDir() + for i := dir.ind + 1; i < len(dir.files); i++ { + matched, err := searchMatch(dir.files[i].Name(), nav.search) if err != nil { return err } if matched { - nav.down(i - last.ind) + nav.down(i - dir.ind) return nil } } if gOpts.wrapscan { - for i := 0; i < last.ind; i++ { - matched, err := searchMatch(last.files[i].Name(), nav.search) + for i := 0; i < dir.ind; i++ { + matched, err := searchMatch(dir.files[i].Name(), nav.search) if err != nil { return err } if matched { - nav.up(last.ind - i) + nav.up(dir.ind - i) return nil } } @@ -1092,25 +1094,25 @@ func (nav *nav) searchNext() error { } func (nav *nav) searchPrev() error { - last := nav.currDir() - for i := last.ind - 1; i >= 0; i-- { - matched, err := searchMatch(last.files[i].Name(), nav.search) + dir := nav.currDir() + for i := dir.ind - 1; i >= 0; i-- { + matched, err := searchMatch(dir.files[i].Name(), nav.search) if err != nil { return err } if matched { - nav.up(last.ind - i) + nav.up(dir.ind - i) return nil } } if gOpts.wrapscan { - for i := len(last.files) - 1; i > last.ind; i-- { - matched, err := searchMatch(last.files[i].Name(), nav.search) + for i := len(dir.files) - 1; i > dir.ind; i-- { + matched, err := searchMatch(dir.files[i].Name(), nav.search) if err != nil { return err } if matched { - nav.down(i - last.ind) + nav.down(i - dir.ind) return nil } } @@ -1184,12 +1186,12 @@ func (nav *nav) currDir() *dir { } func (nav *nav) currFile() (*file, error) { - last := nav.dirs[len(nav.dirs)-1] + dir := nav.dirs[len(nav.dirs)-1] - if len(last.files) == 0 { + if len(dir.files) == 0 { return nil, fmt.Errorf("empty directory") } - return last.files[last.ind], nil + return dir.files[dir.ind], nil } type indexedSelections struct { diff --git a/ui.go b/ui.go index 8823291..cfaa2b0 100644 --- a/ui.go +++ b/ui.go @@ -579,8 +579,6 @@ func (ui *ui) drawPromptLine(nav *nav) { pwd = filepath.Join("~", strings.TrimPrefix(pwd, gUser.HomeDir)) } - pwd = filepath.Clean(pwd) - sep := string(filepath.Separator) if !strings.HasSuffix(pwd, sep) { @@ -622,12 +620,12 @@ func (ui *ui) drawPromptLine(nav *nav) { func (ui *ui) drawStatLine(nav *nav) { fg, bg := termbox.ColorDefault, termbox.ColorDefault - currDir := nav.currDir() + dir := nav.currDir() ui.msgWin.print(0, 0, fg, bg, ui.msg) - tot := len(currDir.files) - ind := min(currDir.ind+1, tot) + tot := len(dir.files) + ind := min(dir.ind+1, tot) acc := string(ui.keyCount) + string(ui.keyAcc) var progress string @@ -719,13 +717,13 @@ func (ui *ui) draw(nav *nav) { } if gOpts.preview { - f, err := nav.currFile() + curr, err := nav.currFile() if err == nil { preview := ui.wins[len(ui.wins)-1] - if f.IsDir() { + if curr.IsDir() { preview.printDir(ui.dirPrev, nav.selections, nav.saves, ui.colors, ui.icons) - } else if f.Mode().IsRegular() { + } else if curr.Mode().IsRegular() { preview.printReg(ui.regPrev) } }