Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,16 @@ __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
* `opts` - Allows various options to be passed trough:
* `highlight` - Either "max" (default), "min" or none. Which hilights the biggest, least or none of the values.

__Example__

```js
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'));
console.log(Sparkline(reqsPerSec, 'reqs/sec', {"highlight": "min"}));
```

<a name="progress"></a>
Expand Down
26 changes: 23 additions & 3 deletions lib/clui.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@ var helpers = {
},

// Make a unicode sparkline chart
Sparkline: function (points, suffix) {
Sparkline: function (points, suffix, opts) {
if(typeof suffix == 'undefined')
suffix = '';

if(typeof opts == 'undefined')
opts = {'highlight': 'max'}




var max = Math.max.apply(Math, points);
var min = Math.min.apply(Math, points);

var scaledSequence = points.map(function (thisPoint) {
if(max === 0)
Expand All @@ -139,16 +146,29 @@ var helpers = {
var sparklineGraph = '';
var alreadyDrawnMax = false;
scaledSequence.forEach(function (symbolNumber) {
if(symbolNumber[1] == max & !alreadyDrawnMax)
if(symbolNumber[1] == max & !alreadyDrawnMax & opts.highlight == 'max')
{
sparklineGraph += clc.green(sparklineSymbols[symbolNumber[0]]);
alreadyDrawnMax = true;
}
else if(symbolNumber[1] == min & !alreadyDrawnMax & opts.highlight == 'min')
{
sparklineGraph += clc.red(sparklineSymbols[symbolNumber[0]]);
alreadyDrawnMax = true;
}
else
sparklineGraph += sparklineSymbols[symbolNumber[0]];
});

return sparklineGraph + ' ' + clc.blackBright(points[points.length-1] + suffix + ' (') + clc.green(max + suffix) + clc.blackBright(')');
var statistic = "";

if(opts.highlight == "max")
statistic = clc.blackBright(' (') + clc.green(max + suffix) + clc.blackBright(')');

if(opts.highlight == "min")
statistic = clc.blackBright(' (') + clc.red(min + suffix) + clc.blackBright(')');

return sparklineGraph + ' ' + clc.blackBright(points[points.length-1] + suffix) + statistic;
},

// Interface for storing multiple lines and then outputting them all at once.
Expand Down