The Utils class in Softio offers general-purpose utility functions to help with console formatting, color styling, screen control, and text manipulation.
You can access the Utils class in two ways:
const Console = require('softio');
Console.Utils; // Access the Utils classOr using object destructuring:
const { Utils } = require('softio');Centers a given string by adding appropriate spaces based on the console's width.
-
Parameters:
message— The string to be centered.
-
Returns: The centered string.
Example:
Console.Out.writeln(Console.Utils.center('Welcome to Softio!'));Clears the entire console screen.
Example:
Console.Utils.clear();Returns an ANSI escape code that resets all text formatting (color, style, etc.) to default.
Example:
Console.write(Console.Utils.reset());Generates an ANSI code to change the text color.
-
Supports:
- Named colors (e.g.,
'red') - HEX formats (
'#fff','000','ffffff') - RGB arrays (
[255, 100, 0]) - ANSI 256 color codes (
0–255)
- Named colors (e.g.,
Example:
const red = Console.Utils.color('red');
const reset = Console.Utils.reset();
Console.write(`${red}This text is red${reset}`);Generates an ANSI code to change the background color.
- Supports: Same formats as
color.
Example:
const bg = Console.Utils.background('#0000ff');
Console.write(`${bg}Text with blue background${Console.Utils.reset()}`);Returns an ANSI code to apply a text style.
-
Supported Styles:
bolddimitalicunderlineblinkingreversehiddenstrikethroughdefault(resets text style)
Example:
const underline = Console.Utils.fontStyle('underline');
Console.write(`${underline}Underlined text${Console.Utils.reset()}`);Formats any kind of data (arrays, objects, primitives) into a readable, printable string — similar to JSON/stringify with enhancements for console output.
- Parameters: Accepts any number of values of any type.
- Returns: A formatted string representation.
Example:
const user = { id: 1, name: 'admin' };
process.stdout.write(Console.Utils.prettier(user));Note: All output methods prepare data for output by default and you no longer need to use this method with output methods.
Console.write(Console.Utils.color([0, 255, 0]) + 'Green RGB Text' + Console.Utils.reset());
Console.write(Console.Utils.background(42) + 'ANSI Background' + Console.Utils.reset());
Console.Out.writeln(Console.Utils.center('Centered Title'));The Utils class helps simplify many common terminal formatting tasks such as:
- Coloring text/backgrounds with rich input formats
- Styling text using ANSI styles
- Centering and clearing the console
- Safely formatting and displaying any data
- Back to the previous section: 📥 Input Methods ←
- Continue to the next section: 🎛️ Attr Methods →