From 1a7ec6d97cead332caf579414497c3c5026ba42b Mon Sep 17 00:00:00 2001 From: "lucas.mior" Date: Tue, 9 Mar 2021 17:54:30 -0300 Subject: [PATCH] New Feature: Extra arguments 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. --- Makefile | 11 ++++++++++ README.adoc | 43 ++++++++++++++++++++++++++++++++++++++- cmd/pistol/main.go | 9 +++++--- previewer.go | 23 +++++++++++++++++++-- tests/VERSION.bz2 | Bin 0 -> 44 bytes tests/config | 4 ++++ tests/exit-code.sh | 6 +++--- tests/renovate.json5.bz2 | Bin 0 -> 126 bytes 8 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 tests/VERSION.bz2 create mode 100644 tests/renovate.json5.bz2 diff --git a/Makefile b/Makefile index 5691f96..782fd3b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.adoc b/README.adoc index 1f1b24e..67e7874 100644 --- a/README.adoc +++ b/README.adoc @@ -135,7 +135,7 @@ https://github.com/doronbehar/pistol/issues/6[#6] for more details.] .... $ pistol --help -Usage: pistol OPTIONS [ ...] +Usage: pistol OPTIONS [ ...] OPTIONS @@ -146,6 +146,7 @@ OPTIONS ARGUMENTS file the file to preview +extras extra arguments passed to the command .... === Integrations @@ -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 diff --git a/cmd/pistol/main.go b/cmd/pistol/main.go index ccc742d..3afd3a1 100644 --- a/cmd/pistol/main.go +++ b/cmd/pistol/main.go @@ -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") { @@ -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) diff --git a/previewer.go b/previewer.go index 3f8f1fc..dc5bb1e 100644 --- a/previewer.go +++ b/previewer.go @@ -6,6 +6,7 @@ import ( "io" "os/exec" // "fmt" + "strconv" "strings" "regexp" "path/filepath" @@ -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 @@ -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) @@ -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") @@ -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, " ")) diff --git a/tests/VERSION.bz2 b/tests/VERSION.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..064c6c4bf13702b42adc9783c096a641d5c88e8c GIT binary patch literal 44 zcmZ>Y%CIzaj8qGbjERdCWME*t!N4HEz?h(*z@XUdIEz^#CibMrLnp_=$`Tg<@Tm*K literal 0 HcmV?d00001 diff --git a/tests/config b/tests/config index e08d4e3..877fffd 100644 --- a/tests/config +++ b/tests/config @@ -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 diff --git a/tests/exit-code.sh b/tests/exit-code.sh index 1de55d1..bd1274f 100755 --- a/tests/exit-code.sh +++ b/tests/exit-code.sh @@ -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 diff --git a/tests/renovate.json5.bz2 b/tests/renovate.json5.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..2c7c0c24b6b9ac1875163c055c12b9dc071cd15a GIT binary patch literal 126 zcmV-^0D=EPT4*^jL0KkKS%Q-38~^|?-+(|6Py~Pw2mpo(Kc~)WAOKWU(?itFPfbk# z8R|h2ew6h+Pg6!f8U~sNqIdNou4fgZ=a6k^WCjMxCQ=K;Op7h+loB4z!eLO`gh-*{ g6=@f>iJ&~fiAy7{uKE_Q52HU9az!{$kb;uw9BE-Ou>b%7 literal 0 HcmV?d00001