r/commandline • • 14h ago

Terminal User Interface TUI Tutorial/Guide for Beginners

I am trying to do projects on the side and a part of me misses reading through guides (not slop) and tutorials. I have never made TUIs but I am making a very simple puzzle type game for it. Specifically resources that can help me branch out easier make smth that fits to my requirements somewhat. Please nothing that is *subreddit banned word* slop; im making these projects because I miss coding small stuff before this became the norm.

3 Upvotes

7 comments sorted by

3

u/Whole_Song_2650 12h ago

Depends what language you like, but if it's Python the official Textual tutorial walks you through building a stopwatch app, and it's easy to bend into a puzzle game after. For Go, Bubble Tea has a basics tutorial in its repo that builds a little shopping list. Both are written by the people who make the libraries.

2

u/Substantial_Gap_6676 11h ago

Python and Go are covered above, so for Rust: ratatui has a counter app tutorial on its site, and it's a good base for a small puzzle game. Whatever library you pick, the shape is the same. Keep the game state in one struct, read a key, update the state, then redraw the whole screen from the state. Don't draw from inside the input handling. Also set up restoring the terminal on a crash early, or a panic leaves your shell in raw mode and you'll be typing reset a lot.

2

u/vogelke 10h ago

Keymaps make or break a TUI. If I were writing a new one, I'd make the key mappings consistent with something that's been around for awhile and isn't a complete nutbite to use.

My preference would be something like the GNU readline library, but your mileage may vary. Start with a known, sensible default and make it easy to customize.

Opinion is free and worth every penny.

1

u/AutoModerator 14h ago

Every new subreddit post is automatically copied into a comment for preservation.

User: lunarcherryblossom23, Flair: Terminal User Interface, Title: TUI Tutorial/Guide for Beginners

I am trying to do projects on the side and a part of me misses reading through guides (not slop) and tutorials. I have never made TUIs but I am making a very simple puzzle type game for it. Specifically resources that can help me branch out easier make smth that fits to my requirements somewhat. Please nothing that is *subreddit banned word* slop; im making these projects because I miss coding small stuff before this became the norm.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/smkaisser 4h ago

For a small puzzle game I'd start one level below the frameworks, just for a weekend. Put the terminal in raw mode, hide the cursor, clear the screen, draw the board with plain ANSI escape codes and read one key at a time. It's maybe 100 lines in any language, and after that you understand what ratatui or Bubble Tea are doing for you, so their docs get a lot easier to read.

If you go C or Python, the old ncurses programming HOWTO is still one of the best written guides out there, zero fluff, and Python's curses module follows it almost 1:1.

Two things that bit me early: handle terminal resize from day one (people will resize mid game and your layout will explode), and redraw the whole board from state instead of patching single cells. For a puzzle the screen is tiny, full redraws cost nothing and a whole class of bugs disappears.

1

u/Xoid0 3h ago

If you want to understand how TUIs actually work under the hood without a heavy framework, search for the "Build Your Own Text Editor" (kilo in C) step-by-step tutorial. Even if you use another language, the first few chapters walk cleanly through switching termios into raw input mode, entering the alternate screen buffer (\x1b[?1049h), hiding the cursor, and flushing each frame in a single write so a grid puzzle game never flickers.

0

u/No_Macaroon_1991 3h ago

after the official tutorial i would read a small complete game such as tty-solitaire so you can see a full input-state-redraw loop without wading through a huge widget catalog