// Generated by CoffeeScript 2.0.2
var wcwidth;

wcwidth = require('wcwidth');

module.exports = function(text, length, options) {
  var escapecolor, invert, pad, padlength, textnocolors;
  if (options == null) {
    options = {};
  }
  invert = typeof text === 'number';
  if (invert) {
    [length, text] = [text, length];
  }
  if (typeof options === 'string') {
    options = {
      char: options
    };
  }
  if (options.char == null) {
    options.char = ' ';
  }
  if (options.strip == null) {
    options.strip = false;
  }
  if (typeof text !== 'string') {
    text = text.toString();
  }
  textnocolors = null;
  pad = '';
  if (options.colors) {
    escapecolor = /\x1B\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)?[m|K]/g;
    textnocolors = text.replace(escapecolor, '');
  }
  padlength = options.fixed_width ? length - (textnocolors || text).length : length - wcwidth(textnocolors || text, options.wcwidth_options);
  if (padlength < 0) {
    if (options.strip) {
      if (invert) {
        return text.substr(length * -1);
      } else {
        return text.substr(0, length);
      }
    }
    return text;
  }
  pad += options.char.repeat(padlength);
  if (invert) {
    return pad + text;
  } else {
    return text + pad;
  }
};
