Skip to content

Commit

Permalink
A progress bar controler now has a .reset() method cronvel#77
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Ronvel committed Dec 7, 2018
1 parent ab4819d commit c4b1621
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 90 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

v1.26.8
-------

A progress bar controler now has a .reset() method #77


v1.26.7
-------

Expand Down
2 changes: 2 additions & 0 deletions doc/high-level.md
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,8 @@ The controller provides those functions:

* resume(): resume a previously stopped progress bar, it will be redrawn again

* reset(): reset the progress bar, removing progress value, items done, time elapsed and so on...



Example of a progress bar using fake progress values:
Expand Down
14 changes: 9 additions & 5 deletions lib/progressBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ module.exports = function progressBar_( options ) {


var redraw = updated => {

var time , itemIndex , itemName = itemFiller , titleName = titleFiller ,
innerBarSize , progressSize , voidSize ,
progressBar = '' , voidBar = '' , percent = '' , eta = '' ;
Expand Down Expand Up @@ -307,7 +306,6 @@ module.exports = function progressBar_( options ) {
}
else {
// Get the cursor location before getting started

this.getCursorLocation( ( error , x_ , y_ ) => {

var oldWidth_ = width ;
Expand All @@ -329,7 +327,6 @@ module.exports = function progressBar_( options ) {
}

controller.startItem = name => {

itemsStarted.push( name ) ;

// No need to redraw NOW if there are other items running.
Expand All @@ -351,7 +348,6 @@ module.exports = function progressBar_( options ) {
} ;

controller.itemDone = name => {

var index ;

itemsDone ++ ;
Expand Down Expand Up @@ -381,7 +377,6 @@ module.exports = function progressBar_( options ) {
} ;

controller.update = toUpdate => {

if ( ! toUpdate ) { toUpdate = {} ; }
else if ( typeof toUpdate === 'number' ) { toUpdate = { progress: toUpdate } ; }

Expand Down Expand Up @@ -447,6 +442,15 @@ module.exports = function progressBar_( options ) {
}
} ;

controller.reset = () => {
etaStartingTime = startingTime = ( new Date() ).getTime() ;
itemsDone = 0 ;
progress = undefined ;
itemsStarted.length = 0 ;
wheelCounter = itemRollCounter = updateCount = progressUpdateCount = 0 ;
redraw() ;
} ;

return controller ;
} ;

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terminal-kit",
"version": "1.26.7",
"version": "1.26.8",
"description": "256 colors, keys and mouse, input field, progress bars, screen buffer (including 32-bit composition and image loading), text buffer, and many more... Whether you just need colors and styles, build a simple interactive command line tool or a complexe terminal app: this is the absolute terminal lib for Node.js!",
"main": "lib/termkit.js",
"directories": {
Expand Down
157 changes: 73 additions & 84 deletions sample/progress-bar-items-test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/env node
/*
Terminal Kit
Copyright (c) 2009 - 2018 Cédric Ronvel
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -34,98 +34,87 @@


var fs = require( 'fs' ) ;
var term = require( '..' ).terminal ;


var timeFactor = 10 ;
var progress ;
var progressBar ;
var queuedFiles = [] , inProgressFiles = [] ;

require( '../lib/termkit.js' ).getDetectedTerminal( function( error , term ) {


var progress ;
var progressBar ;
var queuedFiles = [] , inProgressFiles = [] ;

function doProgress()
{
var file ;

if ( progress === undefined )
{
if ( Math.random() < 0.1 )
{
progress = 0 ;
}

progressBar.update( progress ) ;
setTimeout( doProgress , 200 + Math.random() * 600 ) ;


function doProgress() {
var file ;

if ( progress === undefined ) {
if ( Math.random() < 0.1 ) {
progress = 0 ;
}

progressBar.update( progress ) ;
setTimeout( doProgress , 2 * timeFactor + Math.random() * 6 * timeFactor ) ;
}
else {
if ( queuedFiles.length && ( ! inProgressFiles.length || Math.random() < 0.5 ) ) {
//console.log( '\nstartItem\n' ) ;
file = queuedFiles.shift() ;
progressBar.startItem( file ) ;
inProgressFiles.push( file ) ;
}
else
{
if ( queuedFiles.length && ( ! inProgressFiles.length || Math.random() < 0.5 ) )
{
else {
//console.log( '\nitemDone\n' ) ;
progressBar.itemDone( inProgressFiles.shift() ) ;

if ( ! inProgressFiles.length && queuedFiles.length ) {
//console.log( '\nstartItem\n' ) ;
file = queuedFiles.shift() ;
progressBar.startItem( file ) ;
inProgressFiles.push( file ) ;
}
else
{
//console.log( '\nitemDone\n' ) ;
progressBar.itemDone( inProgressFiles.shift() ) ;

if ( ! inProgressFiles.length && queuedFiles.length )
{
//console.log( '\nstartItem\n' ) ;
file = queuedFiles.shift() ;
progressBar.startItem( file ) ;
inProgressFiles.push( file ) ;
}
}

if ( queuedFiles.length + inProgressFiles.length === 0 )
{
setTimeout(
function() { term( '\n' ) ; process.exit() ; } ,
2000
) ;
}
else
{
setTimeout( doProgress , 2000 + Math.random() * 2000 ) ;
}
}

if ( queuedFiles.length + inProgressFiles.length === 0 ) {
setTimeout(
() => { term( '\n' ) ; process.exit() ; } ,
20 * timeFactor
) ;
}
else {
setTimeout( doProgress , 20 * timeFactor + Math.random() * 20 * timeFactor ) ;
}
}

//term.bold( 'Analysing files: ' ) ;

progressBar = term.progressBar( {
width: 80 ,
percent: true ,
eta: true ,
title: 'Analysing files:' ,
//inline: true ,
/*
barStyle: term.brightGreen.bold ,
barBracketStyle: term.brightWhite ,
percentStyle: term.brightMagenta.inverse ,
barChar: '~' ,
barHeadChar: '*' ,
//barChar: ' ' ,
//barHeadChar: ' ' ,
//barStyle: term.bgCyan
//*/
} ) ;

term.column( 1 ) ;

fs.readdir( __dirname , function( error , files ) {
if ( error ) { process.exit( 1 ) ; }
queuedFiles = files ;
progressBar.update( { items: files.length } ) ;
doProgress() ;
} ) ;
}

//term.bold( 'Analysing files: ' ) ;

progressBar = term.progressBar( {
width: 80 ,
percent: true ,
eta: true ,
title: 'Analysing files:'
//inline: true ,
/*
barStyle: term.brightGreen.bold ,
barBracketStyle: term.brightWhite ,
percentStyle: term.brightMagenta.inverse ,
barChar: '~' ,
barHeadChar: '*' ,
//barChar: ' ' ,
//barHeadChar: ' ' ,
//barStyle: term.bgCyan
//*/
} ) ;

//setTimeout( () => progressBar.reset() , 5000 ) ;

term.column( 1 ) ;

fs.readdir( __dirname , ( error , files ) => {
if ( error ) { process.exit( 1 ) ; }
queuedFiles = files ;
progressBar.update( { items: files.length } ) ;
doProgress() ;
} ) ;

0 comments on commit c4b1621

Please sign in to comment.