Logo

dev-resources.site

for different kinds of informations.

How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!

Published at
1/15/2025
Categories
webdev
javascript
beginners
tutorial
Author
sfundomhlungu
Author
13 person written this
sfundomhlungu
open
How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!

I’m obsessed with TUIs—maybe you are too! If not yet, I hope you will be, because they’re not just fun but incredibly useful!

About two months ago, I ported Lipgloss from Go to WebAssembly. That was the first article in this series! My next plan was to port forms, but—long story short—some features couldn’t make the jump to WASM. Native features and runtime limitations threw up roadblocks, so I went a step lower: DLLs and SO files (shared libraries). And finally, we have forms!

Note: To use shared libraries in JavaScript, you’ll need Node.js with native module support and node-gyp for the C bindings.

The simplest way? Reinstall Node.js and select the native modules option during setup:

Native Node

Prefer manual installation? Follow this README.


Why DLLs and SO files?

They’re much smaller compared to WASM, and I might end up rewriting everything to take advantage of that!

If you’re ready to dive in, set up a new JavaScript project and install charsm:

pnpm add charsm
Enter fullscreen mode Exit fullscreen mode

Forms in the CLI

1. Theme Customization

To customize your forms’ appearance, use themes:

import { huh } from "charsm";
huh.SetTheme("dracula");
Enter fullscreen mode Exit fullscreen mode

All components defined afterward will use the Dracula theme. You can override the theme anytime:

huh.SetTheme("dracula"); 
// Components here use Dracula
huh.SetTheme("Catppuccin");
// Components here use Catppuccin

// Available themes: default, Charm, Base16, Dracula, Catppuccin
Enter fullscreen mode Exit fullscreen mode

2. Create a Confirmation Dialog

A simple confirmation dialog with customizable Yes (affirmative) and No (negative) buttons:

const m = huh.Confirm("Do you want to proceed?", "Yes", "No");
Enter fullscreen mode Exit fullscreen mode

When run, it returns "1" for Yes and "0" for No. pointer to strings in shared libraries are easy to return:

if (m.run() === "1") {
  console.log("User chose the affirmative option");
} else {
  console.log("User chose the negative option");
}
Enter fullscreen mode Exit fullscreen mode

3. Create an Input Field

Example 1: Single Input

Define input fields with validation and placeholders:

const i = new huh.NewInput(
  {
    Title: "Username",
    Description: "Enter your name",
    Placeholder: "e.g., John Doe",
    validators: "no_numbers,required", 
  },
  0 // Mode: Single Input
);
i.load();
console.log(i.run());
Enter fullscreen mode Exit fullscreen mode

Validators are defined as a comma-separated string. For example, "no_numbers,required" ensures the input meets all conditions before continuing.

validators

Validators include:

  • required
  • email
  • no_numbers
  • alpha_only
  • no_special_chars

Modes:

  • 0: Single-line input
  • 1: Multiline text area

Example 2: Multiline Input

const s = new huh.NewInput(
  {
    Title: "Search",
    Description: "Enter query",
    Placeholder: "Type something...",
    validators: "required",
  },
  1 // Mode: Multiline
);
s.load();
console.log(s.run());
Enter fullscreen mode Exit fullscreen mode

4. Create a Selection Component

const c = huh.Select("Choose your favorite person", ["Opt1", "Opt2", "Opt3"]);
console.log(c.run()); // Returns the selected option
Enter fullscreen mode Exit fullscreen mode

5. Add a Spinner

huh.Spinner(2, "Doing some work");
Enter fullscreen mode Exit fullscreen mode

6. Creating Forms

Forms can hold multiple groups, rendering them sequentially. Here’s an example:

const m = huh.Confirm("All your codebase are belong to us?", "Yes", "No");

const single_ = new huh.NewInput(
  {
    Title: "Username",
    Description: "Enter your name",
    Placeholder: "e.g., John Doe",
    validators: "no_numbers,required",
  },
  0
);
single_.load();

const multiline_ = new huh.NewInput(
  {
    Title: "Search",
    Description: "Enter query",
    Placeholder: "Type something...",
    validators: "required",
  },
  1
);
multiline_.load();

const s = huh.Select("Choose your favorite person", ["Opt1", "Opt2", "Opt3"]);

const group = huh.CreateGroup(`${m.id},${single_.id},${multiline_.id},${s.id}`);
huh.CreateForm(group);
Enter fullscreen mode Exit fullscreen mode

