'use strict';

const _ = require('lodash');
const colors = require('colors/safe');

const ProgressBar = {

  init(options){

    this.startTime = process.hrtime();
    this.size = options.size;
    this.total = options.total;
    this.template = options.template;
    this.progress = options.progress;
    this.charComplete = options.complete;
    this.charPending = options.pending;

    this.render();

    process.on('exit', () => {
      if(this.percent < 100 ){
        this.newLine();
      }
    });

    return (this);
  },

  newLine(){
    process.stderr.write('\n');
  },

  render(){

    var percent = ((this.progress * 100) / this.total) || 0;
    var completed = this.size * (percent/100);
    var pending = this.size - completed;
    //█░░░
    var completedBar = _.repeat(this.charComplete, completed);
    var pendingBar = _.repeat(this.charPending, pending);
    var endTime = process.hrtime(this.startTime);
    var totalTime = Number(endTime[0] + (endTime[1] * 1e-9)).toFixed(2);
    //var bar = `Downloading [${completedBar}${pendingBar}] ${percent}% in ${totalTime} seconds`;

    var bar = this
      .template
      .replace(/\:b/, `${completedBar}${pendingBar}`)
      .replace(/\:p/, Number(percent).toFixed(0))
      .replace(/\:t/, totalTime);

    //console.log(bar, percent + '%');
    process.stderr.cursorTo(0);
    process.stderr.write(bar);
    process.stderr.clearLine(1);

    if (this.progress >= this.total) {
      this.percent = percent;
      this.newLine();
    }

    return (this);
  }
};

var settings = {
  size: 20,
  total: 20,
  progress: 0,
  template: 'Loading :b :p% in :ts',
  complete: colors.white('▪'),
  pending: colors.gray('▫')
};

module.exports = {
  colors: colors,
  config(options){
    _.extend(settings, options);
  },
  create(){

    var options = _.defaults(_.get(arguments, 0),
      settings);

    return Object
      .create(ProgressBar, {
        step: {
          value(size, total){
            if(total){
              this.total += total;
            }
            this.progress += size;
            this.render();
          }
        }
      })
      .init(options);
  }
};

@@ -117,17 +117,16 @@
 tions)%7B%0A
-%0A
     this
@@ -396,17 +396,16 @@
 nder();%0A
-%0A
     proc
@@ -498,17 +498,16 @@
     %7D);%0A
-%0A
     retu
@@ -740,19 +740,8 @@
 ed;%0A
-    //%E2%96%88%E2%96%91%E2%96%91%E2%96%91%0A
     
@@ -983,106 +983,8 @@
 (2);
-%0A    //var bar = %60Downloading %5B$%7BcompletedBar%7D$%7BpendingBar%7D%5D $%7Bpercent%7D%25 in $%7BtotalTime%7D seconds%60;
 %0A%0A  
@@ -1159,47 +1159,8 @@
 );%0A%0A
-    //console.log(bar, percent + '%25');%0A
     

remove comments