Nodejs terminal prints progress bar instance code and nodejs progress bar
1. Scenario Import
When we batch process a large number of files (for example, upload/download, save, and compile), we often want to know how the current progress is or how many failed (successful) tasks are; when our code or program has been released, a proper (Terminal/command line) progress bar can accurately reflect the installation steps and processes during installation, improve the availability of the program and relieve the user's troubles while waiting ......
2. Basic Principles
First, it is easier to print the text on the terminal. Then, you can use simple text and symbols to piece together the effect of the command line (in the following example ):
File Uploaded: 43.60% when there are too many threads, there are too many threads, too many threads please refer to the following link for more information:
Of course, you can design the progress bar based on your needs. I will just give you a reference here.
Here, I construct the print command line method into a tool module progress-bar.js
, The specific implementation is as follows :-)
// A very practical npm module is used to print the text var slog = require ('single-line-log') in the same line '). stdout; // The encapsulated ProgressBar tool function ProgressBar (description, bar_length) {// two basic parameters (attributes) this. description = description | 'progress'; // The text information starting with the command line. this. length = bar_length | 25; // length of the progress bar (unit: character). The default value is 25. // this is the method for refreshing the progress bar pattern and text. render = function (opts) {var percent = (opts. completed/opts. total ). toFixed (4); // progress (number of subtasks completed divided by the total number) var cell_num = Math. floor (percent * this. length); // calculate the number of tokens required to splice the pattern. // splice the black var cell = ''; for (var I = 0; I <cell_num; I ++) {cell + = 'blood';} // splice the gray var empty = ''; for (var I = 0; I <this. length-cell_num; I ++) {empty + = 'authorization';} // concatenate the final text var plain text = this. description + ':' + (100 * percent ). toFixed (2) + '%' + cell + empty + ''+ opts. completed + '/' + opts. total; // output text slog (plain text) ;}}// module export module. exports = ProgressBar;
3. Run it.
Based on the above implementation, let's first talk about this progress-bar.js
Usage:
// Introduce the tool module var ProgressBar = require ('. /progress_bar '); // initialize a ProgressBar instance with a progress bar length of 50 (var pb = new ProgressBar ('Download progress', 50); // This is just an example of pb usage, does not contain any function var num = 0, total = 200; function downloading () {if (num <= total) {// update progress bar pb. render ({completed: num, total: total}); num ++; setTimeout (function () {downloading () ;}, 500) }} downloading ();
Run the above Code and the execution result is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.