-
Notifications
You must be signed in to change notification settings - Fork 321
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
Support optional fields for statfmt
option
#1337
Support optional fields for statfmt
option
#1337
Conversation
@joelim-work I don't have a strong opinion about this as I don't use this myself so feel free to merge this if you like. However, my two cents is that I'm not sure about the |
@gokcehan I guess your concern is fair, the irony is that I chose
Another alternative is to provide an escape character, for example I'm not sure how useful the character
I'm not sure of how to get feedback on this, we could either merge this and see what happens, or post this as a discussion first. |
@joelim-work I guess I agree it wouldn't be an issue most of the time. So feel free to merge this whenever you feel like. You don't even need to add the literal escape (i.e. |
@gokcehan Thank, I wanted to get this change in before I agree that it would be better to just keep the current implementation for now, and only add statfmt := gOpts.statfmt
statfmt = strings.ReplaceAll(statfmt, "%|", "\x1a")
statfmt = strings.ReplaceAll(statfmt, "|", "\x1f")
replace := func(s string, val string) {
if val == "" {
val = "\x00"
}
statfmt = strings.ReplaceAll(statfmt, s, val)
}
replace("%p", curr.Mode().String())
replace("%c", linkCount(curr))
replace("%u", userName(curr))
replace("%g", groupName(curr))
replace("%s", humanize(curr.Size()))
replace("%t", curr.ModTime().Format(gOpts.timefmt))
replace("%l", curr.linkTarget)
fileInfo := ""
for _, section := range strings.Split(statfmt, "\x1f") {
if !strings.Contains(section, "\x00") {
fileInfo += section
}
}
fileInfo = strings.ReplaceAll(fileInfo, "\x1a", "|")
ui.echo(fileInfo) |
For the
statfmt
option, I finally found a way to support display text depending on whether a field exists or not. The idea is to divide the format string into sections, and if any section contains a failed expansion (blank string), then the section is discarded and not shown.Benefits:
%l
), and the link target with an arrow (%L
).In the example given in #1288, the
\033[36m %l \033[0m
part for displaying the link target would show as two cyan spaces if there wasn't one, which is not ideal. With optional fields supported, this can now work:There are a couple of limitations now, though I don't think it will matter in practice:
|
character cannot appear in the format string, as it now has a special meaning.0x00
) or a unit separator byte (0x1f
) as they now have a special meaning (failed expansion, and field delimiter, respectively).