Skip to content

Tutorial 5 Put It Together

rizukirr edited this page Jun 5, 2026 · 2 revisions

Tutorial 5: Put It All Together

Let's combine everything — keyboard, mouse, layout, and colors — into one little app: a paint program with a color toolbar and a status bar. You pick a color with the number keys and paint with the mouse.

This tutorial also introduces a few more handy tools: lt_printf for live text, millions of colors, and using lt_detect_color_depth so your program looks its best everywhere.

The plan (our layout)

We'll split the screen into three parts:

row 0      Colors (press 1-7):          ← title
row 1      1 ▓▓  2 ▓▓  3 ▓▓ ...         ← color swatches (the toolbar)
rows 2..   (the canvas — paint here)
last row   left-drag = paint  c = clear  q = quit   pointer: 12,5   ← status bar

The program

Paste this into examples/playground.c:

#include "libterm/libterm.h"

/* Our seven paint colors. */
static const lt_attr palette[] = {LT_RED,     LT_GREEN, LT_BLUE,  LT_YELLOW,
                                  LT_MAGENTA, LT_CYAN,  LT_WHITE};
#define NCOLORS 7

static int cur = 1; /* index of the chosen color (start on green) */

/* Draw the title + color swatches across the top two rows. */
static void draw_toolbar(void) {
  for (int x = 0; x < lt_width(); x++) { /* wipe the two toolbar rows */
    lt_set_cell(x, 0, ' ', LT_DEFAULT, LT_DEFAULT);
    lt_set_cell(x, 1, ' ', LT_DEFAULT, LT_DEFAULT);
  }
  lt_print(1, 0, LT_WHITE | LT_BOLD, LT_DEFAULT, "Colors (press 1-7):");

  for (int i = 0; i < NCOLORS; i++) {
    int x = 1 + i * 5;
    lt_attr num = (i == cur) ? (LT_WHITE | LT_BOLD) : LT_WHITE;
    lt_printf(x, 1, num, LT_DEFAULT, "%d", i + 1);  /* the number */
    lt_set_cell(x + 1, 1, ' ', LT_DEFAULT, palette[i]); /* a 2-cell swatch */
    lt_set_cell(x + 2, 1, ' ', LT_DEFAULT, palette[i]);
    if (i == cur)
      lt_print(x + 3, 1, LT_WHITE | LT_BOLD, LT_DEFAULT, "<");
  }
}

/* Draw the status bar along the bottom row. */
static void draw_status(int px, int py) {
  int row = lt_height() - 1;
  for (int x = 0; x < lt_width(); x++)
    lt_set_cell(x, row, ' ', LT_WHITE, LT_BLUE); /* a blue bar */
  lt_printf(1, row, LT_WHITE, LT_BLUE,
            "left-drag = paint   c = clear   q = quit   pointer: %d,%d", px, py);
}

int main(void) {
  if (lt_init() != LT_OK)
    return 1;

  lt_set_output_mode(lt_detect_color_depth()); /* best colors this terminal has */
  lt_set_input_mode(LT_INPUT_MOUSE);

  int px = 0, py = 0; /* last pointer position (for the status bar) */

  lt_clear();
  draw_toolbar();
  draw_status(px, py);
  lt_present();

  struct lt_event ev;
  while (lt_poll_event(&ev) == LT_OK) {
    if (ev.type == LT_EVENT_KEY) {
      if (ev.ch == 'q' || ev.key == LT_KEY_ESC)
        break;
      if (ev.ch >= '1' && ev.ch <= '0' + NCOLORS) { /* pick a color */
        cur = ev.ch - '1';
        draw_toolbar();
        lt_present();
      } else if (ev.ch == 'c') { /* clear the canvas */
        lt_clear();
        draw_toolbar();
        draw_status(px, py);
        lt_present();
      }
    } else if (ev.type == LT_EVENT_MOUSE) {
      px = ev.x;
      py = ev.y;
      /* paint only inside the canvas (below the toolbar, above the status bar) */
      if (ev.key == LT_KEY_MOUSE_LEFT && ev.y >= 2 && ev.y <= lt_height() - 2)
        lt_set_cell(ev.x, ev.y, ' ', LT_DEFAULT, palette[cur]);
      draw_status(px, py);
      lt_present();
    } else if (ev.type == LT_EVENT_RESIZE) {
      lt_clear();
      draw_toolbar();
      draw_status(px, py);
      lt_present();
    }
  }

  lt_shutdown();
  return 0;
}