full form

Values from the form are stored in each component’s value property:

if (m.value === "1") {
  console.log("The user knows the Zig reference!");
} else {
  console.log(":(");
}

console.log("Name: ", single_.value);
console.log("Query: ", multiline_.value);
console.log("Favorite person: ", s.value);
Enter fullscreen mode Exit fullscreen mode

Validators can be a bit buggy on Linux for forms (I might’ve skipped building updated .so files—oops!). If you’re curious or want to lend a hand, check out these repos for updates—or even better, contribute!

  • Charsm: Good first issue – Remove the load method in inputs so it’s automatically called in huh.NewInput.

  • Huh Shared Lib Code: Two good first issues – Fix the incorrect README documentation and add a build file for macOS support.

Now, let’s talk groups. You can create multiple groups and pass them to a single form like this:

const group = huh.CreateGroup(`${m.id},${single_.id}`);
const group2 = huh.CreateGroup(`${multiline_.id},${s.id}`);

huh.CreateForm(`${group},${group2}`);
Enter fullscreen mode Exit fullscreen mode

When you do this, Huh will render the groups in a staggered order:

staggered form

Pretty cool, right? Huge shoutout to Charm for their amazing tools! This is just the tip of the iceberg. I’ll keep updating and refining this tool to make it even more useful.

Want a complete example? Check out Building a Terminal Coffee Shop.

For something lighter but in the same spirit as DLLs, read my article on Loading a Go Process in Node.js.

If you’re into deep-dive, non-blog-friendly content—think long series and unpolished gems designed to level up your dev skills—follow me on Substack you can also find me on x .

Upcoming Articles and Series on Substack:

  • P2P Series: Building Livescribe, an open-source peer-to-peer writing app (desktop and mobile) in Go.
  • Custom Template Engine: Crafting one from scratch.
  • Low-Level Libuv Server: Exploring custom protocols and server building.
  • Windows Universal Meets WebView: Stylish domination.
  • RAG Apps: Delving into retrieval-augmented generation.
  • Custom Node.js C/C++ Modules: Pushing Node.js to its limits.

Thanks for reading—here’s to a fantastic 2025! 🚀

webdev Article's
30 articles in total
Web development involves creating websites and web applications, combining coding, design, and user experience to build functional online platforms.
Favicon
7 Developer Tools That Will Boost Your Workflow in 2025
Favicon
Lessons from A Philosophy of Software Design
Favicon
Can I build & market a SaaS app to $100 in 1 month?
Favicon
Learning HTML is the best investment I ever did
Favicon
Creating a live HTML, CSS and JS displayer
Favicon
How to scrape Crunchbase using Python in 2024 (Easy Guide)
Favicon
🕒 What’s your most productive time of the day?
Favicon
Daily.dev's unethical software design
Favicon
Unique Symbols: How to Use Symbols for Type Safety
Favicon
How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!
Favicon
[Boost]
Favicon
CĂłmo Iniciar y Crecer como Desarrollador Frontend en 2025
Favicon
Filling a 10 Million Image Grid with PHP for Internet History
Favicon
Building bun-tastic: A Fast, High-Performance Static Site Server (OSS)
Favicon
Chronicles of Supermarket website
Favicon
Easy development environments with Nix and Nix flakes!
Favicon
My React Journey: Project
Favicon
Boost Your Productivity with Momentum Builder: A Web App to Overcome Procrastination and Track Progress
Favicon
Что делает React Compiler?
Favicon
Day 04: Docker Compose: Managing multi-container applications
Favicon
Setup Shopify GraphQL Admin API Client in Hydrogen
Favicon
The Language Server Protocol - Building DBChat (Part 5)
Favicon
How to Use JavaScript to Reduce HTML Code: A Simple Example
Favicon
From Bootcamp to Senior Engineer: Growing, Learning, and Feeling Green
Favicon
📝✨ClearText
Favicon
Habit Tracker: A Web Application to Track Your Daily Habits
Favicon
Impostor syndrome website: Copilot 1-Day Build Challenge
Favicon
Easy Discount Calculation: Tax, Fees & Discount Percentage Explained
Favicon
Example of using Late Static Binding in PHP.
Favicon
Top 5 Python Scripts to Automate Your Daily Tasks: Boost Productivity with Automation

Featured ones: