介绍
安装
npm install clui
LineBuffer(options)
Options
- x - The X location of where to draw the lines in this buffer.
- y - The Y location of where the draw the lines.
- width - How wide the buffer is in columns. Any lines longer than this will be cropped. You can specify either an integer value or 'console' in order to let the width of the console determine the width of the LineBuffer.
- height - How high the buffer is in rows. You can either pass in an integer value or 'console' to let the height on the console determine the height of the LineBuffer.
- scroll - Where the user is scrolled to in the buffer
Functions
- height() - Return the height of the LineBuffer, in case you specified it as 'console'
- width() - Return the width of the LineBuffer, in case you specified it as 'console'
- addLine(Line) - Put a Line object into the LineBuffer.
- fill() - If you don't have enough lines in the buffer this will fill the rest of the lines with empty space.
- output() - Draw the LineBuffer to screen.
example
var CLI = require('clui'),
clc = require('cli-color');
var Line = CLI.Line,
LineBuffer = CLI.LineBuffer;
var outputBuffer = new LineBuffer({
x: 0,
y: 0,
width: 'console',
height: 'console'
});
var message = new Line(outputBuffer)
.column('Title Placehole', 20, [clc.green])
.fill()
.store();
var blankLine = new Line(outputBuffer)
.fill()
.store();
var header = new Line(outputBuffer)
.column('Suscipit', 20, [clc.cyan])
.column('Voluptatem', 20, [clc.cyan])
.column('Nesciunt', 20, [clc.cyan])
.column('Laudantium', 11, [clc.cyan])
.fill()
.store();
var line;
for(var l = 0; l < 20; l++)
{
line = new Line(outputBuffer)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 11)
.fill()
.store();
}
outputBuffer.output();
Line(outputBuffer)
Chainable Functions
- padding(width) - Output width characters of blank space.
- column(text, width, styles) - Output text within a column of the specified width. If the text is longer than width it will be truncated, otherwise extra padding will be added until it is width characters long. The styles variable is a list of cli-color styles to apply to this column.
- fill() - At the end of a line fill the rest of the columns to the right edge of the terminal with whitespace to erase any content there.
- output() - Print the generated line of text to the console.
- contents() - Return the contents of this line as a string.
Example
var clui = require('clui'),
clc = require('cli-color'),
Line = clui.Line;
var headers = new Line()
.padding(2)
.column('Column One', 20, [clc.cyan])
.column('Column Two', 20, [clc.cyan])
.column('Column Three', 20, [clc.cyan])
.column('Column Four', 20, [clc.cyan])
.fill()
.output();
var line = new Line()
.padding(2)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 20)
.column((Math.random()*100).toFixed(3), 20)
.fill()
.output();
Gauge(value, maxValue, gaugeWidth, dangerZone, suffix)
Parameters
- value - The current value of the metric being displayed by this gauge
- maxValue - The highest possible value of the metric being displayed
- gaugeWidth - How many columns wide to draw the gauge
- dangerZone - The point after which the value will be drawn in red because it is too high
- suffix - A value to output after the gauge itself.
example
var os = require('os'),
clui = require('clui');
var Gauge = clui.Gauge;
var total = os.totalmem();
var free = os.freemem();
var used = total - free;
var human = Math.ceil(used / 1000000) + ' MB';
console.log(Gauge(used, total, 20, total * 0.8, human));
Sparkline(values, suffix)
Parameters
- values - An array of values to go into the sparkline
- suffix - A suffix to use when drawing the current and max values at the end of sparkline
Example
var Sparkline = require('clui').Sparkline;
var reqsPerSec = [10,12,3,7,12,9,23,10,9,19,16,18,12,12];
console.log(Sparkline(reqsPerSec, 'reqs/sec'));
Progress(length)
Parameters
- length - The desired length of the progress bar in characters.
methods
- update(currentValue, maxValue) - Returns the progress bar min/max content to write to stdout. Allows for dynamic max values.
- update(percent) - Returns the progress bar content as a percentage to write to stdout. 0.0 > value < 1.0.
Example
var clui = require('clui');
var Progress = clui.Progress;
var thisProgressBar = new Progress(20);
console.log(thisProgressBar.update(10, 30));
var thisPercentBar = new Progress(20);
console.log(thisPercentBar.update(0.4));
Spinner(statusText)
Parameters
- statusText - The default status text to display while the spinner is spinning.
- style - Array of graphical characters used to draw the spinner. By default, on Windows:
['|', '/', '-', ''], on other platforms: ['◜','◠','◝','◞','◡','◟']
Methods
- start() - Show the spinner on the screen.
- message(statusMessage) - Update the status message that follows the spinner.
- stop() - Erase the spinner from the screen.
Example
var CLI = require('clui'),
Spinner = CLI.Spinner;
var countdown = new Spinner('Exiting in 10 seconds... ', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);
countdown.start();
var number = 10;
setInterval(function () {
number--;
countdown.message('Exiting in ' + number + ' seconds... ');
if (number === 0) {
process.stdout.write('\n');
process.exit(0);
}
}, 1000);