Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0175c1bd1b | ||
|
|
01581f902f | ||
|
|
54315ffde2 | ||
|
|
bdf20aa451 | ||
|
|
847c46421c | ||
|
|
f6c99a7d46 | ||
|
|
e55c490bf5 | ||
|
|
c63c8f63ac | ||
|
|
68232c06e3 | ||
|
|
4f05975e77 | ||
|
|
32d729eac1 |
No files matched your search
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -17,6 +20,9 @@ type app struct {
|
||||
ui *ui
|
||||
nav *nav
|
||||
quitChan chan bool
|
||||
cmd *exec.Cmd
|
||||
cmdIn io.WriteCloser
|
||||
cmdOutBuf []byte
|
||||
cmdHist []cmdItem
|
||||
cmdHistInd int
|
||||
}
|
||||
@@ -168,18 +174,19 @@ func waitKey() error {
|
||||
|
||||
// This function is used to run a command in shell. Following modes are used:
|
||||
//
|
||||
// Prefix Wait Async Stdin/Stdout/Stderr UI action
|
||||
// $ No No Yes Pause and then resume
|
||||
// ! Yes No Yes Pause and then resume
|
||||
// & No Yes No Do nothing
|
||||
//
|
||||
// Waiting async commands are not used for now.
|
||||
func (app *app) runShell(s string, args []string, wait bool, async bool) {
|
||||
// Prefix Wait Async Stdin Stdout Stderr UI action
|
||||
// $ No No Yes Yes Yes Pause and then resume
|
||||
// % No No Yes Yes Yes Use statline for input/output
|
||||
// ! Yes No Yes Yes Yes Pause and then resume
|
||||
// & No Yes No No No Do nothing
|
||||
func (app *app) runShell(s string, args []string, prefix string) {
|
||||
app.exportVars()
|
||||
|
||||
cmd := shellCommand(s, args)
|
||||
|
||||
if !async {
|
||||
var out io.Reader
|
||||
switch prefix {
|
||||
case "$", "!":
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
@@ -187,25 +194,69 @@ func (app *app) runShell(s string, args []string, wait bool, async bool) {
|
||||
app.ui.pause()
|
||||
defer app.ui.resume()
|
||||
defer app.nav.renew(app.ui.wins[0].h)
|
||||
case "%":
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
log.Printf("writing stdin: %s", err)
|
||||
}
|
||||
app.cmdIn = stdin
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Printf("reading stdout: %s", err)
|
||||
}
|
||||
out = stdout
|
||||
cmd.Stderr = cmd.Stdout
|
||||
}
|
||||
|
||||
var err error
|
||||
if async {
|
||||
err = cmd.Start()
|
||||
} else {
|
||||
switch prefix {
|
||||
case "$", "!":
|
||||
err = cmd.Run()
|
||||
case "%", "&":
|
||||
err = cmd.Start()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
app.ui.printf("running shell: %s", err)
|
||||
}
|
||||
|
||||
if wait {
|
||||
switch prefix {
|
||||
case "!":
|
||||
if err := waitKey(); err != nil {
|
||||
app.ui.printf("waiting shell: %s", err)
|
||||
app.ui.printf("waiting key: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
app.ui.loadFile(app.nav)
|
||||
app.ui.loadFileInfo(app.nav)
|
||||
|
||||
switch prefix {
|
||||
case "%":
|
||||
go func() {
|
||||
app.cmd = cmd
|
||||
app.cmdOutBuf = nil
|
||||
app.ui.msg = ""
|
||||
app.ui.cmdPrefix = ">"
|
||||
|
||||
reader := bufio.NewReader(out)
|
||||
for {
|
||||
b, err := reader.ReadByte()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
app.cmdOutBuf = append(app.cmdOutBuf, b)
|
||||
app.ui.exprChan <- multiExpr{&callExpr{"echo", []string{string(app.cmdOutBuf)}}, 1}
|
||||
if b == '\n' || b == '\r' {
|
||||
app.cmdOutBuf = nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.Printf("running shell: %s", err)
|
||||
}
|
||||
app.cmd = nil
|
||||
app.ui.cmdPrefix = ""
|
||||
app.ui.exprChan <- multiExpr{&callExpr{"reload", nil}, 1}
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -35,9 +35,10 @@ var (
|
||||
"redraw",
|
||||
"reload",
|
||||
"read",
|
||||
"read-shell",
|
||||
"read-shell-wait",
|
||||
"read-shell-async",
|
||||
"shell",
|
||||
"shell-pipe",
|
||||
"shell-wait",
|
||||
"shell-async",
|
||||
"search",
|
||||
"search-back",
|
||||
"search-next",
|
||||
@@ -45,6 +46,7 @@ var (
|
||||
"sync",
|
||||
"echo",
|
||||
"cd",
|
||||
"select",
|
||||
"push",
|
||||
}
|
||||
|
||||
|
||||
@@ -12,61 +12,64 @@ Reference
|
||||
|
||||
The following commands are provided by lf with default keybindings:
|
||||
|
||||
up (default 'k' and '<up>')
|
||||
half-up (default '<c-u>')
|
||||
page-up (default '<c-b>')
|
||||
down (default 'j' and '<down>')
|
||||
half-down (default '<c-d>')
|
||||
page-down (default '<c-f>')
|
||||
updir (default 'h' and '<left>')
|
||||
open (default 'l' and '<right>')
|
||||
quit (default 'q')
|
||||
top (default 'gg')
|
||||
bot (default 'G')
|
||||
toggle (default '<space>')
|
||||
invert (default 'v')
|
||||
unmark (default 'u')
|
||||
yank (default 'y')
|
||||
delete (default 'd')
|
||||
put (default 'p')
|
||||
clear (default 'c')
|
||||
redraw (default '<c-l>')
|
||||
reload (default '<c-r>')
|
||||
read (default ':')
|
||||
read-shell (default '$')
|
||||
read-shell-wait (default '!')
|
||||
read-shell-async (default '&')
|
||||
search (default '/')
|
||||
search-back (default '?')
|
||||
search-next (default 'n')
|
||||
search-prev (default 'N')
|
||||
up (default 'k' and '<up>')
|
||||
half-up (default '<c-u>')
|
||||
page-up (default '<c-b>')
|
||||
down (default 'j' and '<down>')
|
||||
half-down (default '<c-d>')
|
||||
page-down (default '<c-f>')
|
||||
updir (default 'h' and '<left>')
|
||||
open (default 'l' and '<right>')
|
||||
quit (default 'q')
|
||||
top (default 'gg')
|
||||
bot (default 'G')
|
||||
toggle (default '<space>')
|
||||
invert (default 'v')
|
||||
unmark (default 'u')
|
||||
yank (default 'y')
|
||||
delete (default 'd')
|
||||
put (default 'p')
|
||||
clear (default 'c')
|
||||
redraw (default '<c-l>')
|
||||
reload (default '<c-r>')
|
||||
read (default ':')
|
||||
shell (default '$')
|
||||
shell-pipe (default '%')
|
||||
shell-wait (default '!')
|
||||
shell-async (default '&')
|
||||
search (default '/')
|
||||
search-back (default '?')
|
||||
search-next (default 'n')
|
||||
search-prev (default 'N')
|
||||
|
||||
The following commands are provided by lf without default keybindings:
|
||||
|
||||
sync synchronizes yanked/deleted files with server
|
||||
echo prints its arguments to the message line
|
||||
cd changes working directory to its argument
|
||||
select changes current file selection to its argument
|
||||
push simulate key pushes given in its argument
|
||||
|
||||
The following command line commands are provided by lf with default
|
||||
keybindings:
|
||||
|
||||
cmd-escape (default '<esc>')
|
||||
cmd-comp (default '<tab>')
|
||||
cmd-enter (default '<c-j>' and '<enter>')
|
||||
cmd-hist-next (default '<c-n>')
|
||||
cmd-hist-prev (default '<c-p>')
|
||||
cmd-delete (default '<c-d>' and '<delete>')
|
||||
cmd-delete-back (default '<bs>' and '<bs2>')
|
||||
cmd-left (default '<c-b>' and '<left>')
|
||||
cmd-right (default '<c-f>' and '<right>')
|
||||
cmd-beg (default '<c-a>' and '<home>')
|
||||
cmd-end (default '<c-e>' and '<end>')
|
||||
cmd-delete-beg (default '<c-u>')
|
||||
cmd-delete-end (default '<c-k>')
|
||||
cmd-delete-word (default '<c-w>')
|
||||
cmd-put (default '<c-y>')
|
||||
cmd-transpose (default '<c-t>')
|
||||
cmd-escape (default '<esc>')
|
||||
cmd-comp (default '<tab>')
|
||||
cmd-enter (default '<c-j>' and '<enter>')
|
||||
cmd-hist-next (default '<c-n>')
|
||||
cmd-hist-prev (default '<c-p>')
|
||||
cmd-delete (default '<c-d>' and '<delete>')
|
||||
cmd-delete-back (default '<bs>' and '<bs2>')
|
||||
cmd-left (default '<c-b>' and '<left>')
|
||||
cmd-right (default '<c-f>' and '<right>')
|
||||
cmd-beg (default '<c-a>' and '<home>')
|
||||
cmd-end (default '<c-e>' and '<end>')
|
||||
cmd-delete-beg (default '<c-u>')
|
||||
cmd-delete-end (default '<c-k>')
|
||||
cmd-delete-word (default '<c-w>')
|
||||
cmd-put (default '<c-y>')
|
||||
cmd-transpose (default '<c-t>')
|
||||
cmd-interrupt (default '<c-c>')
|
||||
|
||||
The following options can be used to customize the behavior of lf:
|
||||
|
||||
@@ -141,12 +144,13 @@ Prefixes
|
||||
|
||||
The following command prefixes are used by lf:
|
||||
|
||||
: read (default) builtin/custom command
|
||||
$ read-shell shell command
|
||||
! read-shell-wait shell command waiting for key press
|
||||
& read-shell-async asynchronous shell command
|
||||
/ search search file in current directory
|
||||
? search-back search file in the reverse order
|
||||
: read (default) builtin/custom command
|
||||
$ shell shell command
|
||||
% shell-pipe shell command running with the ui
|
||||
! shell-wait shell command waiting for key press
|
||||
& shell-async shell command running asynchronously
|
||||
/ search search file in current directory
|
||||
? search-back search file in the reverse order
|
||||
|
||||
The same evaluator is used for the command line and the configuration file. The
|
||||
difference is that prefixes are not necessary in the command line. Instead
|
||||
@@ -215,7 +219,7 @@ prefix.
|
||||
set info time
|
||||
}}
|
||||
|
||||
Mappings
|
||||
Push Mappings
|
||||
|
||||
The usual way to map a key sequence is to assign it to a named or unnamed
|
||||
command. While this provides a clean way to remap builtin keys as well as other
|
||||
@@ -240,12 +244,11 @@ commands it is possible to accidentally create recursive bindings:
|
||||
|
||||
These types of bindings create a deadlock when executed.
|
||||
|
||||
Commands
|
||||
Shell Commands
|
||||
|
||||
For demonstration let us write a shell command to move selected file(s) to
|
||||
trash.
|
||||
|
||||
A first attempt to write such a command may look like this:
|
||||
Regular shell commands are the most basic command type that is useful for many
|
||||
purposes. For example, we can write a shell command to move selected file(s) to
|
||||
trash. A first attempt to write such a command may look like this:
|
||||
|
||||
cmd trash ${{
|
||||
mkdir -p ~/.trash
|
||||
@@ -285,6 +288,47 @@ could use the 'ifs' option to set it for all shell commands (i.e. 'set ifs
|
||||
behave unexpectedly for new users. However, use of this option is highly
|
||||
recommended and it is assumed in the rest of the documentation.
|
||||
|
||||
Piping Shell Commands
|
||||
|
||||
Regular shell commands have some limitations in some cases. When an output or
|
||||
error message is given and the command exits afterwards, the ui is immediately
|
||||
resumed and there is no way to see the message without dropping to shell again.
|
||||
Also, even when there is no output or error, the ui still needs to be paused
|
||||
while the command is running. This can cause flickering on the screen for short
|
||||
commands and similar distractions for longer commands.
|
||||
|
||||
Instead of pausing the ui, piping shell commands connects stdin, stdout, and
|
||||
stderr of the command to the statline in the bottom of the ui. This can be
|
||||
useful for programs following the unix philosophy to give no output in the
|
||||
success case, and brief error messages or prompts in other cases.
|
||||
|
||||
For example, following rename command prompts for overwrite in the statline if
|
||||
there is an existing file with the given name:
|
||||
|
||||
cmd rename %mv -i $f $1
|
||||
|
||||
You can also output error messages in the command and it will show up in the
|
||||
statline. For example, an alternative rename command may look like this:
|
||||
|
||||
cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
|
||||
|
||||
One thing to be careful is that although input is still line buffered, output
|
||||
and error are byte buffered and verbose commands will be very slow to display.
|
||||
|
||||
Waiting Shell Commands
|
||||
|
||||
Waiting shell commands are similar to regular shell commands except that they
|
||||
wait for a key press when the command is finished. These can be useful to see
|
||||
the output of a program before the ui is resumed. Waiting shell commands are
|
||||
more appropriate than piping shell commands when the command is verbose and the
|
||||
output is best displayed as multiline.
|
||||
|
||||
Asynchronous Shell Commands
|
||||
|
||||
Asynchronous shell commands are used to start a command in the background and
|
||||
then resume operation without waiting for the command to finish. Stdin, stdout,
|
||||
and stderr of the command is neither connected to the terminal nor to the ui.
|
||||
|
||||
Remote Commands
|
||||
|
||||
One of the more advanced features in lf is remote commands. All clients connect
|
||||
@@ -321,15 +365,17 @@ a shell command:
|
||||
lf -remote "send $id echo hello world"
|
||||
|
||||
Since lf does not have control flow syntax, remote commands are used for such
|
||||
needs. A common use is to display an error message back in the client. You can
|
||||
implement a safe rename command which does not overwrite an existing file or
|
||||
directory as such:
|
||||
needs. For example, you can configure the number of columns in the ui with
|
||||
respect to the terminal width as follows:
|
||||
|
||||
cmd rename ${{
|
||||
if [ -e $1 ]; then
|
||||
lf -remote "send $id echo file exists"
|
||||
cmd recol ${{
|
||||
w=$(tput cols)
|
||||
if [ $w -le 80 ]; then
|
||||
lf -remote "send $id set ratios 1:2"
|
||||
elif [ $w -le 160 ]; then
|
||||
lf -remote "send $id set ratios 1:2:3"
|
||||
else
|
||||
mv $f $1
|
||||
lf -remote "send $id set ratios 1:2:3:5"
|
||||
fi
|
||||
}}
|
||||
|
||||
|
||||
+116
-63
@@ -16,61 +16,64 @@ Reference
|
||||
|
||||
The following commands are provided by lf with default keybindings:
|
||||
|
||||
up (default 'k' and '<up>')
|
||||
half-up (default '<c-u>')
|
||||
page-up (default '<c-b>')
|
||||
down (default 'j' and '<down>')
|
||||
half-down (default '<c-d>')
|
||||
page-down (default '<c-f>')
|
||||
updir (default 'h' and '<left>')
|
||||
open (default 'l' and '<right>')
|
||||
quit (default 'q')
|
||||
top (default 'gg')
|
||||
bot (default 'G')
|
||||
toggle (default '<space>')
|
||||
invert (default 'v')
|
||||
unmark (default 'u')
|
||||
yank (default 'y')
|
||||
delete (default 'd')
|
||||
put (default 'p')
|
||||
clear (default 'c')
|
||||
redraw (default '<c-l>')
|
||||
reload (default '<c-r>')
|
||||
read (default ':')
|
||||
read-shell (default '$')
|
||||
read-shell-wait (default '!')
|
||||
read-shell-async (default '&')
|
||||
search (default '/')
|
||||
search-back (default '?')
|
||||
search-next (default 'n')
|
||||
search-prev (default 'N')
|
||||
up (default 'k' and '<up>')
|
||||
half-up (default '<c-u>')
|
||||
page-up (default '<c-b>')
|
||||
down (default 'j' and '<down>')
|
||||
half-down (default '<c-d>')
|
||||
page-down (default '<c-f>')
|
||||
updir (default 'h' and '<left>')
|
||||
open (default 'l' and '<right>')
|
||||
quit (default 'q')
|
||||
top (default 'gg')
|
||||
bot (default 'G')
|
||||
toggle (default '<space>')
|
||||
invert (default 'v')
|
||||
unmark (default 'u')
|
||||
yank (default 'y')
|
||||
delete (default 'd')
|
||||
put (default 'p')
|
||||
clear (default 'c')
|
||||
redraw (default '<c-l>')
|
||||
reload (default '<c-r>')
|
||||
read (default ':')
|
||||
shell (default '$')
|
||||
shell-pipe (default '%')
|
||||
shell-wait (default '!')
|
||||
shell-async (default '&')
|
||||
search (default '/')
|
||||
search-back (default '?')
|
||||
search-next (default 'n')
|
||||
search-prev (default 'N')
|
||||
|
||||
The following commands are provided by lf without default keybindings:
|
||||
|
||||
sync synchronizes yanked/deleted files with server
|
||||
echo prints its arguments to the message line
|
||||
cd changes working directory to its argument
|
||||
select changes current file selection to its argument
|
||||
push simulate key pushes given in its argument
|
||||
|
||||
The following command line commands are provided by lf with default
|
||||
keybindings:
|
||||
|
||||
cmd-escape (default '<esc>')
|
||||
cmd-comp (default '<tab>')
|
||||
cmd-enter (default '<c-j>' and '<enter>')
|
||||
cmd-hist-next (default '<c-n>')
|
||||
cmd-hist-prev (default '<c-p>')
|
||||
cmd-delete (default '<c-d>' and '<delete>')
|
||||
cmd-delete-back (default '<bs>' and '<bs2>')
|
||||
cmd-left (default '<c-b>' and '<left>')
|
||||
cmd-right (default '<c-f>' and '<right>')
|
||||
cmd-beg (default '<c-a>' and '<home>')
|
||||
cmd-end (default '<c-e>' and '<end>')
|
||||
cmd-delete-beg (default '<c-u>')
|
||||
cmd-delete-end (default '<c-k>')
|
||||
cmd-delete-word (default '<c-w>')
|
||||
cmd-put (default '<c-y>')
|
||||
cmd-transpose (default '<c-t>')
|
||||
cmd-escape (default '<esc>')
|
||||
cmd-comp (default '<tab>')
|
||||
cmd-enter (default '<c-j>' and '<enter>')
|
||||
cmd-hist-next (default '<c-n>')
|
||||
cmd-hist-prev (default '<c-p>')
|
||||
cmd-delete (default '<c-d>' and '<delete>')
|
||||
cmd-delete-back (default '<bs>' and '<bs2>')
|
||||
cmd-left (default '<c-b>' and '<left>')
|
||||
cmd-right (default '<c-f>' and '<right>')
|
||||
cmd-beg (default '<c-a>' and '<home>')
|
||||
cmd-end (default '<c-e>' and '<end>')
|
||||
cmd-delete-beg (default '<c-u>')
|
||||
cmd-delete-end (default '<c-k>')
|
||||
cmd-delete-word (default '<c-w>')
|
||||
cmd-put (default '<c-y>')
|
||||
cmd-transpose (default '<c-t>')
|
||||
cmd-interrupt (default '<c-c>')
|
||||
|
||||
The following options can be used to customize the behavior of lf:
|
||||
|
||||
@@ -148,12 +151,13 @@ Prefixes
|
||||
|
||||
The following command prefixes are used by lf:
|
||||
|
||||
: read (default) builtin/custom command
|
||||
$ read-shell shell command
|
||||
! read-shell-wait shell command waiting for key press
|
||||
& read-shell-async asynchronous shell command
|
||||
/ search search file in current directory
|
||||
? search-back search file in the reverse order
|
||||
: read (default) builtin/custom command
|
||||
$ shell shell command
|
||||
% shell-pipe shell command running with the ui
|
||||
! shell-wait shell command waiting for key press
|
||||
& shell-async shell command running asynchronously
|
||||
/ search search file in current directory
|
||||
? search-back search file in the reverse order
|
||||
|
||||
The same evaluator is used for the command line and the configuration file.
|
||||
The difference is that prefixes are not necessary in the command line.
|
||||
@@ -224,7 +228,7 @@ proper prefix.
|
||||
}}
|
||||
|
||||
|
||||
Mappings
|
||||
Push Mappings
|
||||
|
||||
The usual way to map a key sequence is to assign it to a named or unnamed
|
||||
command. While this provides a clean way to remap builtin keys as well as
|
||||
@@ -251,12 +255,12 @@ of commands it is possible to accidentally create recursive bindings:
|
||||
These types of bindings create a deadlock when executed.
|
||||
|
||||
|
||||
Commands
|
||||
Shell Commands
|
||||
|
||||
For demonstration let us write a shell command to move selected file(s) to
|
||||
trash.
|
||||
|
||||
A first attempt to write such a command may look like this:
|
||||
Regular shell commands are the most basic command type that is useful for
|
||||
many purposes. For example, we can write a shell command to move selected
|
||||
file(s) to trash. A first attempt to write such a command may look like
|
||||
this:
|
||||
|
||||
cmd trash ${{
|
||||
mkdir -p ~/.trash
|
||||
@@ -298,6 +302,53 @@ behave unexpectedly for new users. However, use of this option is highly
|
||||
recommended and it is assumed in the rest of the documentation.
|
||||
|
||||
|
||||
Piping Shell Commands
|
||||
|
||||
Regular shell commands have some limitations in some cases. When an output
|
||||
or error message is given and the command exits afterwards, the ui is
|
||||
immediately resumed and there is no way to see the message without dropping
|
||||
to shell again. Also, even when there is no output or error, the ui still
|
||||
needs to be paused while the command is running. This can cause flickering
|
||||
on the screen for short commands and similar distractions for longer
|
||||
commands.
|
||||
|
||||
Instead of pausing the ui, piping shell commands connects stdin, stdout, and
|
||||
stderr of the command to the statline in the bottom of the ui. This can be
|
||||
useful for programs following the unix philosophy to give no output in the
|
||||
success case, and brief error messages or prompts in other cases.
|
||||
|
||||
For example, following rename command prompts for overwrite in the statline
|
||||
if there is an existing file with the given name:
|
||||
|
||||
cmd rename %mv -i $f $1
|
||||
|
||||
You can also output error messages in the command and it will show up in the
|
||||
statline. For example, an alternative rename command may look like this:
|
||||
|
||||
cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
|
||||
|
||||
One thing to be careful is that although input is still line buffered,
|
||||
output and error are byte buffered and verbose commands will be very slow to
|
||||
display.
|
||||
|
||||
|
||||
Waiting Shell Commands
|
||||
|
||||
Waiting shell commands are similar to regular shell commands except that
|
||||
they wait for a key press when the command is finished. These can be useful
|
||||
to see the output of a program before the ui is resumed. Waiting shell
|
||||
commands are more appropriate than piping shell commands when the command is
|
||||
verbose and the output is best displayed as multiline.
|
||||
|
||||
|
||||
Asynchronous Shell Commands
|
||||
|
||||
Asynchronous shell commands are used to start a command in the background
|
||||
and then resume operation without waiting for the command to finish. Stdin,
|
||||
stdout, and stderr of the command is neither connected to the terminal nor
|
||||
to the ui.
|
||||
|
||||
|
||||
Remote Commands
|
||||
|
||||
One of the more advanced features in lf is remote commands. All clients
|
||||
@@ -335,15 +386,17 @@ calling the following in a shell command:
|
||||
lf -remote "send $id echo hello world"
|
||||
|
||||
Since lf does not have control flow syntax, remote commands are used for
|
||||
such needs. A common use is to display an error message back in the client.
|
||||
You can implement a safe rename command which does not overwrite an existing
|
||||
file or directory as such:
|
||||
such needs. For example, you can configure the number of columns in the ui
|
||||
with respect to the terminal width as follows:
|
||||
|
||||
cmd rename ${{
|
||||
if [ -e $1 ]; then
|
||||
lf -remote "send $id echo file exists"
|
||||
cmd recol ${{
|
||||
w=$(tput cols)
|
||||
if [ $w -le 80 ]; then
|
||||
lf -remote "send $id set ratios 1:2"
|
||||
elif [ $w -le 160 ]; then
|
||||
lf -remote "send $id set ratios 1:2:3"
|
||||
else
|
||||
mv $f $1
|
||||
lf -remote "send $id set ratios 1:2:3:5"
|
||||
fi
|
||||
}}
|
||||
|
||||
|
||||
+9
-20
@@ -12,7 +12,7 @@ set ifs "\n"
|
||||
set scrolloff 10
|
||||
|
||||
# use enter for shell commands
|
||||
map <enter> read-shell
|
||||
map <enter> shell
|
||||
|
||||
# execute current file (must be executable)
|
||||
map x $$f
|
||||
@@ -37,24 +37,15 @@ cmd open-file ${{
|
||||
}}
|
||||
|
||||
# rename current file without overwrite
|
||||
cmd rename ${{
|
||||
if [ -e $1 ]; then
|
||||
lf -remote "send $id echo file exists"
|
||||
else
|
||||
mv $f $1
|
||||
fi
|
||||
}}
|
||||
cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
|
||||
map r push :rename<space>
|
||||
|
||||
# show disk usage
|
||||
cmd usage $du -h . | less
|
||||
|
||||
# make sure trash folder exists
|
||||
$mkdir -p ~/.trash
|
||||
%mkdir -p ~/.trash
|
||||
|
||||
# move current file or selected files to trash folder
|
||||
# (also see 'man mv' for backup/overwrite options)
|
||||
cmd trash $mv $fx ~/.trash
|
||||
cmd trash %mv $fx ~/.trash
|
||||
|
||||
# remove current file or selected files (prompting)
|
||||
cmd remove ${{
|
||||
@@ -66,7 +57,7 @@ cmd remove ${{
|
||||
|
||||
# extract the current file with the right command
|
||||
# (xkcd link: https://xkcd.com/1168/)
|
||||
cmd extract ${{
|
||||
cmd extract %{{
|
||||
case $f in
|
||||
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;;
|
||||
*.tar.gz|*.tgz) tar xzvf $f;;
|
||||
@@ -77,12 +68,10 @@ cmd extract ${{
|
||||
esac
|
||||
}}
|
||||
|
||||
# compress marked files with tar and gunzip
|
||||
# This command takes the output name without '.tar.gz' suffix as an argument
|
||||
# (e.g. 'compress foo' creates 'foo.tar.gz').
|
||||
cmd compress ${{
|
||||
# compress current file or selected files with tar and gunzip
|
||||
cmd compress %{{
|
||||
mkdir $1
|
||||
cp $fs $1
|
||||
tar czvf $1.tar.gz $1
|
||||
cp -r $fx $1
|
||||
tar czf $1.tar.gz $1
|
||||
rm -rf $1
|
||||
}}
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -344,11 +345,13 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
app.nav.reload()
|
||||
case "read":
|
||||
app.ui.cmdPrefix = ":"
|
||||
case "read-shell":
|
||||
case "shell":
|
||||
app.ui.cmdPrefix = "$"
|
||||
case "read-shell-wait":
|
||||
case "shell-pipe":
|
||||
app.ui.cmdPrefix = "%"
|
||||
case "shell-wait":
|
||||
app.ui.cmdPrefix = "!"
|
||||
case "read-shell-async":
|
||||
case "shell-async":
|
||||
app.ui.cmdPrefix = "&"
|
||||
case "search":
|
||||
app.ui.cmdPrefix = "/"
|
||||
@@ -385,6 +388,17 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
}
|
||||
app.ui.loadFile(app.nav)
|
||||
app.ui.loadFileInfo(app.nav)
|
||||
case "select":
|
||||
if len(e.args) != 1 {
|
||||
app.ui.print("select: requires an argument")
|
||||
return
|
||||
}
|
||||
if err := app.nav.find(e.args[0]); err != nil {
|
||||
app.ui.printf("%s", err)
|
||||
return
|
||||
}
|
||||
app.ui.loadFile(app.nav)
|
||||
app.ui.loadFileInfo(app.nav)
|
||||
case "push":
|
||||
if len(e.args) > 0 {
|
||||
log.Println("pushing keys", e.args[0])
|
||||
@@ -397,6 +411,9 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(e.args[0])...)
|
||||
}
|
||||
case "cmd-escape":
|
||||
if app.ui.cmdPrefix == ">" {
|
||||
return
|
||||
}
|
||||
app.ui.menuBuf = nil
|
||||
app.ui.cmdAccLeft = nil
|
||||
app.ui.cmdAccRight = nil
|
||||
@@ -434,13 +451,22 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
}
|
||||
case "$":
|
||||
log.Printf("shell: %s", s)
|
||||
app.runShell(s, nil, false, false)
|
||||
app.runShell(s, nil, app.ui.cmdPrefix)
|
||||
case "%":
|
||||
log.Printf("shell-pipe: %s", s)
|
||||
app.runShell(s, nil, app.ui.cmdPrefix)
|
||||
app.cmdHist = append(app.cmdHist, cmdItem{"%", s})
|
||||
return
|
||||
case ">":
|
||||
io.WriteString(app.cmdIn, s+"\n")
|
||||
app.cmdOutBuf = nil
|
||||
return
|
||||
case "!":
|
||||
log.Printf("shell-wait: %s", s)
|
||||
app.runShell(s, nil, true, false)
|
||||
app.runShell(s, nil, app.ui.cmdPrefix)
|
||||
case "&":
|
||||
log.Printf("shell-async: %s", s)
|
||||
app.runShell(s, nil, false, true)
|
||||
app.runShell(s, nil, app.ui.cmdPrefix)
|
||||
case "/":
|
||||
log.Printf("search: %s", s)
|
||||
app.nav.search = s
|
||||
@@ -465,6 +491,9 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
app.cmdHist = append(app.cmdHist, cmdItem{app.ui.cmdPrefix, s})
|
||||
app.ui.cmdPrefix = ""
|
||||
case "cmd-hist-next":
|
||||
if app.ui.cmdPrefix == "" {
|
||||
return
|
||||
}
|
||||
if app.cmdHistInd > 0 {
|
||||
app.cmdHistInd--
|
||||
}
|
||||
@@ -481,6 +510,9 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
app.ui.cmdAccRight = nil
|
||||
app.ui.menuBuf = nil
|
||||
case "cmd-hist-prev":
|
||||
if app.ui.cmdPrefix == "" {
|
||||
app.cmdHistInd = 0
|
||||
}
|
||||
if app.cmdHistInd == len(app.cmdHist) {
|
||||
return
|
||||
}
|
||||
@@ -534,6 +566,14 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
if len(app.ui.cmdAccLeft) > 1 {
|
||||
app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2] = app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1]
|
||||
}
|
||||
case "cmd-interrupt":
|
||||
if app.cmd != nil {
|
||||
app.cmd.Process.Kill()
|
||||
}
|
||||
app.ui.menuBuf = nil
|
||||
app.ui.cmdAccLeft = nil
|
||||
app.ui.cmdAccRight = nil
|
||||
app.ui.cmdPrefix = ""
|
||||
default:
|
||||
cmd, ok := gOpts.cmds[e.name]
|
||||
if !ok {
|
||||
@@ -548,19 +588,16 @@ func (e *execExpr) eval(app *app, args []string) {
|
||||
switch e.prefix {
|
||||
case "$":
|
||||
log.Printf("shell: %s -- %s", e, args)
|
||||
app.runShell(e.value, args, false, false)
|
||||
app.runShell(e.value, args, e.prefix)
|
||||
case "%":
|
||||
log.Printf("shell-pipe: %s -- %s", e, args)
|
||||
app.runShell(e.value, args, e.prefix)
|
||||
case "!":
|
||||
log.Printf("shell-wait: %s -- %s", e, args)
|
||||
app.runShell(e.value, args, true, false)
|
||||
app.runShell(e.value, args, e.prefix)
|
||||
case "&":
|
||||
log.Printf("shell-async: %s -- %s", e, args)
|
||||
app.runShell(e.value, args, false, true)
|
||||
case "/":
|
||||
log.Printf("search: %s -- %s", e, args)
|
||||
// TODO: implement
|
||||
case "?":
|
||||
log.Printf("search-back: %s -- %s", e, args)
|
||||
// TODO: implement
|
||||
app.runShell(e.value, args, e.prefix)
|
||||
default:
|
||||
log.Printf("evaluating unknown execution prefix: %q", e.prefix)
|
||||
}
|
||||
|
||||
@@ -260,6 +260,7 @@ func (nav *nav) getDirs(wd string) {
|
||||
|
||||
for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) {
|
||||
dir := nav.loadDir(curr)
|
||||
dir.find(base, nav.height)
|
||||
dirs = append(dirs, dir)
|
||||
}
|
||||
|
||||
@@ -618,6 +619,30 @@ func (nav *nav) cd(wd string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nav *nav) find(path string) error {
|
||||
lstat, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("select: %s", err)
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
|
||||
if err := nav.cd(dir); err != nil {
|
||||
return fmt.Errorf("select: %s", err)
|
||||
}
|
||||
|
||||
base := filepath.Base(path)
|
||||
|
||||
last := nav.dirs[len(nav.dirs)-1]
|
||||
if last.loading {
|
||||
last.fi = append(last.fi, &file{FileInfo: lstat})
|
||||
} else {
|
||||
last.find(base, nav.height)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func match(pattern, name string) (matched bool, err error) {
|
||||
if gOpts.ignorecase {
|
||||
lpattern := strings.ToLower(pattern)
|
||||
|
||||
@@ -75,9 +75,10 @@ func init() {
|
||||
gOpts.keys["<c-l>"] = &callExpr{"redraw", nil}
|
||||
gOpts.keys["<c-r>"] = &callExpr{"reload", nil}
|
||||
gOpts.keys[":"] = &callExpr{"read", nil}
|
||||
gOpts.keys["$"] = &callExpr{"read-shell", nil}
|
||||
gOpts.keys["!"] = &callExpr{"read-shell-wait", nil}
|
||||
gOpts.keys["&"] = &callExpr{"read-shell-async", nil}
|
||||
gOpts.keys["$"] = &callExpr{"shell", nil}
|
||||
gOpts.keys["%"] = &callExpr{"shell-pipe", nil}
|
||||
gOpts.keys["!"] = &callExpr{"shell-wait", nil}
|
||||
gOpts.keys["&"] = &callExpr{"shell-async", nil}
|
||||
gOpts.keys["/"] = &callExpr{"search", nil}
|
||||
gOpts.keys["?"] = &callExpr{"search-back", nil}
|
||||
gOpts.keys["n"] = &callExpr{"search-next", nil}
|
||||
@@ -123,6 +124,7 @@ func init() {
|
||||
gOpts.cmdkeys["<c-w>"] = &callExpr{"cmd-delete-word", nil}
|
||||
gOpts.cmdkeys["<c-y>"] = &callExpr{"cmd-put", nil}
|
||||
gOpts.cmdkeys["<c-t>"] = &callExpr{"cmd-transpose", nil}
|
||||
gOpts.cmdkeys["<c-c>"] = &callExpr{"cmd-interrupt", nil}
|
||||
|
||||
gOpts.cmds = make(map[string]expr)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ const (
|
||||
// no explicit keyword type
|
||||
tokenIdent // e.g. set, ratios, 1:2:3
|
||||
tokenColon // :
|
||||
tokenPrefix // $, !, &, / or ?
|
||||
tokenPrefix // $, %, !, &
|
||||
tokenLBraces // {{
|
||||
tokenRBraces // }}
|
||||
tokenCommand // in between a prefix to \n or between {{ and }}
|
||||
@@ -90,8 +90,7 @@ func isDigit(b byte) bool {
|
||||
}
|
||||
|
||||
func isPrefix(b byte) bool {
|
||||
// TODO: how to differentiate slash in path vs search?
|
||||
return b == '$' || b == '!' || b == '&' // || b == '/' || b == '?'
|
||||
return b == '$' || b == '%' || b == '!' || b == '&'
|
||||
}
|
||||
|
||||
func (s *scanner) scan() bool {
|
||||
|
||||
@@ -415,6 +415,7 @@ type ui struct {
|
||||
msg string
|
||||
regPrev *reg
|
||||
dirPrev *dir
|
||||
exprChan chan multiExpr
|
||||
keyChan chan string
|
||||
evChan chan termbox.Event
|
||||
menuBuf *bytes.Buffer
|
||||
@@ -611,14 +612,21 @@ func (ui *ui) draw(nav *nav) {
|
||||
ui.wins[woff+i].printDir(nav.dirs[doff+i], nav.marks, nav.saves)
|
||||
}
|
||||
|
||||
if ui.cmdPrefix != "" {
|
||||
switch ui.cmdPrefix {
|
||||
case "":
|
||||
ui.drawStatLine(nav)
|
||||
termbox.HideCursor()
|
||||
case ">":
|
||||
ui.msgWin.printLine(0, 0, fg, bg, ui.cmdPrefix)
|
||||
ui.msgWin.print(len(ui.cmdPrefix), 0, fg, bg, ui.msg)
|
||||
ui.msgWin.print(len(ui.cmdPrefix)+len(ui.msg), 0, fg, bg, string(ui.cmdAccLeft))
|
||||
ui.msgWin.print(len(ui.cmdPrefix)+len(ui.msg)+runeSliceWidth(ui.cmdAccLeft), 0, fg, bg, string(ui.cmdAccRight))
|
||||
termbox.SetCursor(ui.msgWin.x+len(ui.cmdPrefix)+len(ui.msg)+runeSliceWidth(ui.cmdAccLeft), ui.msgWin.y)
|
||||
default:
|
||||
ui.msgWin.printLine(0, 0, fg, bg, ui.cmdPrefix)
|
||||
ui.msgWin.print(len(ui.cmdPrefix), 0, fg, bg, string(ui.cmdAccLeft))
|
||||
ui.msgWin.print(len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), 0, fg, bg, string(ui.cmdAccRight))
|
||||
termbox.SetCursor(ui.msgWin.x+len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), ui.msgWin.y)
|
||||
} else {
|
||||
ui.drawStatLine(nav)
|
||||
termbox.HideCursor()
|
||||
}
|
||||
|
||||
if gOpts.preview {
|
||||
@@ -830,6 +838,8 @@ func (ui *ui) readEvent(ch chan<- multiExpr, ev termbox.Event) {
|
||||
func (ui *ui) readExpr() <-chan multiExpr {
|
||||
ch := make(chan multiExpr)
|
||||
|
||||
ui.exprChan = ch
|
||||
|
||||
go func() {
|
||||
ch <- multiExpr{&callExpr{"redraw", nil}, 1}
|
||||
|
||||
|
||||
Reference in new issue
Block a user