Skip to content

Commit

Permalink
Merge pull request #322 from yibn2008/fix-mb-bug
Browse files Browse the repository at this point in the history
fix incorrect cursor position
  • Loading branch information
SBoudrias committed Jan 18, 2016
2 parents d7ffce5 + b653b63 commit 9d70b0e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/utils/screen-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _ = require('lodash');
var util = require('./readline');
var cliWidth = require('cli-width');
var stripAnsi = require('strip-ansi');
var stringWidth = require('string-width');

// Prevent crashes on environments where the width can't be properly detected
cliWidth.defaultWidth = 80;
Expand All @@ -11,10 +12,6 @@ function height(content) {
return content.split('\n').length;
}

function width(content) {
return stripAnsi(content).length;
}

function lastLine(content) {
return _.last(content.split('\n'));
}
Expand All @@ -35,8 +32,6 @@ var ScreenManager = module.exports = function (rl) {
};

ScreenManager.prototype.render = function (content, bottomContent) {
var cursorPos = this.rl._getCursorPos();

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

Expand All @@ -56,6 +51,9 @@ ScreenManager.prototype.render = function (content, bottomContent) {
}
this.rl.setPrompt(prompt);

// setPrompt will change cursor position, now we can get correct value
var cursorPos = this.rl._getCursorPos();

content = forceLineReturn(content);
if (bottomContent) {
bottomContent = forceLineReturn(bottomContent);
Expand All @@ -82,14 +80,10 @@ ScreenManager.prototype.render = function (content, bottomContent) {
}

// Reset cursor at the beginning of the line
util.left(this.rl, width(lastLine(fullContent)));
util.left(this.rl, stringWidth(lastLine(fullContent)));

// Adjust cursor on the right
var rightPos = cursorPos.cols;
if (cursorPos.rows === 0) {
rightPos = Math.max(rightPos, width(lastLine(content)));
}
util.right(this.rl, rightPos);
util.right(this.rl, cursorPos.cols);

/**
* Set up state for next re-rendering
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"readline2": "^1.0.1",
"run-async": "^0.1.0",
"rx-lite": "^3.1.2",
"string-width": "^1.0.1",
"strip-ansi": "^3.0.0",
"through": "^2.3.6"
},
Expand Down

0 comments on commit 9d70b0e

Please sign in to comment.