forked from chjj/blessed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbox.js
77 lines (62 loc) · 1.57 KB
/
textbox.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
69
70
71
72
73
74
75
76
77
/**
* textbox.js - textbox element for blessed
* Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
* https://github.com/chjj/blessed
*/
/**
* Modules
*/
var Node = require('./node');
var Textarea = require('./textarea');
/**
* Textbox
*/
function Textbox(options) {
if (!(this instanceof Node)) {
return new Textbox(options);
}
options = options || {};
options.scrollable = false;
Textarea.call(this, options);
this.secret = options.secret;
this.censor = options.censor;
}
Textbox.prototype.__proto__ = Textarea.prototype;
Textbox.prototype.type = 'textbox';
Textbox.prototype.__olistener = Textbox.prototype._listener;
Textbox.prototype._listener = function(ch, key) {
if (key.name === 'enter') {
this._done(null, this.value);
return;
}
return this.__olistener(ch, key);
};
Textbox.prototype.setValue = function(value) {
var visible, val;
if (value == null) {
value = this.value;
}
if (this._value !== value) {
value = value.replace(/\n/g, '');
this.value = value;
this._value = value;
if (this.secret) {
this.setContent('');
} else if (this.censor) {
this.setContent(Array(this.value.length + 1).join('*'));
} else {
visible = -(this.width - this.iwidth - 1);
val = this.value.replace(/\t/g, this.screen.tabc);
this.setContent(val.slice(visible));
}
this._updateCursor();
}
};
Textbox.prototype.submit = function() {
if (!this.__listener) return;
return this.__listener('\r', { name: 'enter' });
};
/**
* Expose
*/
module.exports = Textbox;