Build and run it. You should see a color toolbar up top and a blue status bar at the bottom. Press a number 1–7 to choose a color, then click-and-drag in the middle to paint. The status bar shows your pointer position as you move. Press c to clear, q to quit. You built a real little app! 🏆

What's new here?

One loop, many event types. The while loop now handles keys, mouse, and resize — each in its own else if. That's the shape of almost every real terminal app.

A palette array + number keys. palette[] holds our colors. The check ev.ch >= '1' && ev.ch <= '7' turns the typed digit into an index with cur = ev.ch - '1' (so '1' → 0).

lt_printf works like the C printf you may know, but draws into a cell at (x, y) with colors: lt_printf(x, y, fg, bg, "x=%d y=%d", px, py). Perfect for status lines and scores.

A status bar. We fill the bottom row with blue cells, then print white text on top. Filling a whole row with a background color is a classic UI trick.

Drawing regions. The canvas is "rows 2 to height - 2". The paint check ev.y >= 2 && ev.y <= lt_height() - 2 keeps your brush out of the toolbar and status bar.

Millions of colors

The named colors (LT_RED, …) are only 8 colors. For true color (16 million!), do two things:

  1. Tell libterm to use the richest colors the terminal supports:

    lt_set_output_mode(lt_detect_color_depth());

    lt_detect_color_depth() checks the terminal and returns the best mode; lt_set_output_mode switches to it. (Our program above already does this.)

  2. Make a color with LT_RGB(red, green, blue), where each part is 0–255:

    lt_attr orange = LT_RGB(255, 128, 0);
    lt_attr sky    = LT_RGB(135, 206, 235);
    lt_set_cell(10, 5, ' ', LT_DEFAULT, orange);

Try a rainbow gradient:

for (int x = 0; x < lt_width(); x++) {
  int r = (x * 255) / lt_width();
  lt_set_cell(x, 6, ' ', LT_DEFAULT, LT_RGB(r, 64, 255 - r));
}
lt_present();

If a terminal doesn't support true color, lt_detect_color_depth() picks the next-best mode and your colors are mapped to the closest available — your program still works.

The tools you now know

Tool What it's for
lt_init / lt_shutdown wake up / restore the screen
lt_set_cell draw one cell (a character + colors)
lt_print / lt_printf draw text (with %d, %s, …)
lt_clear / lt_present wipe the back buffer / show your drawing
lt_width / lt_height the screen size (for centering & layout)
lt_poll_event wait for a key, click, or resize
ev.key / ev.ch / ev.mod / ev.action what happened
lt_set_input_mode(LT_INPUT_MOUSE) turn on the mouse
lt_set_cursor / lt_hide_cursor place + show the cursor / hide it (hidden by default at init)
lt_set_output_mode / lt_detect_color_depth / LT_RGB true color
LT_BOLD, LT_UNDERLINE, LT_REVERSE, … text styles (OR with a color)

Where to go next

  • API Reference — every function, grouped by area.
  • Input & Events — the full event struct, every key/mouse code, and how input differs across terminals (including Windows).
  • Colors & Attributes — output modes and the color bit layout in detail.
  • Examples — the programs in examples/. The kbd demo is a great next thing to read: it's an on-screen keyboard and a live event inspector.

Ideas to keep going: add an eraser (right-click), a bigger brush, save your art to a file, or turn the hero from Tutorial 3 into a game that collects coins. You have all the pieces now — go build something fun. 🚀

Clone this wiki locally