Skip to content

The moving parts

By the end of this chapter you have a map of the five parts every Glow run passes through, so that when a later chapter opens one of them, you know where it sits.

1. The source picker

Glow accepts at most one argument, and what the argument is decides where the document comes from:

  • a file path reads that file — chapter 4;
  • a directory path opens the file browser there — chapter 11;
  • a URL, or a shorthand like github.com/owner/repo, fetches a document from the web — chapter 10;
  • - reads standard input, and standard input has a surprise of its own: if it is not a terminal, it overrules a file argument entirely — chapter 6;
  • no argument at all, on a terminal, opens the file browser in the current directory — chapter 11.

Two arguments are an error, worded exactly "accepts at most 1 arg(s), received 2" (transcript glow-lab/transcripts/t09-sources.txt).

2. The parser

The document is read as Markdown and parsed into a structure — headings, paragraphs, lists, tables, code — by the parser Glow is built on (the project uses the goldmark parser; you do not need to know it to use Glow, but its limits become Glow's limits, and chapter 14 meets one: footnotes are not in the structure it builds, so they never reach the page).

Whether a file is treated as Markdown at all is decided mostly by its name. The lab fed Glow the same content under eleven different names: .md, .MD, .markdown, .mdown, .mkd and no extension at all were rendered; .txt, .rst, .py, .html, .text, .adoc, .mdwn and .md.txt were printed plain, source marks and all (transcript glow-lab/transcripts/t09-sources.txt). Only the final extension counts.

3. The renderer and its styles

The parsed structure is drawn by Glow's rendering library, glamour, following a style: a small definition of colours and habits — what a heading looks like, how a table is ruled, which token stands for a ticked box. Glow ships several styles (auto, dark, light, notty, pink, ascii, dracula, tokyo-night), and a style can also be a JSON file of your own — chapter 8 opens all of this. The renderer also decides the small mercies and the small losses: an image becomes a line of text naming the file, a link shows its address beside its text.

4. The width step

The drawn page is wrapped to a width — 80 columns unless flags or the configuration file say otherwise, and not your terminal's width, whatever that is. The lab proved the default on terminals 30, 50 and 120 columns wide: the longest rendered line was 78 every time (transcript glow-lab/transcripts/t05-width.txt). Chapter 9 covers changing it.

5. The exits

The finished page leaves by one of three doors:

  • printed straight to the terminal, or onwards into a pipe or a file — where, if no style was forced, it quietly becomes the plain notty reading (chapter 6);
  • handed to an external pager — the program named by the PAGER environment variable, less by default — when -p is passed (chapter 7);
  • shown in Glow's built-in pager, full-screen, when -t is passed, which is also where the file browser opens documents (chapter 11).

Standing behind all five parts are the settings: a handful of flags, and a small configuration file, glow.yml, that Glow writes for you on first run. Chapter 12 maps which setting wins where — including one place where the configuration file's style is simply ignored.

One more part is easy to miss because it is a conversation, not a component: before drawing on a terminal, Glow asks the terminal questions — escape sequences that mean "what is your foreground colour? what is your background colour?" — to choose between its dark and light readings. Chapter 8 shows the questions in the raw bytes, and the ten-second wait when no answer comes.

Check: name the five parts in order, and say which part decides whether a .txt file is rendered or printed plain. If you can, the map is yours.