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

Added statcmd option to run external commands for file stats #1347

Closed
wants to merge 1 commit into from
Closed
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 @@ -214,6 +214,7 @@ var (
"sortby",
"statfmt",
"timefmt",
"statcmd",
"tempmarks",
"tagfmt",
"infotimefmtnew",
Expand Down
10 changes: 9 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ The following options can be used to customize the behavior of lf:
smartcase bool (default true)
smartdia bool (default false)
sortby string (default 'natural')
statcmd string (default "")
statfmt string (default "\033[36m%p\033[0m| %c| %u| %g| %s| %t| -> %l")
tabstop int (default 8)
tagfmt string (default "\033[31m")
Expand Down Expand Up @@ -925,10 +926,17 @@ This option has no effect when 'ignoredia' is disabled.
Sort type for directories.
Currently supported sort types are 'natural', 'name', 'size', 'time', 'ctime', 'atime', and 'ext'.

statcmd string (default "")

Command to be run when using the '%C' expansion in statfmt. The '%C' is replaced with the output of the specified command.
lf always waits for the command to finish, before continuing. It is the users responsibility to ensure the command finishes quickly, so that lf does not hang.
The command is run in mostly the same way, as any other shell command, with respect to the shell, shellflag and shellopts options.
That also means you can use environment variables, like in any other shell command; most notably "$f" to access the currently selected file.

statfmt string (default "\033[36m%p\033[0m| %c| %u| %g| %s| %t| -> %l")

Format string of the file info shown in the bottom left corner.
Special expansions are provided, '%p' as the file permissions, '%c' as the link count, '%u' as the user, '%g' as the group, '%s' as the file size, '%t' as the last modified time, and '%l' as the link target.
Special expansions are provided, '%p' as the file permissions, '%c' as the link count, '%u' as the user, '%g' as the group, '%s' as the file size, '%t' as the last modified time, '%l' as the link target, and '%C' as the output of the statcmd (see above).
The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown.

tabstop int (default 8)
Expand Down
19 changes: 16 additions & 3 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 @@ -803,6 +803,8 @@ func (e *setExpr) eval(app *app, args []string) {
}
app.nav.sort()
app.ui.sort()
case "statcmd":
gOpts.statcmd = e.val
case "statfmt":
gOpts.statfmt = e.val
case "tabstop":
Expand Down
9 changes: 8 additions & 1 deletion lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ The following options can be used to customize the behavior of lf:
smartcase bool (default true)
smartdia bool (default false)
sortby string (default 'natural')
statcmd string (default "")
statfmt string (default "\e033[36m%p\e033[0m| %c| %u| %g| %s| %t| -> %l")
tabstop int (default 8)
tagfmt string (default "\e033[31m")
Expand Down Expand Up @@ -1101,11 +1102,17 @@ Override 'ignoredia' option when the pattern contains a character with diacritic
.PP
Sort type for directories. Currently supported sort types are 'natural', 'name', 'size', 'time', 'ctime', 'atime', and 'ext'.
.PP
.EX
statcmd string (default "")
.EE
.PP
Command to be run when using the '%C' expansion in statfmt. The '%C' is replaced with the output of the specified command. lf always waits for the command to finish, before continuing. It is the users responsibility to ensure the command finishes quickly, so that lf does not hang. The command is run in mostly the same way, as any other shell command, with respect to the shell, shellflag and shellopts options. That also means you can use environment variables, like in any other shell command; most notably "$f" to access the currently selected file.
.PP
.EX
statfmt string (default "\e033[36m%p\e033[0m| %c| %u| %g| %s| %t| -> %l")
.EE
.PP
Format string of the file info shown in the bottom left corner. Special expansions are provided, '%p' as the file permissions, '%c' as the link count, '%u' as the user, '%g' as the group, '%s' as the file size, '%t' as the last modified time, and '%l' as the link target. The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown.
Format string of the file info shown in the bottom left corner. Special expansions are provided, '%p' as the file permissions, '%c' as the link count, '%u' as the user, '%g' as the group, '%s' as the file size, '%t' as the last modified time, '%l' as the link target, and '%C' as the output of the statcmd (see above). The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown.
.PP
.EX
tabstop int (default 8)
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var gOpts struct {
shellflag string
statfmt string
timefmt string
statcmd string
infotimefmtnew string
infotimefmtold string
truncatechar string
Expand Down Expand Up @@ -136,6 +137,7 @@ func init() {
gOpts.timefmt = time.ANSIC
gOpts.infotimefmtnew = "Jan _2 15:04"
gOpts.infotimefmtold = "Jan _2 2006"
gOpts.statcmd = ""
gOpts.truncatechar = "~"
gOpts.truncatepct = 100
gOpts.ratios = []int{1, 2, 3}
Expand Down
16 changes: 16 additions & 0 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,22 @@ func (ui *ui) loadFileInfo(nav *nav) {
replace("%s", humanize(curr.Size()))
replace("%t", curr.ModTime().Format(gOpts.timefmt))
replace("%l", curr.linkTarget)
if strings.Contains(statfmt, "%C") {
if gOpts.statcmd == "" {
ui.echoerrf("statcmd is empty")
return
}
nav.exportFiles()
ui.exportSizes()
exportOpts()
cmd := shellCommand(gOpts.statcmd, nil)
out, err := cmd.Output()
if err != nil {
ui.echoerrf("statcmd: %s", err)
return
}
replace("%C", string(out))
}

fileInfo := ""
for _, section := range strings.Split(statfmt, "\x1f") {
Expand Down