// lesson: status-line
The Status Bar and Messages
Every serious full-screen tool reserves a strip of screen for
telling you where you are: vim's status line, nano's shortcut bars,
less's prompt. It's the difference between editing a file and
editing blind. Ours takes the bottom row of the terminal: the
text viewport gets terminal_rows - 1 rows, the status bar gets
the last one. (That subtraction is why the editor struct's
screen_rows is the viewport height, not the terminal height โ
decide once, at init, who owns which rows.)
Inverted video
The bar must read as chrome, not content. The classic trick costs five bytes: reverse video โ SGR 7 swaps foreground and background, so the bar renders as a solid contrasting stripe:
\x1b[7m ...status text padded to full width... \x1b[m
Two details make it look right rather than almost-right:
- Pad to exactly the screen width. Reverse video colors only the
cells you actually print. Print a 30-character status on an
80-column terminal and you get a 30-character stripe and 50 cells
of abandoned background. The bar text must be built to precisely
screen_colscharacters โ which is the actual programming problem here. - Reset afterwards (
\x1b[m), or the next thing drawn inherits the inversion. You knew that; it's still the #1 status-bar bug.
Left, right, and the squeeze
The conventional layout puts identity left and position right:
report.txt [+] Ln 128, Col 43
โโ filename, dirty marker 1-based, always โโ
Building it is a fixed-width layout problem in miniature:
- Compose the left segment: filename (or
[No Name]for a fresh buffer), plus a[+]dirty marker when unsaved changes exist. (1-based Ln/Col on the right, by the way โ users count from 1, programs from 0; the status bar is user territory.) - Compose the right segment.
- If both fit in
width, print left,width - left - rightspaces, right. - If they don't fit, something must yield: truncate the left
segment (long filenames lose their tails; a position display
truncated to
Ln 12is misinformation), keeping one space between the segments. In the pathological case where even the right segment alone doesn't fit, truncate it towidthโ never write more thanwidth.
This "compose segments, then resolve the squeeze" shape recurs in every TUI you'll ever write (tab bars, prompts, tmux's status) โ worth doing carefully once.
โบ Render the Status Bar
20 ptsImplement format_status_bar(e, out, width): fill out with
exactly width characters (plus a terminating NUL) per the
lesson's layout rules. No escape sequences here โ the renderer adds
the \x1b[7m wrapper; this function is pure fixed-width text
layout, and the tests measure it to the character.
Log in to submit a solution and earn points.