Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a selection mode #849

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,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 @@ -144,6 +144,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 @@ -728,6 +729,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 @@ -384,6 +384,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 @@ -163,6 +163,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 @@ -881,6 +882,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 @@ -1739,27 +1739,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 @@ -59,6 +59,7 @@ var gOpts struct {
previewer string
cleaner string
promptfmt string
selmode string
shell string
shellflag string
timefmt string
Expand Down Expand Up @@ -110,6 +111,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 @@ -774,8 +774,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