Skip to content

Commit

Permalink
Add a selection mode that limits the selection to the current directo…
Browse files Browse the repository at this point in the history
…ry. (#849)
  • Loading branch information
laktak authored Oct 15, 2022
1 parent 479ed92 commit 21a32a7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ var (
"cleaner",
"promptfmt",
"ratios",
"selmode",
"shell",
"shellflag",
"shellopts",
Expand Down
7 changes: 7 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ The following options can be used to customize the behavior of lf:
relativenumber bool (default off)
reverse bool (default off)
scrolloff int (default 0)
selmode string (default 'all')
shell string (default 'sh' for Unix and 'cmd' for Windows)
shellflag string (default '-c' for Unix and '/c' for Windows)
shellopts []string (default '')
Expand Down Expand Up @@ -742,6 +743,12 @@ When 'number' is enabled, current line shows the absolute position, otherwise no
Reverse the direction of sort.
selmode string (default 'all')
Selection mode for commands.
When set to 'all' it will use the selected files from all directories.
When set to 'dir' it will only use the selected files in the current directory.
scrolloff int (default 0)
Minimum number of offset lines shown at all times in the top and the bottom of the screen when scrolling.
Expand Down
7 changes: 7 additions & 0 deletions docstring.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.ratios = rats
app.ui.wins = getWins(app.ui.screen)
app.ui.loadFile(app.nav, true)
case "selmode":
gOpts.selmode = e.val
case "shell":
gOpts.shell = e.val
case "shellflag":
Expand Down
7 changes: 7 additions & 0 deletions lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ The following options can be used to customize the behavior of lf:
relativenumber bool (default off)
reverse bool (default off)
scrolloff int (default 0)
selmode string (default 'all')
shell string (default 'sh' for Unix and 'cmd' for Windows)
shellflag string (default '-c' for Unix and '/c' for Windows)
shellopts []string (default '')
Expand Down Expand Up @@ -894,6 +895,12 @@ Show the position number relative to the current line. When 'number' is enabled,
.PP
Reverse the direction of sort.
.PP
.EX
selmode string (default 'all')
.EE
.PP
Selection mode for commands. When set to 'all' it will use the selected files from all directories. When set to 'dir' it will only use the selected files in the current directory.
.PP
.EX
scrolloff int (default 0)
.EE
Expand Down
22 changes: 16 additions & 6 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,27 +1838,37 @@ func (m indexedSelections) Swap(i, j int) {
func (m indexedSelections) Less(i, j int) bool { return m.indices[i] < m.indices[j] }

func (nav *nav) currSelections() []string {

currDirOnly := gOpts.selmode == "dir"
currDirPath := ""
if currDirOnly {
// select only from this directory
currDirPath = nav.currDir().path
}

paths := make([]string, 0, len(nav.selections))
indices := make([]int, 0, len(nav.selections))
for path, index := range nav.selections {
paths = append(paths, path)
indices = append(indices, index)
if !currDirOnly || filepath.Dir(path) == currDirPath {
paths = append(paths, path)
indices = append(indices, index)
}
}
sort.Sort(indexedSelections{paths: paths, indices: indices})
return paths
}

func (nav *nav) currFileOrSelections() (list []string, err error) {
if len(nav.selections) == 0 {
sel := nav.currSelections()

if len(sel) == 0 {
curr, err := nav.currFile()
if err != nil {
return nil, errors.New("no file selected")
}

return []string{curr.path}, nil
}

return nav.currSelections(), nil
return sel, nil
}

func (nav *nav) calcDirSize() error {
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var gOpts struct {
previewer string
cleaner string
promptfmt string
selmode string
shell string
shellflag string
timefmt string
Expand Down Expand Up @@ -112,6 +113,7 @@ func init() {
gOpts.previewer = ""
gOpts.cleaner = ""
gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"
gOpts.selmode = "all"
gOpts.shell = gDefaultShell
gOpts.shellflag = gDefaultShellFlag
gOpts.timefmt = time.ANSIC
Expand Down
5 changes: 3 additions & 2 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,9 @@ func (ui *ui) drawStatLine(nav *nav) {
}
}

if len(nav.selections) > 0 {
selection += fmt.Sprintf(" \033[35;7m %d \033[0m", len(nav.selections))
currSelections := nav.currSelections()
if len(currSelections) > 0 {
selection += fmt.Sprintf(" \033[35;7m %d \033[0m", len(currSelections))
}

if len(dir.filter) != 0 {
Expand Down

0 comments on commit 21a32a7

Please sign in to comment.