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:
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
To customize your forms’ appearance, use themes:
import { huh } from "charsm"; huh.SetTheme("dracula");
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
A simple confirmation dialog with customizable Yes (affirmative) and No (negative) buttons:
const m = huh.Confirm("Do you want to proceed?", "Yes", "No");
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"); }
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());
Validators are defined as a comma-separated string. For example, "no_numbers,required" ensures the input meets all conditions before continuing.
Validators include:
Modes:
pnpm add charsm
import { huh } from "charsm"; huh.SetTheme("dracula");
huh.SetTheme("dracula"); // Components here use Dracula huh.SetTheme("Catppuccin"); // Components here use Catppuccin // Available themes: default, Charm, Base16, Dracula, Catppuccin
Forms can hold multiple groups, rendering them sequentially. Here’s an example:
const m = huh.Confirm("Do you want to proceed?", "Yes", "No");
Values from the form are stored in each component’s value property:
if (m.run() === "1") { console.log("User chose the affirmative option"); } else { console.log("User chose the negative option"); }
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 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());
When you do this, Huh will render the groups in a staggered order:
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 .
Thanks for reading—here’s to a fantastic 2025! ?
The above is the detailed content of How To Build Beautiful Terminal UIs (TUIs) in JavaScript forms!. For more information, please follow other related articles on the PHP Chinese website!