Examples

Here are some examples of how to use ts-better-consolein your TypeScript projects. These examples demonstrate various features and use cases of the library to help you get the most out of it.

Styled Logs

You can create styled logs using the s function. This allows you to customize the appearance of your logs with different colors, backgrounds, and styles.

import betterConsole, { s } from "ts-better-console";

betterConsole.(("This is a styled log", {
color: "white",
backgroundColor: "blue",
styles: ["bold", "italic"],
}));

Output

This is a styled log

In this example, we create a styled log with white text on a blue background, and apply bold and italic styles to it.

OS Simulator Demo

A full-featured terminal demo that simulates a Linux-like OS boot sequence using ts-better-console. It combines Card, prompt() with password masking and arrow-key history, styled logs, and graceful Ctrl+C handling into a single cohesive experience.

Run it with:

$ bun run test/demo/os-sample.ts

Highlights

  • Boot sequence — simulates dmesg-style kernel messages with timed delays.
  • Login prompt — uses prompt(question, "password") to mask the password entry. Allows up to 3 attempts.
  • Interactive shell — uses prompt(question, "text", true) for a shell-like REPL with ↑ / ↓ command history. Supports help, clear, shutdown, and exit.
  • Graceful Ctrl+C — intercepts SIGINT and runs a simulated shutdown or emergency boot-interrupt depending on how far the boot progressed.
import betterConsole, { Card, cs } from "ts-better-console";

// ...imports for booting / login / shell / shutdown

betterConsole.log(
new Card(cs([...]), "2/3", { border: { symbols: { style: "double" } } }).render()
);

await booting();
await login();
welcomeMessage();
await shell();
await shutdown();

Explore More!

This is just a quick overview of some of the features of ts-better-console. The library offers many more capabilities and customization options to enhance your logging experience. Be sure to check out the full documentation for more details and examples on how to make the most of ts-better-console in your projects.

Community Examples

We also encourage you to explore examples and use cases shared by the community. You can find these in our Discord server, where developers share their experiences and creative uses of ts-better-console.

Want to support this project?

If you find ts-better-console useful and want to support its development, consider starring the GitHub repository or buying me a coffee! Your support helps me dedicate more time to improving the library and adding new features.

Want to contribute to this project?

Contributions are welcome! If you're interested in improving the library, fixing bugs, or adding new features, feel free to check out the GitHub repository and submit a pull request. Whether you're a seasoned developer or new to open source, your contributions can make a difference!