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 user defined options #865

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
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ The following options can be used to customize the behavior of lf:
waitmsg string (default 'Press any key to continue')
wrapscan bool (default on)
wrapscroll bool (default off)
user_{key} string (default none)

The following environment variables are exported for shell commands:

Expand All @@ -172,6 +173,7 @@ The following environment variables are exported for shell commands:
PAGER
SHELL
lf_{option}
lf_user_{key}

The following special shell commands are used to customize the behavior of lf when defined:

Expand Down Expand Up @@ -795,6 +797,10 @@ Searching can wrap around the file list.

Scrolling can wrap around the file list.

user_{key} string (default none)

Any option that is prefixed with 'user_' is a user defined option and can be set to any string. Inside a user defined command the value will be provided in the `lf_user_{key}` environment variable. These options are not used by lf and are not persisted.

Environment Variables

The following variables are exported for shell commands:
Expand Down
9 changes: 9 additions & 0 deletions docstring.go

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

7 changes: 6 additions & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ func (e *setExpr) eval(app *app, args []string) {

gOpts.truncatechar = e.val
default:
app.ui.echoerrf("unknown option: %s", e.opt)
// any key with the prefix user_ is accepted as a user defined option
if strings.HasPrefix(e.opt, "user_") {
gOpts.user[e.opt[5:]] = e.val
} else {
app.ui.echoerrf("unknown option: %s", e.opt)
}
return
}
app.ui.loadFileInfo(app.nav)
Expand Down
8 changes: 8 additions & 0 deletions lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ The following options can be used to customize the behavior of lf:
waitmsg string (default 'Press any key to continue')
wrapscan bool (default on)
wrapscroll bool (default off)
user_{key} string (default none)
.EE
.PP
The following environment variables are exported for shell commands:
Expand All @@ -193,6 +194,7 @@ The following environment variables are exported for shell commands:
PAGER
SHELL
lf_{option}
lf_user_{key}
.EE
.PP
The following special shell commands are used to customize the behavior of lf when defined:
Expand Down Expand Up @@ -969,6 +971,12 @@ Searching can wrap around the file list.
.EE
.PP
Scrolling can wrap around the file list.
.PP
.EX
user_{key} string (default none)
.EE
.PP
Any option that is prefixed with 'user_' is a user defined option and can be set to any string. Inside a user defined command the value will be provided in the `lf_user_{key}` environment variable. These options are not used by lf and are not persisted.
.SH ENVIRONMENT VARIABLES
The following variables are exported for shell commands: These are referred with a '$' prefix on POSIX shells (e.g. '$f'), between '%' characters on Windows cmd (e.g. '%f%'), and with a '$env:' prefix on Windows powershell (e.g. '$env:f').
.PP
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func exportOpts() {

dirfirst := strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0)
os.Setenv("lf_dirfirst", dirfirst)
} else if name == "lf_user" {
// set each user option
for key, value := range gOpts.user {
os.Setenv(name+"_"+key, value)
}
} else {
field := e.Field(i)
value := fieldToString(field)
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var gOpts struct {
keys map[string]expr
cmdkeys map[string]expr
cmds map[string]expr
user map[string]string
sortType sortType
tempmarks string
tagfmt string
Expand Down Expand Up @@ -231,6 +232,7 @@ func init() {
gOpts.cmdkeys["<a-t>"] = &callExpr{"cmd-transpose-word", nil, 1}

gOpts.cmds = make(map[string]expr)
gOpts.user = make(map[string]string)

setDefaults()
}