From 422cd6b4591d6cbcbfc8d1bd222dd0b5247b1cf2 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Thu, 7 Sep 2017 23:01:57 +0300 Subject: [PATCH] change default file separator to newline Mentioned in #47. --- doc.go | 43 ++++++++++++++++----------------- docstring.go | 43 ++++++++++++++++----------------- etc/lfrc.example | 62 +++++++++++++++++++++++++----------------------- eval_test.go | 42 ++++++++++++++++---------------- opts.go | 2 +- 5 files changed, 94 insertions(+), 98 deletions(-) diff --git a/doc.go b/doc.go index a7facdc..62dda5f 100644 --- a/doc.go +++ b/doc.go @@ -79,7 +79,7 @@ The following options can be used to customize the behavior of lf: wrapscan boolean (default on) scrolloff integer (default 0) tabstop integer (default 8) - filesep string (default ":") + filesep string (default "\n") ifs string (default "") (not exported if empty) previewer string (default "") (not filtered if empty) shell string (default "/bin/sh") @@ -91,7 +91,7 @@ The following options can be used to customize the behavior of lf: The following variables are exported for shell commands: $f current file - $fs marked file(s) separated with ':' + $fs marked file(s) separated with 'filesep' $fx current file or marked file(s) if any $id id number of the client @@ -221,10 +221,10 @@ A first attempt to write such a command may look like this: cmd trash ${{ mkdir -p ~/.trash - if [ -z $fs ]; then - mv --backup=numbered "$f" $HOME/.trash + if [ -z "$fs" ]; then + mv --backup=numbered "$f" ~/.trash else - IFS=':'; mv --backup=numbered $fs $HOME/.trash + IFS="`printf '\n\t'`"; mv --backup=numbered $fs ~/.trash fi }} @@ -234,7 +234,7 @@ variable is provided. We can use this variable to get rid of the conditional: cmd trash ${{ mkdir -p ~/.trash - IFS=':'; mv --backup=numbered $fx $HOME/.trash + IFS="`printf '\n\t'`"; mv --backup=numbered $fx ~/.trash }} The trash directory is checked each time the command is executed. We can move @@ -242,19 +242,20 @@ it outside of the command so it would only run once at startup: ${{ mkdir -p ~/.trash }} - cmd trash ${{ IFS=':'; mv --backup=numbered $fx $HOME/.trash }} + cmd trash ${{ IFS="`printf '\n\t'`"; mv --backup=numbered $fx ~/.trash }} Since these are one liners, we can drop "{{" and "}}": $mkdir -p ~/.trash - cmd trash $IFS=':'; mv --backup=numbered $fx $HOME/.trash + cmd trash $IFS="`printf '\n\t'`"; mv --backup=numbered $fx ~/.trash -Finally note that we set "IFS" variable accordingly in the command. Instead we -could use the "ifs" option to set it for all commands (e.g. "set ifs ':'"). -This can be especially useful for interactive use (e.g. "rm $fs" would simply -work). This option is not set by default as things may behave unexpectedly at -other places. +Finally note that we set "IFS" variable manually in these commands. Instead we +could use the "ifs" option to set it for all shell commands (i.e. 'set ifs +"\n"'). This can be especially useful for interactive use (e.g. "$rm $f" or +"$rm $fs" would simply work). This option is not set by default as it can +behave unexpectedly for new users. However, use of this option is highly +recommended and it is assumed in the rest of the documentation. Remote Commands @@ -363,22 +364,18 @@ called by "open" when the current file is not a directory. Normally a user maps the "open" command to a key (default "l") and customize "open-file" command as desired. You can define it just as you would define any other command: - cmd open-file $IFS=':'; vim $fx + cmd open-file $vim $fx It is possible to use different command types: - cmd open-file &xdg-open "$f" + cmd open-file &xdg-open $f You may want to use either file extensions or mime types from "file" command: cmd open-file ${{ - case $(file --mime-type "$f" -b) in - text/*) - IFS=':'; vim $fx;; - *) - IFS=':'; for f in $fx; do - xdg-open "$f" > /dev/null 2> /dev/null & - done;; + case $(file --mime-type $f -b) in + text/*) vim $fx;; + *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} @@ -403,7 +400,7 @@ this file. Output of the execution is printed in the preview pane. You may want to use the same script in your pager mapping as well if any: set previewer ~/.config/lf/pv.sh - map i $~/.config/lf/pv.sh "$f" | less -R + map i $~/.config/lf/pv.sh $f | less -R Since this script is called for each file selection change it needs to be as efficient as possible and this responsibility is left to the user. You may use diff --git a/docstring.go b/docstring.go index c777f42..6426f72 100644 --- a/docstring.go +++ b/docstring.go @@ -83,7 +83,7 @@ The following options can be used to customize the behavior of lf: wrapscan boolean (default on) scrolloff integer (default 0) tabstop integer (default 8) - filesep string (default ":") + filesep string (default "\n") ifs string (default "") (not exported if empty) previewer string (default "") (not filtered if empty) shell string (default "/bin/sh") @@ -95,7 +95,7 @@ The following options can be used to customize the behavior of lf: The following variables are exported for shell commands: $f current file - $fs marked file(s) separated with ':' + $fs marked file(s) separated with 'filesep' $fx current file or marked file(s) if any $id id number of the client @@ -231,10 +231,10 @@ A first attempt to write such a command may look like this: cmd trash ${{ mkdir -p ~/.trash - if [ -z $fs ]; then - mv --backup=numbered "$f" $HOME/.trash + if [ -z "$fs" ]; then + mv --backup=numbered "$f" ~/.trash else - IFS=':'; mv --backup=numbered $fs $HOME/.trash + IFS="'printf '\n\t''"; mv --backup=numbered $fs ~/.trash fi }} @@ -245,7 +245,7 @@ conditional: cmd trash ${{ mkdir -p ~/.trash - IFS=':'; mv --backup=numbered $fx $HOME/.trash + IFS="'printf '\n\t''"; mv --backup=numbered $fx ~/.trash }} The trash directory is checked each time the command is executed. We can @@ -253,19 +253,20 @@ move it outside of the command so it would only run once at startup: ${{ mkdir -p ~/.trash }} - cmd trash ${{ IFS=':'; mv --backup=numbered $fx $HOME/.trash }} + cmd trash ${{ IFS="'printf '\n\t''"; mv --backup=numbered $fx ~/.trash }} Since these are one liners, we can drop "{{" and "}}": $mkdir -p ~/.trash - cmd trash $IFS=':'; mv --backup=numbered $fx $HOME/.trash + cmd trash $IFS="'printf '\n\t''"; mv --backup=numbered $fx ~/.trash -Finally note that we set "IFS" variable accordingly in the command. Instead -we could use the "ifs" option to set it for all commands (e.g. "set ifs -':'"). This can be especially useful for interactive use (e.g. "rm $fs" -would simply work). This option is not set by default as things may behave -unexpectedly at other places. +Finally note that we set "IFS" variable manually in these commands. Instead +we could use the "ifs" option to set it for all shell commands (i.e. 'set +ifs "\n"'). This can be especially useful for interactive use (e.g. "$rm $f" +or "$rm $fs" would simply work). This option is not set by default as it can +behave unexpectedly for new users. However, use of this option is highly +recommended and it is assumed in the rest of the documentation. Remote Commands @@ -379,23 +380,19 @@ maps the "open" command to a key (default "l") and customize "open-file" command as desired. You can define it just as you would define any other command: - cmd open-file $IFS=':'; vim $fx + cmd open-file $vim $fx It is possible to use different command types: - cmd open-file &xdg-open "$f" + cmd open-file &xdg-open $f You may want to use either file extensions or mime types from "file" command: cmd open-file ${{ - case $(file --mime-type "$f" -b) in - text/*) - IFS=':'; vim $fx;; - *) - IFS=':'; for f in $fx; do - xdg-open "$f" > /dev/null 2> /dev/null & - done;; + case $(file --mime-type $f -b) in + text/*) vim $fx;; + *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} @@ -422,7 +419,7 @@ pane. You may want to use the same script in your pager mapping as well if any: set previewer ~/.config/lf/pv.sh - map i $~/.config/lf/pv.sh "$f" | less -R + map i $~/.config/lf/pv.sh $f | less -R Since this script is called for each file selection change it needs to be as efficient as possible and this responsibility is left to the user. You may diff --git a/etc/lfrc.example b/etc/lfrc.example index 4be209e..4d33024 100644 --- a/etc/lfrc.example +++ b/etc/lfrc.example @@ -8,10 +8,12 @@ # interpreter for shell commands (needs to be POSIX compatible) #set shell /bin/sh -# set internal field seperator (IFS) to ':' -# This is especially useful for interactive use to automatically split file -# names in $fs and $fx. Things may behave unexpectedly so use with caution. -#set ifs ':' +# set internal field separator (IFS) to "\n" for shell commands +# This is useful to automatically split file names in $fs and $fx properly +# since default file separator used in these variables (i.e. `filesep` option) +# is newline. You need to consider the values of these options and create your +# commands accordingly. +set ifs "\n" # leave some space at the top and the bottom of the screen set scrolloff 10 @@ -39,20 +41,20 @@ map gr cd / map read-shell # mappings for pager and editor (change as you like) -map i $less "$f" -map e $vim "$f" +map i $less $f +map e $vim $f # mapping to spawn a shell in working directory # (see also etc/lf.sh as an alternative workflow) map w $$SHELL # execute current file (must be executable) -map x $"$f" -map X !"$f" +map x $$f +map X !$f # dedicated keys for file opener actions -map o &mimeopen "$f" -map O $mimeopen --ask "$f" +map o &mimeopen $f +map O $mimeopen --ask $f # show documentation (overrides search-back) map ? $lf -doc | less @@ -62,18 +64,18 @@ map ? $lf -doc | less # want to use either file extensions and/or mime types here. Below uses an # editor for text files and a file opener for the rest. cmd open-file ${{ - case $(file --mime-type "$f" -b) in - text/*) IFS=':'; vim $fx;; - *) IFS=':'; for f in $fx; do xdg-open "$f" > /dev/null 2> /dev/null & done;; + case $(file --mime-type $f -b) in + text/*) vim $fx;; + *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} # rename current file without overwrite cmd rename ${{ - if [ -e "$1" ]; then + if [ -e $1 ]; then lf -remote "send $id echo file exists" else - mv "$f" "$1" + mv $f $1 fi }} map r push :rename @@ -86,26 +88,26 @@ $mkdir -p $HOME/.trash # move current file or selected files to trash folder # (see 'man mv' or 'mv --help' for backup options) -cmd trash $IFS=':'; mv --backup=numbered $fx $HOME/.trash +cmd trash $mv --backup=numbered $fx $HOME/.trash # remove current file or selected files (prompting) #cmd remove ${{ -# echo "$fx" | tr ':' '\n' -# echo -n 'remove?[y/n]' +# printf "$fx\n" +# printf "remove?[y/n]" # read ans -# [ $ans = 'y' ] && (IFS=':'; rm -rf $fx) +# [ $ans = "y" ] && rm -rf $fx #}} # extract the current file with the right command # (xkcd link: https://xkcd.com/1168/) cmd extract ${{ - case "$f" in - *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf "$f";; - *.tar.gz|*.tgz) tar xzvf "$f";; - *.tar.xz|*.txz) tar xJvf "$f";; - *.zip) unzip "$f";; - *.rar) unrar x "$f";; - *.7z) 7z x "$f";; + case $f in + *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;; + *.tar.gz|*.tgz) tar xzvf $f;; + *.tar.xz|*.txz) tar xJvf $f;; + *.zip) unzip $f;; + *.rar) unrar x $f;; + *.7z) 7z x $f;; esac }} @@ -113,10 +115,10 @@ cmd extract ${{ # This command takes the output name without '.tar.gz' suffix as an argument # (e.g. "compress foo" creates "foo.tar.gz"). cmd compress ${{ - mkdir "$1" - IFS=':'; cp $fs "$1" - tar czvf "$1.tar.gz" "$1" - rm -rf "$1" + mkdir $1 + cp $fs $1 + tar czvf $1.tar.gz $1 + rm -rf $1 }} # dynamically set the number of columns on startup based on terminal width diff --git a/eval_test.go b/eval_test.go index e97bc1f..1a15a7b 100644 --- a/eval_test.go +++ b/eval_test.go @@ -264,9 +264,9 @@ var gEvalTests = []struct { }, { - "cmd usage $du -h \"$1\" | less", - []string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"}, - []expr{&cmdExpr{"usage", &execExpr{"$", `du -h "$1" | less`}}}, + "cmd usage $du -h $1 | less", + []string{"cmd", "usage", "$", "du -h $1 | less", "\n"}, + []expr{&cmdExpr{"usage", &execExpr{"$", "du -h $1 | less"}}}, }, { @@ -320,42 +320,42 @@ var gEvalTests = []struct { { `map c ${{ mkdir foo - IFS=':'; cp ${fs} foo - tar -czvf "foo.tar.gz" foo + cp $fs foo + tar -czvf foo.tar.gz foo rm -rf foo }}`, []string{"map", "c", "$", "{{", ` mkdir foo - IFS=':'; cp ${fs} foo - tar -czvf "foo.tar.gz" foo + cp $fs foo + tar -czvf foo.tar.gz foo rm -rf foo `, "}}", "\n"}, []expr{&mapExpr{"c", &execExpr{"$", ` mkdir foo - IFS=':'; cp ${fs} foo - tar -czvf "foo.tar.gz" foo + cp $fs foo + tar -czvf foo.tar.gz foo rm -rf foo `}}}, }, { `cmd compress ${{ - mkdir "$1" - IFS=':'; cp ${fs} "$1" - tar -czvf "$1.tar.gz" "$1" - rm -rf "$1" + mkdir $1 + cp $fs $1 + tar -czvf $1.tar.gz $1 + rm -rf $1 }}`, []string{"cmd", "compress", "$", "{{", ` - mkdir "$1" - IFS=':'; cp ${fs} "$1" - tar -czvf "$1.tar.gz" "$1" - rm -rf "$1" + mkdir $1 + cp $fs $1 + tar -czvf $1.tar.gz $1 + rm -rf $1 `, "}}", "\n"}, []expr{&cmdExpr{"compress", &execExpr{"$", ` - mkdir "$1" - IFS=':'; cp ${fs} "$1" - tar -czvf "$1.tar.gz" "$1" - rm -rf "$1" + mkdir $1 + cp $fs $1 + tar -czvf $1.tar.gz $1 + rm -rf $1 `}}}, }, } diff --git a/opts.go b/opts.go index a52d69c..6005043 100644 --- a/opts.go +++ b/opts.go @@ -39,7 +39,7 @@ func init() { gOpts.wrapscan = true gOpts.scrolloff = 0 gOpts.tabstop = 8 - gOpts.filesep = ":" + gOpts.filesep = "\n" gOpts.shell = gDefaultShell gOpts.sortby = "natural" gOpts.timefmt = time.ANSIC