What is single-line-log?
The single-line-log npm package allows you to log messages to the console on a single line, overwriting the previous message. This is particularly useful for creating progress bars, status updates, or any other scenario where you want to update the console output without cluttering it with multiple lines.
What are single-line-log's main functionalities?
Basic Single Line Logging
This feature allows you to log messages on a single line, overwriting the previous message. The code sample demonstrates logging 'Loading...', then 'Still loading...' after 1 second, and finally 'Done!' after 2 seconds.
const log = require('single-line-log').stdout;
log('Loading...');
setTimeout(() => log('Still loading...'), 1000);
setTimeout(() => log('Done!'), 2000);
Progress Bar
This feature is useful for creating a progress bar. The code sample shows a progress bar that updates every second, increasing the progress by 10% each time until it reaches 100%.
const log = require('single-line-log').stdout;
let progress = 0;
const interval = setInterval(() => {
progress += 10;
log(`Progress: ${progress}%`);
if (progress >= 100) clearInterval(interval);
}, 1000);
Other packages similar to single-line-log
cli-progress
The cli-progress package provides a more advanced and customizable progress bar for command-line interfaces. It offers multiple progress bars, different bar styles, and more control over the appearance and behavior of the progress bars. Compared to single-line-log, cli-progress is more feature-rich and suitable for more complex use cases.
ora
The ora package is a terminal spinner that provides a simple way to display a spinner while performing asynchronous tasks. It supports different spinner styles and colors, and can be used to indicate ongoing operations. While single-line-log focuses on overwriting a single line, ora provides a more visually appealing way to show progress or status updates.
progress
The progress package is another option for creating progress bars in the terminal. It offers a simple API for creating and updating progress bars, with support for custom tokens and formatting. Compared to single-line-log, the progress package is specifically designed for progress bars and offers more customization options.
single-line-log
Node.js module that keeps writing to the same line in the console (or a stream). Very useful when you write progress bars, or a status message during longer operations. Supports multilines.
Installation
npm install single-line-log
Usage
var log = require('single-line-log').stdout;
var read = 0;
var size = fs.statSync('super-large-file').size;
var rs = fs.createReadStream('super-large-file');
rs.on('data', function(data) {
read += data.length;
var percentage = Math.floor(100*read/size);
log('Writing to super large file\n[' + percentage + '%]', read, 'bytes read');
});
.clear()
Clears the log (i.e., writes a newline).
var log = require('single-line-log').stdout;
log('Line 1');
log.clear();
log('Line 2');
.stdout
Outputs to process.stdout
.
.stderr
Outputs to process.stderr
.
License
MIT