forked from chjj/blessed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget-pos.js
124 lines (102 loc) · 2.52 KB
/
widget-pos.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var blessed = require('../')
, assert = require('assert')
, screen;
screen = blessed.screen({
dump: __dirname + '/logs/pos.log'
});
// My terminal size at the time of writing these tests:
screen.program.cols = 154;
screen.program.rows = 19;
screen.alloc();
var main = blessed.box({
//width: '75%',
//height: '75%',
width: 115,
height: 14,
style: {
bg: 'yellow'
},
top: 2,
left: 2,
content: 'Welcome to my program'
});
screen.append(main);
var inner = blessed.box({
width: '50%',
height: '50%',
//width: 57,
//height: 7,
style: {
bg: 'blue'
},
top: 2,
left: 2,
content: 'Hello'
});
main.append(inner);
inner.setContent(inner.content + '\n' + JSON.stringify({
aleft: inner.aleft,
aright: inner.aright,
atop: inner.atop,
abottom: inner.abottom,
width: inner.width,
height: inner.height,
rleft: inner.rleft,
rright: inner.rright,
rtop: inner.rtop,
rbottom: inner.rbottom
}));
assert.equal(inner.width, 57);
assert.equal(inner.height, 7);
assert.equal(inner.aleft, 4);
assert.equal(inner.aright, 93);
assert.equal(inner.atop, 4);
assert.equal(inner.abottom, 8);
assert.equal(inner.rleft, 2);
assert.equal(inner.rright, 56);
assert.equal(inner.rtop, 2);
assert.equal(inner.rbottom, 5);
// Change left to half of the parent width.
inner.rleft = '50%';
assert.equal(inner.aleft, 59);
// Change left to half of the screen width.
inner.aleft = '50%';
assert.equal(inner.aleft, screen.width / 2 | 0);
// Test implied height/width.
reset(inner, {
top: 5,
bottom: 5,
left: 5,
right: 5
});
assert.equal(inner.width, 105);
assert.equal(inner.height, 4);
// Demonstrate the difference between `left: 5`, and `.aleft = 5` (relative vs. absolute):
inner.atop = inner.abottom = inner.aleft = inner.aright = 5;
assert.equal(inner.width, 144);
assert.equal(inner.height, 9);
// Test center keyword
reset(inner, {
width: '50%',
height: '50%',
left: 'center',
top: 'center'
});
assert.equal(inner.rleft, 29);
assert.equal(inner.rtop, 4);
// TODO: Start storing position.left, etc. as absolute?
screen.on('keypress', function(ch, key) {
if (key.name === 'escape' || key.name === 'q') {
return screen.destroy();
}
});
screen.render();
function reset(el, pos) {
pos = pos || {};
el.position.width = el.options.width = pos.width;
el.position.height = el.options.height = pos.height;
el.position.left = el.options.left = pos.left;
el.position.right = el.options.right = pos.right;
el.position.top = el.options.top = pos.top;
el.position.bottom = el.options.bottom = pos.bottom;
}