'use strict'
module.exports = function (input_obj, opts) {
  var barChar = require('figures').square
  var Stats = require('fast-stats').Stats
  var width = require('window-size').width
  var ObjectValues = require('object.values')

  var maxBarWidth = width - (width > 20 ? 20 : 1)
  var total_sum = 0.0
  var percentage = []
  var barWidths = []
  var index
  var values = ObjectValues(input_obj)

  var printValues = false;

  if (opts !== undefined) {
	  if (opts.values === 'true' || opts.values === true) {
		  printValues = true;
	  }
  }

  // TODO: Oneliner
  for (index in input_obj) {
    total_sum += input_obj[index]
  }

  // TODO: Use map
  for (index in input_obj) {
    percentage.push((input_obj[index] / total_sum) * 100)
  }

  // TODO: Find maximum directly using something like lodash.max
  var max_percentage = (new Stats().push(percentage)).range()[1]

  var fixedLabels = []
  var labelsTrue = opts ? opts.labels : false
  var maxLabelLength = 0

  if (labelsTrue) {
    // TODO: Use oneliners
    for (index in input_obj) {
      maxLabelLength = (index.toString().length > maxLabelLength)
        ? index.toString().length : maxLabelLength
    }

    // TODO: Use oneliners
    for (index in input_obj) {
      var padLength = maxLabelLength - index.toString().length + 1
      fixedLabels.push(index + (padLength > 0
          ? new Array(padLength).join(' ') : ''))
    }
  }

  maxBarWidth = labelsTrue ? (maxBarWidth - maxLabelLength) : maxBarWidth

  // TODO: map
  percentage.forEach(function (element, index, array) {
    var potentialWidth = Math.ceil(element / max_percentage * maxBarWidth)
    barWidths.push(potentialWidth <= 1 ? 0 : potentialWidth)
  })

  console.log(barWidths);

  // TODO: map
  percentage.forEach(function (element, index, array) {
    console.log((labelsTrue ? fixedLabels[index] : '') + ' : ' +
      new Array(barWidths[index]).join(barChar) + '  ' +
      (printValues ? (values[index].toFixed(2)) : (element.toFixed(2) + "%")))
  })

  // TODO: filter
  for (index in barWidths) {
    if (barWidths[index] === 0) {
      if (opts.warnings) {
        console.log("This terminal's width is too less!" + (labelsTrue ? ' You can remove labels, and try again!' : ''))
      }
      break
    }
  }

}

@@ -58,21 +58,178 @@
 %7B%0A  
-var barChar =
+// Ensure that %60opts%60 is always an object.%0A  opts = (opts && typeof opts === 'object') ? opts : %7B%7D%0A%0A  var barChar = (opts.ascii === true %7C%7C opts.ascii === 'true') ? '=' :
  req
@@ -549,17 +549,16 @@
 ut_obj)%0A
-%0A
   var pr
@@ -572,50 +572,8 @@
 es =
- false;%0A%0A  if (opts !== undefined) %7B%0A%09  if
  (op
@@ -621,43 +621,9 @@
 rue)
- %7B%0A%09%09  printValues = true;%0A%09  %7D%0A  %7D
+;
 %0A%0A  

Add support for 'ascii' flag/arg (sets 'barChar' to '=' when true). Ensure that 'opts' arg is always type object. Refactor 'printValues' assignment.