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! 🚀

beginners Article's
30 articles in total
Beginner-friendly resources provide step-by-step guidance and foundational knowledge for those new to coding or technology.
Favicon
7 Developer Tools That Will Boost Your Workflow in 2025
Favicon
Creating a live HTML, CSS and JS displayer
Favicon
Build Your First AI Application Using LlamaIndex!
Favicon
Creating Arrays with Reference Variables
Favicon
How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!
Favicon
CĂłmo Iniciar y Crecer como Desarrollador Frontend en 2025
Favicon
Chronicles of Supermarket website
Favicon
Building a Serverless REST API with AWS Lambda and API Gateway
Favicon
ruby -run
Favicon
Day 04: Docker Compose: Managing multi-container applications
Favicon
From "Never Engineering" to "Why Not?"
Favicon
From Bootcamp to Senior Engineer: Growing, Learning, and Feeling Green
Favicon
Easy Discount Calculation: Tax, Fees & Discount Percentage Explained
Favicon
How to Resolve the 'Permission Denied' Error in PHP File Handling
Favicon
Introduction to Terraform: Revolutionizing Infrastructure as Code
Favicon
The Great Failure of 2024
Favicon
2025: The Year of Decentralization – How Nostr Will Make You a Standout Developer
Favicon
Amazon S3 vs. Glacier: Data Archival Explained
Favicon
What is Next Js: A Beginner's guide to Next Js
Favicon
Debugging Adventure Day 1: What to Do When Your Code Doesn’t Work
Favicon
Top 5 SaaS Trends for 2025
Favicon
Easy 301 Redirects For SEO
Favicon
How to Choose the Right Shopify Theme for Your Business Needs
Favicon
ruby -run, again
Favicon
Булеві типи і вирази
Favicon
Build a Secure Password Generator with Javascript
Favicon
5 Fun Projects to Master ES6 Javascript Basics in 2025 🚀👨‍💻
Favicon
got Tired of analysis paralyysis so i built an extensioon to get into flow faster
Favicon
Survival Manual: How to Create and Manage a Project in Git
Favicon
badly I want to know how to code😭😭

Featured ones: