Syntax Demonstration of Using Lumesh to Write lf Configuration Files
The lf file manager is a powerful TUI file manager that supports highly flexible custom operations. It is my favorite file manager. Below are some demonstrations of command configurations written for lf using Lumesh.
1. Variable Definition and Pipeline Operations#
1cmd all-cmd ${{
2 let cmd = lf -remote `query $id cmds` | .lines() | .drop(1) | \
3 .map(x -> {x.split("\t\t", $x) | .first()}) | Ui.pick "select cmd:"
4 lf -remote `send $id :$cmd`
5}}lf -remotequery $id cmds`` retrieves available commands in lf.letdefines a variable.- The pipeline operator
|is used for data stream processing. .lines()is a string processing method..drop(1)is a list operation that skips the first element..map()is functional programming using a lambda expressionx -> {...}.Ui.pickis an interactive selector.
2. String Processing and Table Operations#
1cmd history-dir ${{
2 let hist = lf -remote `query $id jumps` | Into.table('jump','path') | .drop(1) | Ui.pick "choose history:"
3 lf --remote `send $id cd ${$hist.path}`
4}}lf -remotequery $id jumps`` retrieves the history directories in lf.Into.table()converts data into a table structure.- String interpolation
${$hist.path}syntax. - Object property access
$hist.path.
3. Conditional Statements and Pattern Matching#
1cmd toggle-preview %{{
2 match $lf_preview {
3 true => lf -remote `send $id :set nopreview; set ratios 1:5`
4 _ => lf -remote `send $id :set preview; set ratios 1:2:3`
5 }
6}}Lumesh's match pattern matching syntax is similar to Rust's match expression. Here, it matches the string true; if a boolean value is needed, it should be capitalized as True.
4. Conditional Expressions and Ternary Operations#
1cmd select-files &{{
2 let htag= $lf_hidden ? '-H' : ''
3 let r=fd --exact-depth 1 $argv $htag -c never -j 1 -0 | xargs -0 printf ' %q'
4 lf -remote `send $id :unselect; toggle $r`
5}}- Ternary conditional operator
condition ? value1 : value2. - Variable usage in command line:
$argvrepresents the parameters received by the function.
5. Functional Programming and List Operations#
1cmd yank-name &{{
2 $fx.lines() | .map(Fs.base_name) | .join("\n") | wl-copy
3}}- Method chaining
.lines().map().join(). Fs.base_nameis a filesystem module function.- Pipeline operations pass results to external commands.
6. User Interaction#
1cmd delete ${{
2 println '=====DELETE=====' $fx '================'
3 if Ui.confirm('Delete these files [y/n]:'){
4 $lf_user_wheel rm -rf $fx.lines()
5 }
6}}printlnis an output function.Ui.confirmis a confirmation dialog.ifis a conditional statement.
7. Complex Data Processing and Loops#
1cmd mpaste %{{
2 let load=Fs.read ~/.local/share/lf/files | .lines()
3 let files = $load.drop(1)
4 let file_count = len($files)
5 if $file_count==0 {
6 print 'No files yanked'
7 exit 0
8 }
9 let mode=$load.at(0)
10 let base_names = $files.map(Fs.base_name)
11 let ans = read `$mode $file_count files? [y/N]`
12 if $ans == 'y' {
13 match $mode {
14 copy => {
15 $lf_user_wheel cp -r $argv -- $files '.'
16 let tg='='
17 }
18 move => {
19 $lf_user_wheel mv -- $files '.'
20 let tg='>'
21 }
22 }
23 for file in $base_names {
24 lf -remote `send $id :select "$file"; tag $tg`
25 }
26 }
27}}Fs.readreads a file.len()is a length function.readis a user input function.foris a loop statement.- Complex
matchpattern matching.
8. Regular Expressions and String Operations#
1cmd extract-to %{{
2 let dest = $argv[0] ?: {print 'Cancelled'; exit 0}
3 if (Regex.match '\.([gb7xs]z|t[gbx]z|zip|zst|bz2|lz4|lzma|tar|rar|br)$' $f) {
4 $lf_user_wheel ouch -q decompress --dir $dest $f
5 let base_name = Fs.base_name($f)
6 lf -remote `send $id :cd $dest; select $base_name; tag '^'`
7 }else{
8 print 'Unsupported file extension'
9 }
10}}- The null coalescing operator
?:. Regex.matchfor regular expression matching.- Block expression
{print 'Cancelled'; exit 0}.
Notes#
This configuration file perfectly showcases the powerful capabilities of Lumesh as a shell scripting language: it combines the syntactic features of modern programming languages (such as pattern matching, functional programming, and error handling) with the command execution capabilities of traditional shells. The design philosophy of Lumesh, "write like Python/JS, work like bash," is fully reflected in this example.
Wiki pages you might want to explore: