Skip to content

Pipes and stdin

By the end of this chapter you can predict, before you run it, what any Glow command will produce when its output is piped — and you know the one case where the document Glow reads is not the one you named.

What a pipe gets: the plain reading

Glow's auto style is two styles in one. On a terminal it means "choose dark or light" (chapter 8). When the output is not a terminal — a pipe, a file, a captured string in a script — it means the notty style: no colours, drawn tables in ASCII, the source's own [x] in task lists. The lab proved the equivalence byte for byte on two documents:

$ glow packing-list.md | wc -c        → 798
$ glow -s notty packing-list.md | wc -c → 798, identical bytes

notty output is still a rendering: the trail notes came out at 3,899 bytes from a 1,390-byte source, because drawn tables, margins and spacing are real characters. It is not the source file, and piping Glow is never a substitute for cat when the bytes are what you want. It is also not quite plain: as chapter 5 showed, the OSC 8 hyperlink codes survive even here.

Forcing colour into a pipe

You can overrule the fallback:

glow -s dark trail-notes.md > page.txt

Be sure you want to. The lab measured the coloured rendering at 36,138 bytes — twenty-six times the source — because the style restates its colours around nearly every word and space (transcript glow-lab/transcripts/t01-rendering.txt). That file will display as garbage in most editors and as a rainbow in a few. The honest uses are narrow: feeding a program that itself understands terminal codes, or examining the codes, as this guide's lab did. If a person is to read the result, let them run Glow themselves.

The same warning applies to NO_COLOR, the convention by which a user tells every program "no colour, please". Glow ignores it. With NO_COLOR=1 set, a forced -s dark produced the identical 9,212 bytes on the lab's packing list, and auto on a terminal still rendered dark with 594 colour changes (transcript glow-lab/transcripts/t90-terminal.txt). If you need plain output, ask for it outright: -s notty.

stdin: the pipe that overrules

Glow reads documents from pipes as happily as it writes to them:

cat notes.md | glow -
glow - < notes.md
some-command | glow

The documented form is the dash. But here is the behaviour that earns this chapter its warning, proved by the lab (transcript glow-lab/transcripts/t02-stdin-and-pipes.txt):

When standard input is not a terminal, Glow reads standard input and ignores the file you named.

$ echo '# From The Pipe' | glow packing-list.md

prints the pipe's one line, rendered — not the packing list. No warning, no error. Three consequences:

  • In scripts, a Glow command can silently read whatever its stdin happens to be. If you mean the file, say so with the shell: redirect stdin from nowhere — glow file.md < /dev/null.
  • With no argument and stdin redirected from a file, Glow renders that stream; it does not open the file browser. The browser needs a terminal for stdin too.
  • The lab's own first scripted run hung for exactly this reason: its shell offered an open pipe as stdin, and Glow settled down to read a document from it. The fix was the same < /dev/null.

Check: run echo '# Surprise' | glow <a-file> and watch the pipe win; then run glow -s dark <a-file> | wc -c against glow <a-file> | wc -c and see the colour tax in numbers. If both match what this chapter promised, you have it.