The Out class in Softio provides utility methods for printing and formatting output to the console. These methods allow you to display messages with custom formatting, print errors, and even wrap functions with styled output.
You can access the Out class in two ways:
// Full import
const Console = require('softio');
Console.Out;
// Or using destructuring
const { Out } = require('softio');Prints messages to the console without a newline.
-
Parameters:
...message: One or more values to print. They are concatenated with a space.
-
Returns:
void -
Example:
Console.Out.write('Hello world!');
Console.Out.write('Welcome');
Console.Out.write([10, true, 'Hello']);
/*
Output:
Hello world!Welcome[10, true, "Hello"]
*/Prints messages to the console followed by a newline.
-
Parameters:
...message: One or more values to print.
-
Returns:
void -
Example:
Console.Out.writeln('Hello world!');
Console.Out.writeln('Welcome');
Console.Out.writeln([10, true, 'Hello']);
/*
Output:
Hello world!
Welcome
[10, true, "Hello"]
*/Prints a formatted message, similar to C’s printf.
-
Parameters:
message: A format string with%vplaceholders....argv: Values to replace placeholders.
-
Returns:
void -
Notes:
- No newline is added automatically.
- Use
%vfor value substitution. - Supports all data types.
- If
%vplaceholders exceed the number of values,undefinedwill appear. - Extra values beyond the number of placeholders are ignored.
-
Example:
Console.Out.printf('Hello %v\n', 'John');
/*
Output:
Hello John
*/Prints a formatted message to stderr, typically used for errors.
-
Parameters:
message: A format string with%vplaceholders....argv: Values to replace placeholders.
-
Returns:
void -
Notes: Same formatting behavior as
printf. -
Example:
Console.Out.error('Error: %v\n', 'Something went wrong');
/*
Output to stderr:
Error: Something went wrong
*/Wraps a given function and enhances its output with styling (e.g., color, background, underline). Useful for dynamically creating styled versions of write, printf, or any other output function.
-
Parameters:
func: A function to wrap (e.g.,Console.Out.write).style: An object defining output styles such ascolor,background, andstyle(e.g., underline).
-
Returns: A new function with the same behavior, but styled output.
-
Notes:
- Preserves arguments passed to the original function.
- Works with native output functions like
process.stdout.write.
-
Example:
const print = Console.Out.shot(Console.Out.write, {
color: 'blue',
background: 'red',
style: 'underline'
});
print('Styled text: blue, red background, underlined');- Bug: When using this function, you should be careful not to break the defined styles by passing a function as input to this function. Consider the following example:
const fn = Out.shot(Out.printf, {
color: 'blue'
});
/*
Note: In the output, the entire text is not blue,
because when the `printf` function wants to
place the number 10, it places it with the
style associated with numbers, and this position
causes the text tone to change and the rest of
the text to become the default color.
*/
fn("Hello %v with id %v and role %v.\n", "user", 10, "Admin");When using this function, give it functions that do not change the defined styles.
| Method | Description |
|---|---|
write |
Print to stdout without newline |
writeln |
Print to stdout with newline |
printf |
Formatted output (C-style) |
error |
Formatted output to stderr |
shot |
Create a styled version of a function |
For advanced formatting or theming output, consider combining shot() with existing Out methods.
- Back to the previous section: 📘 Introduction ←
- Continue to the next section: 📥 Input Methods →