From 90f0339fa905a71861f471cd92762d81de59f4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Marti=C4=8Dek?= <33220144+tomarticek@users.noreply.github.com> Date: Sun, 27 Jan 2019 15:31:29 +0100 Subject: [PATCH] Implemented line numbers (#133) --- complete.go | 6 ++++++ doc.go | 60 +++++++++++++++++++++++++++------------------------- docstring.go | 60 +++++++++++++++++++++++++++------------------------- eval.go | 12 +++++++++++ lf.1 | 60 +++++++++++++++++++++++++++------------------------- opts.go | 60 +++++++++++++++++++++++++++------------------------- ui.go | 35 ++++++++++++++++++++++++++---- 7 files changed, 173 insertions(+), 120 deletions(-) diff --git a/complete.go b/complete.go index f192a27..159fcc9 100644 --- a/complete.go +++ b/complete.go @@ -106,6 +106,12 @@ var ( "wrapscan", "nowrapscan", "wrapscan!", + "number", + "nonumber", + "number!", + "relativenumber", + "norelativenumber", + "relativenumber!", "findlen", "period", "scrolloff", diff --git a/doc.go b/doc.go index 1ae69af..e6bcaac 100644 --- a/doc.go +++ b/doc.go @@ -94,35 +94,37 @@ keybindings: The following options can be used to customize the behavior of lf: - anchorfind boolean (default on) - color256 boolean (default off) - dircounts boolean (default off) - dirfirst boolean (default on) - drawbox boolean (default off) - globsearch boolean (default off) - hidden boolean (default off) - ignorecase boolean (default on) - ignoredia boolean (default off) - incsearch boolean (default off) - preview boolean (default on) - reverse boolean (default off) - smartcase boolean (default on) - smartdia boolean (default off) - wrapscan boolean (default on) - findlen integer (default 1) (zero to prompt until single match) - period integer (default 0) (zero to disable periodic loading) - scrolloff integer (default 0) - tabstop integer (default 8) - filesep string (default "\n") - ifs string (default '') (not exported if empty) - previewer string (default '') (not filtered if empty) - promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") - shell string (default 'sh') - sortby string (default 'natural') - timefmt string (default 'Mon Jan _2 15:04:05 2006') - ratios string (default '1:2:3') - info string (default '') - shellopts string (default '') + anchorfind boolean (default on) + color256 boolean (default off) + dircounts boolean (default off) + dirfirst boolean (default on) + drawbox boolean (default off) + globsearch boolean (default off) + hidden boolean (default off) + ignorecase boolean (default on) + ignoredia boolean (default off) + incsearch boolean (default off) + preview boolean (default on) + reverse boolean (default off) + smartcase boolean (default on) + smartdia boolean (default off) + wrapscan boolean (default on) + number boolean (default off) + relativenumber boolean (default off) + findlen integer (default 1) (zero to prompt until single match) + period integer (default 0) (zero to disable periodic loading) + scrolloff integer (default 0) + tabstop integer (default 8) + filesep string (default "\n") + ifs string (default '') (not exported if empty) + previewer string (default '') (not filtered if empty) + promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") + shell string (default 'sh') + sortby string (default 'natural') + timefmt string (default 'Mon Jan _2 15:04:05 2006') + ratios string (default '1:2:3') + info string (default '') + shellopts string (default '') The following variables are exported for shell commands: diff --git a/docstring.go b/docstring.go index aa489ab..18e500e 100644 --- a/docstring.go +++ b/docstring.go @@ -97,35 +97,37 @@ keybindings: The following options can be used to customize the behavior of lf: - anchorfind boolean (default on) - color256 boolean (default off) - dircounts boolean (default off) - dirfirst boolean (default on) - drawbox boolean (default off) - globsearch boolean (default off) - hidden boolean (default off) - ignorecase boolean (default on) - ignoredia boolean (default off) - incsearch boolean (default off) - preview boolean (default on) - reverse boolean (default off) - smartcase boolean (default on) - smartdia boolean (default off) - wrapscan boolean (default on) - findlen integer (default 1) (zero to prompt until single match) - period integer (default 0) (zero to disable periodic loading) - scrolloff integer (default 0) - tabstop integer (default 8) - filesep string (default "\n") - ifs string (default '') (not exported if empty) - previewer string (default '') (not filtered if empty) - promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") - shell string (default 'sh') - sortby string (default 'natural') - timefmt string (default 'Mon Jan _2 15:04:05 2006') - ratios string (default '1:2:3') - info string (default '') - shellopts string (default '') + anchorfind boolean (default on) + color256 boolean (default off) + dircounts boolean (default off) + dirfirst boolean (default on) + drawbox boolean (default off) + globsearch boolean (default off) + hidden boolean (default off) + ignorecase boolean (default on) + ignoredia boolean (default off) + incsearch boolean (default off) + preview boolean (default on) + reverse boolean (default off) + smartcase boolean (default on) + smartdia boolean (default off) + wrapscan boolean (default on) + number boolean (default off) + relativenumber boolean (default off) + findlen integer (default 1) (zero to prompt until single match) + period integer (default 0) (zero to disable periodic loading) + scrolloff integer (default 0) + tabstop integer (default 8) + filesep string (default "\n") + ifs string (default '') (not exported if empty) + previewer string (default '') (not filtered if empty) + promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m") + shell string (default 'sh') + sortby string (default 'natural') + timefmt string (default 'Mon Jan _2 15:04:05 2006') + ratios string (default '1:2:3') + info string (default '') + shellopts string (default '') The following variables are exported for shell commands: diff --git a/eval.go b/eval.go index d8cf436..55e4728 100644 --- a/eval.go +++ b/eval.go @@ -143,6 +143,18 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.wrapscan = false case "wrapscan!": gOpts.wrapscan = !gOpts.wrapscan + case "number": + gOpts.number = true + case "nonumber": + gOpts.number = false + case "number!": + gOpts.number = !gOpts.number + case "relativenumber": + gOpts.relativenumber = true + case "norelativenumber": + gOpts.relativenumber = false + case "relativenumber!": + gOpts.relativenumber = !gOpts.relativenumber case "findlen": n, err := strconv.Atoi(e.val) if err != nil { diff --git a/lf.1 b/lf.1 index 127210c..53630b1 100644 --- a/lf.1 +++ b/lf.1 @@ -109,35 +109,37 @@ The following command line commands are provided by lf with default keybindings: The following options can be used to customize the behavior of lf: .PP .EX - anchorfind boolean (default on) - color256 boolean (default off) - dircounts boolean (default off) - dirfirst boolean (default on) - drawbox boolean (default off) - globsearch boolean (default off) - hidden boolean (default off) - ignorecase boolean (default on) - ignoredia boolean (default off) - incsearch boolean (default off) - preview boolean (default on) - reverse boolean (default off) - smartcase boolean (default on) - smartdia boolean (default off) - wrapscan boolean (default on) - findlen integer (default 1) (zero to prompt until single match) - period integer (default 0) (zero to disable periodic loading) - scrolloff integer (default 0) - tabstop integer (default 8) - filesep string (default "\en") - ifs string (default '') (not exported if empty) - previewer string (default '') (not filtered if empty) - promptfmt string (default "\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w/\e033[0m\e033[1m%f\e033[0m") - shell string (default 'sh') - sortby string (default 'natural') - timefmt string (default 'Mon Jan _2 15:04:05 2006') - ratios string (default '1:2:3') - info string (default '') - shellopts string (default '') + anchorfind boolean (default on) + color256 boolean (default off) + dircounts boolean (default off) + dirfirst boolean (default on) + drawbox boolean (default off) + globsearch boolean (default off) + hidden boolean (default off) + ignorecase boolean (default on) + ignoredia boolean (default off) + incsearch boolean (default off) + preview boolean (default on) + reverse boolean (default off) + smartcase boolean (default on) + smartdia boolean (default off) + wrapscan boolean (default on) + number boolean (default off) + relativenumber boolean (default off) + findlen integer (default 1) (zero to prompt until single match) + period integer (default 0) (zero to disable periodic loading) + scrolloff integer (default 0) + tabstop integer (default 8) + filesep string (default "\en") + ifs string (default '') (not exported if empty) + previewer string (default '') (not filtered if empty) + promptfmt string (default "\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w/\e033[0m\e033[1m%f\e033[0m") + shell string (default 'sh') + sortby string (default 'natural') + timefmt string (default 'Mon Jan _2 15:04:05 2006') + ratios string (default '1:2:3') + info string (default '') + shellopts string (default '') .EE .PP The following variables are exported for shell commands: diff --git a/opts.go b/opts.go index 1a56d89..e1ac65d 100644 --- a/opts.go +++ b/opts.go @@ -25,35 +25,37 @@ type sortType struct { } var gOpts struct { - anchorfind bool - color256 bool - dircounts bool - drawbox bool - globsearch bool - ignorecase bool - incsearch bool - preview bool - smartcase bool - ignoredia bool - smartdia bool - wrapscan bool - findlen int - period int - scrolloff int - tabstop int - filesep string - ifs string - previewer string - promptfmt string - shell string - timefmt string - ratios []int - info []string - shellopts []string - keys map[string]expr - cmdkeys map[string]expr - cmds map[string]expr - sortType sortType + anchorfind bool + color256 bool + dircounts bool + drawbox bool + globsearch bool + ignorecase bool + incsearch bool + preview bool + smartcase bool + ignoredia bool + smartdia bool + wrapscan bool + number bool + relativenumber bool + findlen int + period int + scrolloff int + tabstop int + filesep string + ifs string + previewer string + promptfmt string + shell string + timefmt string + ratios []int + info []string + shellopts []string + keys map[string]expr + cmdkeys map[string]expr + cmds map[string]expr + sortType sortType } func init() { diff --git a/ui.go b/ui.go index 2a1e029..5151aad 100644 --- a/ui.go +++ b/ui.go @@ -267,18 +267,45 @@ func (win *win) printDir(dir *dir, selections map[string]int, saves map[string]b return } + var lnwidth int = 0; + var lnformat string = ""; + + if (gOpts.number || gOpts.relativenumber) { + lnwidth = 1; + for j := 10; j < len(dir.files); j *= 10 { + lnwidth++ + } + lnformat = fmt.Sprintf("%%%d.d ", lnwidth) + } + for i, f := range dir.files[beg:end] { fg, bg = colors.get(f) + if (lnwidth > 0) { + var ln string + + if (gOpts.number && (!gOpts.relativenumber || i == dir.pos)) { + ln = fmt.Sprintf(lnformat, i + 1 + beg) + } else if (gOpts.relativenumber) { + if (i < dir.pos) { + ln = fmt.Sprintf(lnformat, dir.pos - i) + } else { + ln = fmt.Sprintf(lnformat, i - dir.pos) + } + } + + win.print(0, i, termbox.ColorYellow, bg, ln) + } + path := filepath.Join(dir.path, f.Name()) if _, ok := selections[path]; ok { - win.print(0, i, fg, termbox.ColorMagenta, " ") + win.print(lnwidth, i, fg, termbox.ColorMagenta, " ") } else if cp, ok := saves[path]; ok { if cp { - win.print(0, i, fg, termbox.ColorYellow, " ") + win.print(lnwidth, i, fg, termbox.ColorYellow, " ") } else { - win.print(0, i, fg, termbox.ColorRed, " ") + win.print(lnwidth, i, fg, termbox.ColorRed, " ") } } @@ -321,7 +348,7 @@ func (win *win) printDir(dir *dir, selections map[string]int, saves map[string]b s = append(s, ' ') - win.print(1, i, fg, bg, string(s)) + win.print(lnwidth+1, i, fg, bg, string(s)) } }