Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to discard output of interactive commands that exit with non-zero status #328

Closed
philsnow opened this issue Jul 2, 2020 · 1 comment

Comments

@philsnow
Copy link

philsnow commented Jul 2, 2020

Background

I just found skim and I love it -- I've long wanted a tool that lets me very quickly iterate on interactive command line filters, to eliminate steps in the "run command, look at output in pager, quit pager, move cursor to right place in the command, re-run" loop, and skim's interactive mode provides that.

I have a shell function that I use to run jq from sk:

function skjq {
  sk --tac --ansi --regex  --query . --multi --interactive --cmd '{}' \
     --bind 'enter:select-all+accept,ctrl-y:select-all+execute-silent(for line in {+}; do echo $line; done | pbcopy)+deselect-all' \
     --cmd-history=${HOME}/.sk_history --cmd-history-size=100000 \
     --cmd-query "cat $1 | jq --raw-output --color-output '.'"
}
  1. --tac is because sk seems to naturally reverse the order of its input (?)
  2. --ansi is to allow colors
  3. --regex --query . is because I don't want to remove any lines from the input -- I'm (ab)using the line selection buffer as a preview of the jq output
  4. --multi is so when I accept or whatever, it can print out the entire line selection buffer
  5. --interactive --cmd '{}' enters interactive mode and just runs whatever command is in the command query
  6. --bind ... sets up a bind for enter that selects all and accepts, similar for C-y
  7. --cmd-history* command query history (skim seems to not know how to expand ~ to my home directory, that's a separate issue)
  8. --cmd-query "..." this is the meat, where I specify the initial jq invocation. It's pretty much just jq ..

This is an amazing experience, but I find myself referring to the line selection buffer and typing the name of some JSON key, and it often disappears while I'm typing (because my in-progress jq query or command is nonsensical).

Suggestion

Could you add an option that disables refreshing of the line selection buffer when the new command query exits with non-zero status? jq has an --exit-status option that makes it exit with status 1 in some cases.

The wrapping shell that invokes the command query could be sh -O pipefail -c ... instead of sh -c ... to make this more robust to noticing failures. I don't know how cross-shell pipefail is, but you could try invoking it with -O pipefail and fall back to omitting it if that fails.

@lotabout
Copy link
Collaborator

@philsnow

--regex --query . is because I don't want to remove any lines from the input -- I'm (ab)using the line selection buffer as a preview of the jq output

This is unnecessary because empty query would preserve all lines by default.

New option --no-clear-if-empty is implemented to meet this need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants