fix tilde expansion and numbering (#373)

* Fixed issue #346 and changed the format of numbers when both relativenumber and number are set.

* Fixed issue #349 (incorrect replacement of '~' with user's home direcotry).

* Changed where appropriate so that tilde replacements happens correctly.

* Added a function to replace tilde with user's home directory, only when it should.

* Renamed ReplaceTilde to replaceTilde so that it won't get exported and removed some ';' at the end of some lines.
This commit is contained in:
gotroyb127 authored and GitHub committed 2020-06-07 20:53:25 +03:00
1 parent c49140ab36
commit 41e0bb32cd
5 files changed
+25 -10

No files matched your search

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