-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegate.js
68 lines (59 loc) · 1.68 KB
/
delegate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
function Delegate(args) {
args = args || {};
this.exitCode = 0;
this.trumped = null;
this._cursor = args.cursor;
this.logUsage = args.logUsage;
this.command = args.command;
this.loggedUsage = false;
}
Delegate.prototype.isDone = function isDone() {
return this.exitCode !== 0 || this.trumped !== null;
};
Delegate.prototype.log = function log(message, cursor) {
console.log(message);
};
Delegate.prototype.error = function error(message) {
if (this.logUsage && !this.loggedUsage) {
this.logUsage(this.command, this);
this.loggedUsage = true;
}
console.error('\n' + message);
this.exitCode = 1;
};
Delegate.prototype.warn = function warn(message) {
console.warn(message);
};
Delegate.prototype.cursor = function markCursor(cursor, offset) {
// New usage allows the cursor to be provided to the delegate constructor.
if (this._cursor && offset === undefined) {
offset = cursor;
cursor = this._cursor;
}
var cursorIndex = cursor.index + (offset || 0);
var line = '';
var length = 0;
for (var index = 0; index < cursor.args.length; index++) {
if (index === cursorIndex) {
length = line.length;
}
line += cursor.args[index];
if (index !== cursor.args.length - 1) {
line += ' ';
}
}
if (index === cursorIndex) {
length = line.length;
}
console.log(line);
line = ' ';
while (line.length < length) {
line = line + line;
}
console.log(line.slice(0, length) + '^');
};
Delegate.prototype.end = function end() {
process.exit(this.exitCode);
};
module.exports = Delegate;