<commit_before>'use strict';

var _ = require('lodash');
var util = require('./readline');
var readline = require('readline');
var cliWidth = require('cli-width');
var stripAnsi = require('strip-ansi');

var ScreenManager = module.exports = function (rl) {
  // These variables are keeping information to allow correct prompt re-rendering
  this.height = 0;
  this.prevCursor = 0;

  this.rl = rl;
};

ScreenManager.prototype.render = function (content, opt) {
  opt = _.extend({ cursor: 0 }, opt || {});
  var cursorPos = this.rl._getCursorPos();

  this.rl.output.unmute();
  this.clean(this.prevCursor);

  var lines = content.split(/\n/);

  var breakedLines = breakLines(lines);

  // Write message to screen and setPrompt to control backspace
  var prompt = lines[lines.length - 1].replace(this.rl.line, '');
  lines = _.flatten(breakedLines);
  this.height = lines.length;
  this.rl.setPrompt(prompt);
  this.rl.output.write(content);

  // Re-adjust cursor at the end of the current prompt
  var promptLineUpDiff = Math.floor(stripAnsi(prompt + this.rl.line).length / cliWidth()) - cursorPos.rows;
  util.up(this.rl, opt.cursor + promptLineUpDiff);

  // Reset cursor at the beginning of the line
  util.left(this.rl, stripAnsi(_.last(lines)).length);

  var rightPos = cursorPos.cols;
  if (cursorPos.rows === 0) {
    rightPos = Math.max(rightPos, stripAnsi(prompt).length);
  }
  util.right(this.rl, rightPos);

  this.rl.output.mute();

  var bottomSection = breakedLines.slice(breakedLines.length - opt.cursor - promptLineUpDiff);
  var cursorLineOffset = _.flatten(bottomSection).length;

  // Set up state for next re-rendering
  this.prevCursor = cursorLineOffset;
};

ScreenManager.prototype.clean = function (extraLines) {<commit_msg>Fix cursor appearing at the beginning of line on first automatic line return<commit_after>'use strict';

var _ = require('lodash');
var util = require('./readline');
var readline = require('readline');
var cliWidth = require('cli-width');
var stripAnsi = require('strip-ansi');

var ScreenManager = module.exports = function (rl) {
  // These variables are keeping information to allow correct prompt re-rendering
  this.height = 0;
  this.prevCursor = 0;

  this.rl = rl;
};

ScreenManager.prototype.render = function (content, opt) {
  opt = _.extend({ cursor: 0 }, opt || {});
  var cursorPos = this.rl._getCursorPos();

  this.rl.output.unmute();
  this.clean(this.prevCursor);

  var lines = content.split(/\n/);

  // Write message to screen and setPrompt to control backspace
  var promptLine = lines[lines.length - 1];
  var rawPromptLine = stripAnsi(promptLine);
  var prompt = promptLine.replace(this.rl.line, '');
  this.rl.setPrompt(prompt);

  // Manually insert an extra line if we're at the end of the line.
  // This prevent the cursor from appearing at the beginning of the
  // current line.
  if (rawPromptLine.length === cliWidth()) {
    lines.splice(lines.length, 0, ' ');
  }

  this.rl.output.write(lines.join('\n'));

  // Re-adjust cursor at the end of the current prompt
  var promptLineUpDiff = Math.floor(rawPromptLine.length / cliWidth()) - cursorPos.rows;
  util.up(this.rl, opt.cursor + promptLineUpDiff);

  // Reset cursor at the beginning of the line
  var breakedLines = breakLines(lines);
  var actualLines = _.flatten(breakedLines);
  util.left(this.rl, stripAnsi(_.last(actualLines)).length);

  var rightPos = cursorPos.cols;

  if (cursorPos.rows === 0) {
    rightPos = Math.max(rightPos, stripAnsi(prompt).length);
  }
  util.right(this.rl, rightPos);

  this.rl.output.mute();

  var bottomSection = breakedLines.slice(breakedLines.length - opt.cursor - promptLineUpDiff);
  var cursorLineOffset = _.flatten(bottomSection).length;

  // Set up state for next re-rendering
  this.prevCursor = cursorLineOffset;
  this.height = actualLines.length;
};

ScreenManager.prototype.clean = function (extraLines) {
{'fraction_non_alphanumeric': 0.13560625480645988, 'fraction_numerical': 0.0028197897974878237, 'mean_word_length': 4.015424164524422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5187753', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1222, 'n_words': 319}