<commit_before>/*eslint no-console: "off"*/

var style = {
  gray: function(text) {
    return '\x1b[38;5;243m' + text + '\x1b[0m';
  },

  underline: function(text) {
    return '\x1b[0;4m' + text + '\x1b[24m';
  }

};

function stylizeError(error, maxErrorMsgLength, maxLineChars, consoleWidth) {
  var str = '  '; // indent 2 spaces so it looks pretty
  var padding = '    '; //padding of 4 spaces, will be used between line numbers, error msgs and rule names

  var line = error.line.toString();
  // add spaces until the line string is as long as our longest line string
  while (line.length < maxLineChars) {
    line += ' ';
  }

  // print the line number as gray
  // print the error message in default color and add at least 2 spaces after it for readability
  str += style.gray(line) + padding + error.message + padding;

  var extraSpaces = Math.min(maxErrorMsgLength - error.message.length, consoleWidth - str.length - error.rule.length);
  if (extraSpaces > 0) {
    str +=  ' '.repeat(extraSpaces);
  }

  // print the rule name in gray
  str += style.gray(error.rule);

  return str; // lastly, return our stylish-est string and pretend that this code was never written
}

function stylizeFilePath(filePath) {
  return style.underline(filePath);
}

function getMaxLengthOfField(result, field) {
  var length = 0;
  result.errors.forEach(function(error) {
    var errorStr = error[field].toString();
    if (errorStr.length > length) {
      length = errorStr.length;
    }
  });
  return length;
}


function printResults(results) {
  // If the console is tty, get its width and use it to ensure we don't try to write messages longer 
  // than the console width when possible
  var consoleWidth = Infinity;
  if (process.stdout.isTTY) {
    consoleWidth = process.stdout.columns;
  }

  results.forEach(function(result) {
    if (result.errors.length > 0) {
      var maxErrorMsgLength = getMaxLengthOfField(result, 'message');
      var maxLineChars = getMaxLengthOfField(result, 'line');
      console.error(stylizeFilePath(result.filePath));

      result.errors.forEach(function(error) {
        console.error(stylizeError(error, maxErrorMsgLength, maxLineChars, consoleWidth));
      });
      console.error('\n');
    }
  });<commit_msg>stylish improvements - vertically allign errors on a per file basis instead of doing it for all the files - when the linter detects that it's running in tty, don't take into account error messages that are longer than the console width, when calculating alignment rules These changes should make the error output more readable and pretty when the linter reports errors with really long messages (eg duplicate scenario names)<commit_after>/*eslint no-console: "off"*/

var style = {
  gray: function(text) {
    return '\x1b[38;5;243m' + text + '\x1b[0m';
  },

  underline: function(text) {
    return '\x1b[0;4m' + text + '\x1b[24m';
  }

};

function stylizeFilePath(filePath) {
  return style.underline(filePath);
}

function stylizeError(error, maxLineLength, maxMessageLength, addColors) {
  var indent = '  '; // indent 2 spaces so it looks pretty
  var padding = '    '; //padding of 4 spaces, will be used between line numbers, error msgs and rule names, for readability
  var errorLinePadded = error.line.toString().padEnd(maxLineLength);
  var errorLineStylized = addColors ? style.gray(errorLinePadded) : errorLinePadded;
  
  var errorRuleStylized = addColors ? style.gray(error.rule) : error.rule;
  return indent + errorLineStylized + padding + error.message.padEnd(maxMessageLength) + padding + errorRuleStylized;
}

function getMaxLineLength(result) {
  var length = 0;
  result.errors.forEach(function(error) {
    var errorStr = error.line.toString();
    if (errorStr.length > length) {
      length = errorStr.length;
    }
  });
  return length;
}

function getMaxMessageLength(result, maxLineLength, consoleWidth) {
  var length = 0;
  result.errors.forEach(function(error) {
    var errorStr = error.message.toString();
   
    // Get the length of the formatted error message when no extra padding is applied
    // If the formatted message is longer than the console width, we will ignore its length
    var expandedErrorStrLength = stylizeError(error, maxLineLength, 0, false).length;

    if (errorStr.length > length && expandedErrorStrLength < consoleWidth) {
      length = errorStr.length;
    }
  });

  return length;
}

function printResults(results) {
  // If the console is tty, get its width and use it to ensure we don't try to write messages longer 
  // than the console width when possible
  var consoleWidth = Infinity;
  if (process.stdout.isTTY) {
    consoleWidth = process.stdout.columns;
  }

  results.forEach(function(result) {
    if (result.errors.length > 0) {
      var maxLineLength = getMaxLineLength(result);
      var maxMessageLength = getMaxMessageLength(result, maxLineLength, consoleWidth);
      console.error(stylizeFilePath(result.filePath));

      result.errors.forEach(function(error) {
        console.error(stylizeError(error, maxLineLength, maxMessageLength, true));
      });
      console.error('\n');
    }
  });
{'fraction_non_alphanumeric': 0.10709426627793975, 'fraction_numerical': 0.00816326530612245, 'mean_word_length': 3.6570135746606334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5906641', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1517, 'n_words': 473}