Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: less with subprocess



On 2021-09-28 19:52:30 +0100, Dominik Vogt wrote:
> On Mon, Sep 27, 2021 at 05:53:33PM -0700, Bart Schaefer wrote:
> > On Mon, Sep 27, 2021 at 5:41 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > > alias -g LF='| () { cat >$1 &! less $1 ; kill $! } =(:)'
> >
> > On additional thought ... it's not really saving very much to make
> > this a global alias.  An actual function
> >
> > LF () { () { cat >$1 &! less $1 ; kill $! } =(:) }
> >
> > is only two more characters to pipe into, and can also be redirected into.
> 
> Both do not work for me (zsh-5.7.1, less 487).  The generating
> command is killed when ctrl-c is pressed for the first time:
> 
>   # using the alias
>   { sleep 10; i=1; while true; do echo $i; i=$[i+1]; sleep 1; done } LF

In case this can be useful, I've been using the following function
since 2019. Here, svn2log is a command that can take much time to
generate the output, so that I wanted to be about to do a Ctrl-C to
interrupt some "less" operation (e.g. after [End]) without killing
the command.

sll()
{
  # The issue with "svn2log ... | less" is that after quitting the pager,
  # svn2log may still take time after getting a broken pipe. One wants to
  # kill it immediately by using a subshell and "kill -PIPE 0" to kill the
  # process group; the PIPE signal is used instead of another one in order
  # to avoid failure/killed messages from svn and/or zsh.
  # The "trap '' INT" prevents Ctrl-C (useful in "less") from killing the
  # subshell and the other processes of the process group, and "less" is
  # run in a subshell so that the INT trap is reset. Note: avoiding the
  # subshell with just "trap - INT; less" does not work as zsh implements
  # WUE (but should work in shells that implement WCE).
  ( trap '' INT; svn2log --color ',bright_green,cyan,yellow' "$@" | \
      { (less); kill -PIPE 0 } )
  # And let's avoid SIGPIPE as the exit status code.
  local r=$?
  return $(( r == 128 + $(kill -l PIPE) ? 0 : r ))
}

Before that, I was using "less -f <(the_command ...)" as also
mentioned in this thread, but it had some issues.

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)




Messages sorted by: Reverse Date, Date, Thread, Author