Skip to content

Commit 2ab4b14

Browse files
authored
Add statfmt option (#1288)
* Add `fileinfofmt` option * echof is now unused * Use different default for Windows * Add new line to docs * Indent placeholders description in documentation * Change option name to `statfmt` * Use plain text format for documentation
1 parent 59f3219 commit 2ab4b14

File tree

9 files changed

+52
-19
lines changed

9 files changed

+52
-19
lines changed

complete.go

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ var (
211211
"shellflag",
212212
"shellopts",
213213
"sortby",
214+
"statfmt",
214215
"timefmt",
215216
"tempmarks",
216217
"tagfmt",

doc.go

+7
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ The following options can be used to customize the behavior of lf:
166166
smartcase bool (default true)
167167
smartdia bool (default false)
168168
sortby string (default 'natural')
169+
statfmt string (default "\033[36m%p\033[0m %c %u %g %s %t %L")
169170
tabstop int (default 8)
170171
tagfmt string (default "\033[31m")
171172
tempmarks string (default '')
@@ -909,6 +910,12 @@ This option has no effect when 'ignoredia' is disabled.
909910
Sort type for directories.
910911
Currently supported sort types are 'natural', 'name', 'size', 'time', 'ctime', 'atime', and 'ext'.
911912
913+
statfmt string (default "\033[36m%p\033[0m %c %u %g %s %t %L")
914+
915+
Format string of the file info shown in the bottom left corner.
916+
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 if it exists (otherwise a blank string). '%L' is the same as '%l' but with an arrow '-> ' prepended.
917+
On Windows, the link count, user and group fields are not supported and will be replaced with a blank string if specified. The default for Windows is "\033[36m%p\033[0m %s %t %L".
918+
912919
tabstop int (default 8)
913920
914921
Number of space characters to show for horizontal tabulation (U+0009) character.

docstring.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eval.go

+2
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ func (e *setExpr) eval(app *app, args []string) {
801801
}
802802
app.nav.sort()
803803
app.ui.sort()
804+
case "statfmt":
805+
gOpts.statfmt = e.val
804806
case "tabstop":
805807
n, err := strconv.Atoi(e.val)
806808
if err != nil {

lf.1

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ The following options can be used to customize the behavior of lf:
185185
smartcase bool (default true)
186186
smartdia bool (default false)
187187
sortby string (default 'natural')
188+
statfmt string (default "\e033[36m%p\e033[0m %c %u %g %s %t %L")
188189
tabstop int (default 8)
189190
tagfmt string (default "\e033[31m")
190191
tempmarks string (default '')
@@ -1080,6 +1081,12 @@ Override 'ignoredia' option when the pattern contains a character with diacritic
10801081
.PP
10811082
Sort type for directories. Currently supported sort types are 'natural', 'name', 'size', 'time', 'ctime', 'atime', and 'ext'.
10821083
.PP
1084+
.EX
1085+
statfmt string (default "\e033[36m%p\e033[0m %c %u %g %s %t %L")
1086+
.EE
1087+
.PP
1088+
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 if it exists (otherwise a blank string). '%L' is the same as '%l' but with an arrow '-> ' prepended. On Windows, the link count, user and group fields are not supported and will be replaced with a blank string if specified. The default for Windows is "\e033[36m%p\e033[0m %s %t %L".
1089+
.PP
10831090
.EX
10841091
tabstop int (default 8)
10851092
.EE

opts.go

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var gOpts struct {
6767
selmode string
6868
shell string
6969
shellflag string
70+
statfmt string
7071
timefmt string
7172
infotimefmtnew string
7273
infotimefmtold string

os.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/user"
1111
"path/filepath"
1212
"runtime"
13+
"strconv"
1314
"strings"
1415
"syscall"
1516

@@ -164,6 +165,8 @@ func setDefaults() {
164165

165166
gOpts.cmds["doc"] = &execExpr{"$", `"$lf" -doc | $PAGER`}
166167
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
168+
169+
gOpts.statfmt = "\033[36m%p\033[0m %c %u %g %s %t %L"
167170
}
168171

169172
func setUserUmask() {
@@ -190,7 +193,7 @@ func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool {
190193
func userName(f os.FileInfo) string {
191194
if stat, ok := f.Sys().(*syscall.Stat_t); ok {
192195
if u, err := user.LookupId(fmt.Sprint(stat.Uid)); err == nil {
193-
return fmt.Sprintf("%v ", u.Username)
196+
return u.Username
194197
}
195198
}
196199
return ""
@@ -199,15 +202,15 @@ func userName(f os.FileInfo) string {
199202
func groupName(f os.FileInfo) string {
200203
if stat, ok := f.Sys().(*syscall.Stat_t); ok {
201204
if g, err := user.LookupGroupId(fmt.Sprint(stat.Gid)); err == nil {
202-
return fmt.Sprintf("%v ", g.Name)
205+
return g.Name
203206
}
204207
}
205208
return ""
206209
}
207210

208211
func linkCount(f os.FileInfo) string {
209212
if stat, ok := f.Sys().(*syscall.Stat_t); ok {
210-
return fmt.Sprintf("%v ", stat.Nlink)
213+
return strconv.FormatUint(stat.Nlink, 10)
211214
}
212215
return ""
213216
}

os_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ func setDefaults() {
123123

124124
gOpts.cmds["doc"] = &execExpr{"!", "%lf% -doc | %PAGER%"}
125125
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
126+
127+
gOpts.statfmt = "\033[36m%p\033[0m %s %t %L"
126128
}
127129

128130
func setUserUmask() {}

ui.go

+14-16
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ func (ui *ui) echo(msg string) {
648648
ui.msg = msg
649649
}
650650

651-
func (ui *ui) echof(format string, a ...interface{}) {
652-
ui.echo(fmt.Sprintf(format, a...))
653-
}
654-
655651
func (ui *ui) echomsg(msg string) {
656652
ui.msg = msg
657653
log.Print(msg)
@@ -722,19 +718,21 @@ func (ui *ui) loadFileInfo(nav *nav) {
722718
return
723719
}
724720

725-
var linkTarget string
721+
linkTargetArrow := ""
726722
if curr.linkTarget != "" {
727-
linkTarget = " -> " + curr.linkTarget
728-
}
729-
730-
ui.echof("%v %v%v%v%4s %v%s",
731-
curr.Mode(),
732-
linkCount(curr), // optional
733-
userName(curr), // optional
734-
groupName(curr), // optional
735-
humanize(curr.Size()),
736-
curr.ModTime().Format(gOpts.timefmt),
737-
linkTarget)
723+
linkTargetArrow = "-> " + curr.linkTarget
724+
}
725+
726+
fileInfo := gOpts.statfmt
727+
fileInfo = strings.Replace(fileInfo, "%p", curr.Mode().String(), -1)
728+
fileInfo = strings.Replace(fileInfo, "%c", linkCount(curr), -1)
729+
fileInfo = strings.Replace(fileInfo, "%u", userName(curr), -1)
730+
fileInfo = strings.Replace(fileInfo, "%g", groupName(curr), -1)
731+
fileInfo = strings.Replace(fileInfo, "%s", humanize(curr.Size()), -1)
732+
fileInfo = strings.Replace(fileInfo, "%t", curr.ModTime().Format(gOpts.timefmt), -1)
733+
fileInfo = strings.Replace(fileInfo, "%l", curr.linkTarget, -1)
734+
fileInfo = strings.Replace(fileInfo, "%L", linkTargetArrow, -1)
735+
ui.echo(fileInfo)
738736
}
739737

740738
func (ui *ui) drawPromptLine(nav *nav) {

0 commit comments

Comments
 (0)