neo-blessed/test/widget.js

277 lines
5.0 KiB
JavaScript
Raw Normal View History

2013-07-04 04:15:09 +00:00
var blessed = require('blessed');
var screen = new blessed.Screen({
tput: true
});
2013-06-01 05:29:31 +00:00
2013-06-06 09:03:25 +00:00
screen.append(new blessed.Text({
top: 0,
left: 2,
width: '100%',
2013-06-16 09:21:57 +00:00
//bg: 'blue',
content: '{green-fg}Welcome{/green-fg} to my {red-fg,ul}program{/red-fg,ul}',
bg: '#0000ff',
// bg: blessed.colors.matchColor('#0000ff'),
2013-06-14 01:21:41 +00:00
tags: true,
align: 'center'
2013-06-06 09:03:25 +00:00
}));
screen.append(new blessed.Line({
orientation: 'horizontal',
top: 1,
left: 0,
right: 0
}));
2013-06-01 07:06:04 +00:00
/*
2013-06-01 05:29:31 +00:00
screen.append(new blessed.Box({
2013-06-01 07:06:04 +00:00
fg: 4,
bg: -1,
2013-06-01 05:29:31 +00:00
border: {
type: 'ascii',
2013-06-01 07:06:04 +00:00
fg: -1,
bg: -1
2013-06-01 05:29:31 +00:00
},
content: 'Hello world!',
width: '50%',
height: '50%',
top: 'center',
left: 'center'
}));
2013-06-01 07:06:04 +00:00
screen.children[0].append(new blessed.Box({
fg: 4,
bg: 3,
border: {
type: 'bg',
fg: 0,
bg: 1,
ch: '/'
},
content: 'Foobar',
width: '50%',
height: '50%',
top: 'center',
left: 'center'
}));
*/
2013-06-06 09:03:25 +00:00
var list = new blessed.List({
align: 'center',
2013-06-06 10:39:44 +00:00
mouse: true,
2013-06-13 07:16:32 +00:00
fg: 'blue',
bg: 'default',
2013-06-01 07:06:04 +00:00
border: {
type: 'ascii',
2013-06-13 07:16:32 +00:00
fg: 'default',
bg: 'default'
2013-06-01 07:06:04 +00:00
},
width: '50%',
height: '50%',
top: 'center',
left: 'center',
2013-06-13 07:16:32 +00:00
selectedBg: 'green',
2013-06-01 07:06:04 +00:00
items: [
2013-06-03 03:29:26 +00:00
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
2013-06-01 07:06:04 +00:00
]
2013-06-06 09:03:25 +00:00
});
2013-06-01 07:06:04 +00:00
2013-06-06 09:03:25 +00:00
screen.append(list);
list.select(0);
2013-06-06 09:03:25 +00:00
list.prepend(new blessed.Text({
2013-06-01 10:47:18 +00:00
left: 2,
content: ' My list '
}));
2013-06-06 09:03:25 +00:00
list.on('keypress', function(ch, key) {
if (key.name === 'up' || key.name === 'k') {
2013-06-06 09:03:25 +00:00
list.up();
2013-06-01 10:47:18 +00:00
screen.render();
return;
} else if (key.name === 'down' || key.name === 'j') {
2013-06-06 09:03:25 +00:00
list.down();
2013-06-01 10:47:18 +00:00
screen.render();
return;
}
2013-06-06 09:03:25 +00:00
});
var progress = new blessed.ProgressBar({
2013-06-13 07:16:32 +00:00
fg: 'blue',
bg: 'default',
barBg: 'default',
barFg: 'blue',
2013-06-06 09:03:25 +00:00
border: {
type: 'ascii',
2013-06-13 07:16:32 +00:00
fg: 'default',
bg: 'default'
2013-06-06 09:03:25 +00:00
},
ch: ':',
2013-06-12 09:18:47 +00:00
//orientation: 'vertical',
//height: 10,
//width: 3,
2013-06-06 09:03:25 +00:00
width: '50%',
height: 3,
right: 0,
bottom: 0,
filled: 50
});
screen.append(progress);
2013-06-06 12:57:50 +00:00
var lorem = 'Lorem ipsum \x1b[41mdolor sit amet, \nconsectetur adipisicing elit, \x1b[43msed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
2013-06-06 09:03:25 +00:00
2013-06-10 01:41:25 +00:00
var lorem = require('fs').readFileSync(__dirname + '/git.diff', 'utf8');
2013-06-06 14:24:03 +00:00
//lorem = lorem.replace(/\x1b[^m]*m/g, '');
2013-06-06 09:03:25 +00:00
var stext = new blessed.ScrollableText({
2013-06-14 00:02:02 +00:00
//padding: 1,
2013-06-06 10:39:44 +00:00
mouse: true,
2013-06-06 09:03:25 +00:00
content: lorem,
2013-06-13 07:16:32 +00:00
fg: 'blue',
bg: 'default',
2013-06-06 09:03:25 +00:00
border: {
type: 'ascii',
2013-06-13 07:16:32 +00:00
fg: 'default',
bg: 'default'
2013-06-06 09:03:25 +00:00
},
width: '50%',
2013-06-06 14:24:03 +00:00
//height: 4,
height: 6,
2013-06-06 09:03:25 +00:00
left: 0,
bottom: 0
});
screen.append(stext);
stext.on('keypress', function(ch, key) {
if (key.name === 'up' || key.name === 'k') {
2013-06-06 09:03:25 +00:00
stext.scroll(-1);
screen.render();
return;
} else if (key.name === 'down' || key.name === 'j') {
2013-06-06 09:03:25 +00:00
stext.scroll(1);
screen.render();
return;
}
});
screen.on('element focus', function(old, cur) {
if (old.border) old.style.border.fg = 'default';
if (cur.border) cur.style.border.fg = 'green';
2013-06-06 09:03:25 +00:00
screen.render();
});
2013-06-16 14:31:55 +00:00
/*
screen.on('element mouseover', function(el) {
el._bg = el.bg;
el.bg = 1;
screen.render();
});
screen.on('element mouseout', function(el) {
el.bg = el._bg;
screen.render();
});
*/
2013-06-09 18:14:42 +00:00
var input = new blessed.Textbox({
mouse: true,
2013-06-09 20:02:12 +00:00
label: ' My Input ',
2013-06-09 18:14:42 +00:00
content: '',
2013-06-13 07:16:32 +00:00
fg: 'blue',
bg: 'default',
barBg: 'default',
barFg: 'blue',
2013-06-09 18:14:42 +00:00
border: {
type: 'ascii',
2013-06-13 07:16:32 +00:00
fg: 'default',
bg: 'default'
2013-06-09 18:14:42 +00:00
},
width: '30%',
height: 3,
right: 0,
2013-06-09 20:02:12 +00:00
top: 2
2013-06-09 18:14:42 +00:00
});
screen.append(input);
2013-06-16 09:21:57 +00:00
var button = blessed.Button({
//content: 'Click me!',
content: 'Click\nme!',
shrink: true,
border: {
type: 'ascii'
},
fg: 'red',
bg: 'blue',
//height: 3,
right: 4,
//bottom: 6,
bottom: 2,
padding: 0
});
button.on('press', function() {
button.setContent('Clicked!');
});
screen.append(button);
2013-06-09 18:14:42 +00:00
screen.on('keypress', function(ch, key) {
2013-06-06 09:03:25 +00:00
if (key.name === 'tab') {
return key.shift
? screen.focusPrev()
: screen.focusNext();
}
2013-06-09 18:14:42 +00:00
if (key.name === 'i') {
2013-07-03 23:46:06 +00:00
return input.readInput(function(err, value) {
2013-06-09 20:02:12 +00:00
if (value) screen.children[0].setContent(value);
2013-06-09 18:14:42 +00:00
});
}
if (key.name === 'e') {
2013-07-03 23:46:06 +00:00
return input.readEditor(function(err, value) {
2013-06-09 20:02:12 +00:00
if (value) screen.children[0].setContent(value);
2013-06-09 18:14:42 +00:00
});
}
2013-06-01 05:29:31 +00:00
if (key.name === 'escape' || key.name === 'q') {
2013-06-01 07:06:04 +00:00
return process.exit(0);
2013-06-01 05:29:31 +00:00
}
});
2013-07-14 15:06:28 +00:00
screen.key('C-z', function() {
screen.sigtstp();
});
2013-06-06 09:03:25 +00:00
list.focus();
2013-06-01 05:29:31 +00:00
//screen.on('element click', function(el) {
// el.focus();
//});
2013-06-01 05:29:31 +00:00
screen.render();
2013-06-06 09:03:25 +00:00
2013-06-06 10:38:30 +00:00
setInterval(function() {
progress.toggle();
screen.render();
}, 2000);
2013-06-06 10:38:30 +00:00
2013-06-06 09:03:25 +00:00
(function fill() {
if (progress.filled === 100) {
progress.reset();
}
2013-06-06 09:03:25 +00:00
progress.progress(2);
screen.render();
setTimeout(fill, 300);
2013-06-11 14:09:57 +00:00
progress.top -= 2;
2013-06-06 09:03:25 +00:00
})();