Skip to content

Commit

Permalink
New Feature: Extra arguments
Browse files Browse the repository at this point in the history
Fixes #51. Teach Pistol to accept extra arguments to be passed to the
commands defined in the config. If the matched line in the config
doesn't use `%pistol-extra[0-9]%` the argument is discarded.

Update documentation and add tests.
  • Loading branch information
lucas-mior authored and doronbehar committed Mar 13, 2021
1 parent 71dfcdd commit 1a7ec6d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ test: pistol
@echo exit code \(issue '#'52\):
@echo -------------------
@./tests/exit-code.sh
@tput sgr0
@echo -------------------
@echo ./tests/VERSION.bz2 should appear along with license of bz2
@echo -------------------
@./pistol --config tests/config tests/VERSION.bz2 -v -L
@tput sgr0
@echo -------------------
@echo ./tests/renovate.json5.bz2 should appear without a license of bz2
@echo or verbosity, although the arguments are passed to pistol
@echo -------------------
@./pistol --config tests/config tests/renovate.json5.bz2 -v -L

deps:
go get github.com/c4milo/github-release
Expand Down
43 changes: 42 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ https://github.com/doronbehar/pistol/issues/6[#6] for more details.]

....
$ pistol --help
Usage: pistol OPTIONS [<file> ...]
Usage: pistol OPTIONS <file> [<extras> ...]
OPTIONS
Expand All @@ -146,6 +146,7 @@ OPTIONS
ARGUMENTS
file the file to preview
extras extra arguments passed to the command
....

=== Integrations
Expand Down Expand Up @@ -329,6 +330,46 @@ More over, with `sh:` you can use shell pipes:
fpath .*.md$ sh: bat --paging=never --color=always %pistol-filename% | head -8
----

==== Passing arbitrary extra arguments to commands

Pistol is capable of passing extra arguments to commands if the config says so.
The arguments `%pistol-extra0%`, `%pistol-extra1%` and so on, are substituted
by the extra arguments given to `pistol`, if these present in invokation and if
they are present in the config. Example usage:

With this config:

....
fpath /problematic-bz2/.*.bz2 bzip2 %pistol-filename% %pistol-extra0%
fpath /working-bz2/.*.bz2 bzip2 %pistol-filename%
....

Running:

....
pistol /problematic-bz2/example.bz2 --test
....

Will run bzip2 while testing the integrity of the compressed file. However,
running:

....
pistol /working-bz2/example.bz2 --test
....

Will not pass the `--test` argument to bzip, due to `%pistol-extra0` not
present in the config for the files at `/working-bz2`. This feature is mainly
present for usage with https://github.com/gokcehan/lf[Lf] and
https://ranger.github.io/[Ranger] which can pass width height and x, y
coordinates for image previews.

