diff --git a/complete.go b/complete.go index fd166188..d00a66de 100644 --- a/complete.go +++ b/complete.go @@ -224,7 +224,7 @@ func matchExec(s string) (matches []string, longest string) { } func matchFile(s string) (matches []string, longest string) { - dir := strings.Replace(s, "~", gUser.HomeDir, -1) + dir := replaceTilde(s) if !filepath.IsAbs(dir) { wd, err := os.Getwd() diff --git a/eval.go b/eval.go index 0ef96045..9a4bdf5e 100644 --- a/eval.go +++ b/eval.go @@ -232,7 +232,7 @@ func (e *setExpr) eval(app *app, args []string) { case "ifs": gOpts.ifs = e.val case "previewer": - gOpts.previewer = strings.Replace(e.val, "~", gUser.HomeDir, -1) + gOpts.previewer = replaceTilde(e.val) case "promptfmt": gOpts.promptfmt = e.val case "shell": @@ -969,7 +969,7 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.echoerr("source: requires an argument") return } - app.readFile(strings.Replace(e.args[0], "~", gUser.HomeDir, -1)) + app.readFile(replaceTilde(e.args[0])) app.ui.loadFileInfo(app.nav) case "push": if len(e.args) != 1 { diff --git a/misc.go b/misc.go index 14e23164..dac73130 100644 --- a/misc.go +++ b/misc.go @@ -207,6 +207,13 @@ func max(a, b int) int { return b } +func replaceTilde(s string) string { + if strings.HasPrefix(s, "~") { + s = strings.Replace(s, "~", gUser.HomeDir, 1) + } + return s +} + // We don't need no generic code // We don't need no type control // No dark templates in compiler diff --git a/nav.go b/nav.go index f717aee4..f983f787 100644 --- a/nav.go +++ b/nav.go @@ -855,7 +855,7 @@ func (nav *nav) sync() error { } func (nav *nav) cd(wd string) error { - wd = strings.Replace(wd, "~", gUser.HomeDir, -1) + wd = replaceTilde(wd) wd = filepath.Clean(wd) if !filepath.IsAbs(wd) { @@ -872,7 +872,7 @@ func (nav *nav) cd(wd string) error { } func (nav *nav) sel(path string) error { - path = strings.Replace(path, "~", gUser.HomeDir, -1) + path = replaceTilde(path) path = filepath.Clean(path) lstat, err := os.Stat(path) diff --git a/ui.go b/ui.go index 722f2abe..2cb7f111 100644 --- a/ui.go +++ b/ui.go @@ -275,6 +275,9 @@ func (win *win) printDir(dir *dir, selections map[string]int, saves map[string]b if gOpts.number || gOpts.relativenumber { lnwidth = 1 + if gOpts.number && gOpts.relativenumber { + lnwidth++ + } for j := 10; j < len(dir.files); j *= 10 { lnwidth++ } @@ -287,13 +290,18 @@ func (win *win) printDir(dir *dir, selections map[string]int, saves map[string]b if lnwidth > 0 { var ln string - if gOpts.number && (!gOpts.relativenumber || i == dir.pos) { + if gOpts.number && (!gOpts.relativenumber) { ln = fmt.Sprintf(lnformat, i+1+beg) } else if gOpts.relativenumber { - if i < dir.pos { + switch { + case i < dir.pos: ln = fmt.Sprintf(lnformat, dir.pos-i) - } else { + case i > dir.pos: ln = fmt.Sprintf(lnformat, i-dir.pos) + case gOpts.number: + ln = fmt.Sprintf(fmt.Sprintf("%%%d.d ", lnwidth-1), i+1+beg) + default: + ln = "" } } @@ -344,9 +352,9 @@ func (win *win) printDir(dir *dir, selections map[string]int, saves map[string]b if len(info) > 0 && win.w-2 > 2*len(info) { if win.w-2 > w+len(info) { - s = runeSliceWidthRange(s, 0, win.w-3-len(info)) + s = runeSliceWidthRange(s, 0, win.w-3-len(info)-lnwidth) } else { - s = runeSliceWidthRange(s, 0, win.w-4-len(info)) + s = runeSliceWidthRange(s, 0, win.w-4-len(info)-lnwidth) s = append(s, '~') } for _, r := range info {