[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Piping man into grep - grotty sgr0 message
From: |
G. Branden Robinson |
Subject: |
Re: Piping man into grep - grotty sgr0 message |
Date: |
Wed, 19 Jul 2023 23:01:56 -0500 |
Forwarding a response from Michael with his permission, which also
contains some further tips and tricks for man page colorization.
At 2023-07-19T13:29:46-0400, Michael J. Donovan wrote:
> Branden,
> Thanks again for your help.
>
> I accidentally replied directly to you instead of to the mailing list.
> Didn't mean to take this conversation private.
> You can quote anything I write.
>
> I use "bat" as MANPAGER to get color man pages.
>
> Here's .bashrc and .zshrc
>
> export BAT_THEME=gruvbox-dark
> # set bat to display man pages. Use default theme (looks better for man
> pages).
> # do not clear screen on exit (leave the man page on screen).
> export MANPAGER="sh -c 'col -bx | bat --theme default -f -l man -p
> --pager \"less -RXs\"'"
> export MANROFFOPT="-c -Ww"
>
> The bat github page suggests MANROFFOPT="-c" so that's why it's there.
>
> And here's a script to fuzzy find man pages, with color preview:
>
> #!/bin/bash
> # fuzzy search/view man pages.
> # Works best with terminal 132x43 or more. 80x24 not so good.
>
> header="Shift-↓ Shift-↑ Scroll Down, Up one line\n"
> header+="Ctrl-j Ctrl-k Scroll Down, Up one page\n"
> header+="Enter Launch selected man page full screen\n"
> header+="Esc Quit"
>
> choice=$(apropos -s 1,5,7,8 . | fzf --no-info --reverse --header-first \
> --preview-window=up,70% --header="$(printf %b "$header")" \
> --bind='ctrl-k:preview-page-up,ctrl-j:preview-page-down' \
> --preview='man {1}{2} | bat -p -l man --color=always
> --theme=default'
> )
> [[ $choice ]] || exit
> echo "$choice" | awk '{print $1 $2}' | xargs man
>
> So, with your suggestions, the script is working "super good enough".
> And I don't have to sprinkle a bunch of 2>/dev/null in there.
>
> -Mike
>
> On 7/19/23 02:02 AM, G. Branden Robinson wrote:
> > Hi Michael,
> >
> > At 2023-07-19T00:26:34-0400, Michael J. Donovan wrote:
> > > Thanks for the explanation.
> > >
> > > So I updated my system once again and got a new groff 1.23.0-2
> > >
> > > No more grotty message!
> > Glad to hear it!
> >
> > > Some (very few) man pages do this:
> > > man sshfs | head
> > > troff:<standard input>:40: warning: cannot select font 'C'
> > > <snip>
> > >
> > > Probably an improperly written man page.
> > I have that man page installed too. When I look at it, I see at its
> > top:
> >
> > .\" Man page generated from reStructuredText.
> >
> > So, yeah, that can be a problem. People who write format converters to
> > man(7) often aren't aware of the portability constraints that apply.
> > Ingo Schwarze (mandoc maintainer) and I have tried to address this, both
> > in his mandoc documentation and in the "Portability" section of the
> > groff_man_style(7) page in groff 1.23.0.
> >
> > I won't get down in the weeds about the things wrong with that man page,
> > but long story short, a _portable_ man page can't count on any typefaces
> > but "R", "I", and "B" being available. (Nowadays, you can probably get
> > away with "BI" as well, but it is little-used anyway.)
> >
> > It may be that whoever wrote the converter had a poor grasp of the
> > distinction between font _families_ and font _styles_ (this is not a
> > groff-specific distinction; font vendors make it, too). It might also
> > be that they didn't think carefully about the fact that man pages are
> > mostly viewed on terminals, which can't change the font family. (With
> > the name "C", they were probably going for a monospaced family like
> > Courier.)
> >
> > > The strange thing is these warnings only occur when man is in a
> > > pipeline.
> > I'm not sure about why that might be.
> >
> > If I format the page "directly" (but discarding its output with the '-z'
> > option), I get the same warnings, as I would expect.
> >
> > $ man -w sshfs
> > /usr/share/man/man1/sshfs.1.gz
> > $ zcat $(!!) | nroff -mandoc -z
> > zcat $(man -w sshfs) | nroff -mandoc -z
> > troff:<standard input>:40: warning: cannot select font 'C'
> > troff:<standard input>:55: warning: cannot select font 'C'
> >
> > man-db's own man(1) page documents a --warning option that appears to
> > correspond directly to groff/troff/nroff's "-w" option, but as far as I
> > can tell says nothing about certain defaults being set one way for
> > pipelines and another way otherwise. man-db maintainer Colin Watson is
> > the real authority here.
> >
> > > Is there some kind of --color=always option like ls and grep have?
> > > Something to make man/groff ignore that it's in a pipeline?
> > I have a workaround for you that seems to work for me.
> >
> > $ man sshfs
> > troff:<standard input>:40: warning: cannot select font 'C'
> > troff:<standard input>:55: warning: cannot select font 'C'
> > $ MANROFFOPT="-Ww" man sshfs
> > $
> >
> > Here I have used man-db man(1)'s $MANROFFOPT variable to pass an option
> > to groff(1) that suppresses all warnings.
> >
> > Let me know if this helps. If so, it might be helpful to others as
> > well, so please let me know if I have your permission to quote your
> > message above back to the groff mailing list.
> >
> > Regards,
> > Branden
signature.asc
Description: PGP signature