Here's an example usage for image previews that works with Lf:
footnote:[`pv` script refers to https://github.com/neeshy/lfimg/blob/e9154721514a1384a89f2713092c15dc77992f37/pv[this script].]

....
image/.* pv %pistol-filename% %pistol-extra0% %pistol-extra1% %pistol-extra2% %pistol-extra3%
....

=== Environmental Variables

Pistol’s internal previewer for text files includes syntax highlighting
Expand Down
9 changes: 6 additions & 3 deletions cmd/pistol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func main() {
cmd := cmdline.New()
cmd.AddOption("c", "config", "config file", fmt.Sprintf("configuration file to use (defaults to %s/pistol/pistol.conf)", xdg.ConfigHome))
cmd.AddFlag("V", "version", "Print version date and exit")
cmd.AddTrailingArguments("file", "the file to preview")
cmd.AddArgument("file", "the file to preview")
cmd.AddTrailingArguments("extras", "extra arguments passed to the command")
cmd.Parse(os.Args)

if cmd.IsOptionSet("version") {
Expand All @@ -42,13 +43,15 @@ func main() {
}
}
configPath := cmd.OptionValue("config")
var extras []string
extras = cmd.TrailingArgumentsValues("extras")

// handle file argument with configuration
if len(cmd.TrailingArgumentsValues("file")) == 0 {
if len(cmd.ArgumentValue("file")) == 0 {
log.Fatalf("no arguments!")
os.Exit(1)
}
previewer, err := pistol.NewPreviewer(cmd.TrailingArgumentsValues("file")[0], configPath)
previewer, err := pistol.NewPreviewer(cmd.ArgumentValue("file"), configPath, extras)
if err != nil {
log.Fatal(err)
os.Exit(2)
Expand Down
23 changes: 21 additions & 2 deletions previewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os/exec"
// "fmt"
"strconv"
"strings"
"regexp"
"path/filepath"
Expand All @@ -21,6 +22,8 @@ import (
type Previewer struct {
// The file to be previewed
FilePath string
// Extra arguments passed to pistol
Extras []string
// The mime type detected
MimeType string
// The command that will be used to print the file. If empty, internal
Expand Down Expand Up @@ -50,7 +53,7 @@ type Previewer struct {
//
// Many mime types are handled internally by Pistol, see table here:
// https://github.com/doronbehar/pistol#introduction
func NewPreviewer(filePath, configPath string) (Previewer, error) {
func NewPreviewer(filePath, configPath string, extras []string) (Previewer, error) {
verbose := os.Getenv("PISTOL_DEBUG")
if verbose != "" {
log.SetLevel(log.InfoLevel)
Expand All @@ -72,6 +75,7 @@ func NewPreviewer(filePath, configPath string) (Previewer, error) {
p.MimeType = mimetype
log.Infof("detected mimetype is %s", p.MimeType)
p.FilePath = filePath
p.Extras = extras
// If configuration file doesn't exist, we don't try to read it
if configPath == "" {
log.Warnf("configuration file was not supplied")
Expand Down Expand Up @@ -155,9 +159,24 @@ func (p *Previewer) Write(w io.Writer) (error) {
}
var cmd *exec.Cmd
var argsOut []string

re := regexp.MustCompile(`%pistol-extra([0-9]+)%`)

for _, arg := range p.Args {
argsOut = append(argsOut, strings.ReplaceAll(arg, "%pistol-filename%", replStr))
if(re.MatchString(arg)) {
auxStr := re.ReplaceAllString(arg, "$1")
auxInt, err := strconv.Atoi(auxStr);
if (err == nil && len(p.Extras) > auxInt) {
arg = re.ReplaceAllString(arg, p.Extras[auxInt])
} else {
continue
}
} else {
arg = strings.ReplaceAll(arg, "%pistol-filename%", replStr)
}
argsOut = append(argsOut, arg)
}

if p.Command == "sh:" {
log.Infof("previewer's command is (shell interpreted): %#v\n", argsOut)
cmd = exec.Command("sh", "-c", strings.Join(argsOut, " "))
Expand Down
Binary file added tests/VERSION.bz2
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ fpath .*/tests/fpath-with-sh$ sh: bat --map-syntax :Markdown --paging=never --st
text/html elinks -dump -dump-color-mode 1 %pistol-filename%
# detects: mimetype, sh: yes
text/plain sh: bat --map-syntax :Markdown --paging=never --style=numbers --color=always %pistol-filename% | head -1
# Test extra arguments
fpath .*/tests/VERSION.bz2$ bzcat %pistol-extra0% %pistol-extra1% %pistol-filename%
# Test extra arguments
fpath .*/tests/renovate.json5.bz2$ bzcat %pistol-filename%
# Test exit code is non-zero in case a command does not exist:
application/x-bzip2 not-a-real-command %pistol-filename%
# Test exit code is non-zero in case a command starts but fails to finish
Expand Down
6 changes: 3 additions & 3 deletions tests/exit-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ if ./pistol --config tests/config tests/34.json.bz2; then
exit 1
else
tput setaf 2
echo "exit code was not zero"
echo "exit code was not zero when testing a non real command"
fi

if ./pistol --config tests/config tests; then
tput setaf 1
echo "exit code was not non-zero when testing a real command with invalid arguments for it"
echo "exit code was not non-zero when testing a real command with invalid arguments"
exit 1
else
tput setaf 2
echo "exit code was not zero"
echo "exit code was not zero when testing a real command with invalid arguments"
fi
tput sgr0
exit 0
Binary file added tests/renovate.json5.bz2
Binary file not shown.

0 comments on commit 1a7ec6d

Please sign in to comment.