Skip to content

Instantly share code, notes, and snippets.

@jomsh0
Last active July 1, 2022 06:05
Show Gist options
  • Save jomsh0/dca857ba937e48f095243a5d0ceb002c to your computer and use it in GitHub Desktop.
Save jomsh0/dca857ba937e48f095243a5d0ceb002c to your computer and use it in GitHub Desktop.
Command line interfaces to interesting UNIX-y things.

Command line interfaces to interesting UNIX-y things.

FIFOs:

mkfifo -m600 name.pipe

Unix sockets:

nc -lkU aSocket.sock

Getting a pty master from command line1

To run cmd connected to a pty which you can access the master side:

script -c cmd logfile | cat
unbuffer [-p] cmd | cat

Render manpage for screen:

Redirecting man to a non-tty causes it to dump perfectly formated plain text, sized to the current screen width. From inspecting

strace -f -o trace.out -e execve man page > page.txt
# (-f = follow child processes; necessary to see `execve` syscalls).

I could reproduce using the source doc and *roff pipelines.

export GROFF_SGR=1  # undoc. requirement for ANSI tty formatting in output.
                    # /usr/share/groff/site-tmac/man.local (Arch-specific?)

# Each of these is (seemingly) equivalent:
zcat page.1.gz | preconv [-e utf-8] | tbl | troff -man -Tutf8 | grotty
  ...          | preconv            | ... | nroff -man
  ...          | groff -kt -man -Tutf8
  • man is a "macro package" for authoring roff manpages.
  • troff is the primary device-independent formatter.
  • nroff is a wrapper for text/tty output (-Tutf8 specifies a "device")
  • grotty implements the tty output device. nroff always uses it.
  • preconv converts all input to plain ascii and \[uXXXX] sequences. The roff programs require this. Uses system locale by default.
  • tbl is a preprocessor for tables. eqn for math, ...
  • groff is the newer front end. -k -t takes care of preconv and tbl

For plain text file:

unset GROFF_SGR  # system-specific; see above.

zcat page.1.gz | preconv | ... | troff -man -Tutf8 | grotty -cbou
  ...          | groff -kt -man -Tutf8 | col -xb
  • Output mode should be non-SGR. i.e., using overstrike sequences instead of ANSI escape sequences for formatting.

    • grotty -c is one way; setting GROFF_NO_SGR is another.
    • Locally, it's actually the default when GROFF_SGR isn't specifically set. But that appears to be Arch-specific.
  • However, the overstrike sequences still need to be suppressed or removed.

    • -b -o -u options to grotty appear to do the trick.
    • col at the end of the pipeline can do it as well.

BONUS

The -rLL={XX}n -rLT={XX}n options to [tg]roff set the column width. man appears to query the tty for this info every time it renders a page.

Capture screen to plain text

tmux command:

capture-pane [-p] [-t pane] [-S n -E n]
save-buffer <filename>
  • p = send to stdout (not to be used from attached client)
  • S and E = start/end lines (default is visible screen)
    (0 = first line of visible screen; neg. numbers = history)

Getting w3m to render stdin

cmark-gfm notes.md | w3m -T text/html

Render Markdown on tty

glow file.md
lowdown -tterm file.md

lowdown has several output targets besides term:

lowdown -st{ms,man,latex,html} file.md

lowdown -stms  ... | groff -tk -mspdf -Tutf8
lowdown -stman ... | groff -tk -man -Tutf8
lowdown -stman ... | mandoc

The -s is critical for those pipelines; it generates a standalone document instead of a fragment.

Misc

TTY Demystified

Footnotes

  1. https://stackoverflow.com/questions/32910661/pretend-to-be-a-tty-in-bash-for-any-command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment