neo-blessed/test/widget.js

285 lines
5.1 KiB
JavaScript
Raw Normal View History

2013-07-18 22:56:05 +00:00
var blessed = require('../')
2013-07-28 19:29:07 +00:00
, screen;
screen = blessed.screen({
dump: __dirname + '/logs/widget.log',
title: 'widget test',
resizeTimeout: 300,
dockBorders: true,
cursor: {
artificial: true,
shape: 'line',
blink: true,
color: null
},
debug: true
2013-07-28 19:29:07 +00:00
});
2013-07-04 04:15:09 +00:00
2015-04-04 09:23:43 +00:00
screen.debugLog.parseTags = true;
2015-04-04 06:43:24 +00:00
var logs = '';
require('./tail')(__dirname + '/logs/widget.log').on('line', function(line) {
2015-04-04 09:23:43 +00:00
if (!screen.debugLog.hidden) return;
2015-04-04 06:43:24 +00:00
logs += line + '\n';
});
2015-04-04 09:23:43 +00:00
screen.debugLog.on('show', function() {
2015-04-04 06:43:24 +00:00
if (logs) {
screen.debug(logs);
logs = '';
}
});
2013-07-18 22:56:05 +00:00
screen.append(blessed.text({
2013-06-06 09:03:25 +00:00
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}',
2015-04-04 06:43:24 +00:00
style: {
bg: '#0000ff'
},
2013-07-23 10:04:04 +00:00
// bg: blessed.colors.match('#0000ff'),
2013-06-14 01:21:41 +00:00
tags: true,
align: 'center'
2013-06-06 09:03:25 +00:00
}));
2013-07-18 22:56:05 +00:00
screen.append(blessed.line({
2013-06-06 09:03:25 +00:00
orientation: 'horizontal',
top: 1,
left: 0,
right: 0
}));
2013-07-18 22:56:05 +00:00
var list = blessed.list({
align: 'center',
2013-06-06 10:39:44 +00:00
mouse: true,
label: ' My list ',
2015-04-04 06:43:24 +00:00
border: 'line',
style: {
fg: 'blue',
bg: 'default',
border: {
fg: 'default',
bg: 'default'
},
selected: {
bg: 'green'
}
2013-06-01 07:06:04 +00:00
},
width: '50%',
height: '50%',
top: 'center',
left: 'center',
items: [
2013-06-03 03:29:26 +00:00
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
2013-07-19 06:57:17 +00:00
],
scrollbar: {
ch: ' ',
2013-07-27 11:58:50 +00:00
track: {
bg: 'yellow'
},
style: {
inverse: true
}
2013-07-19 06:57:17 +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.items.forEach(function(item) {
item.setHover(item.getText().trim());
});
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
});
list.on('select', function(item, select) {
list.setLabel(' ' + item.getText() + ' ');
screen.render();
});
2013-07-18 22:56:05 +00:00
var progress = blessed.progressbar({
2015-04-04 06:43:24 +00:00
border: 'line',
style: {
fg: 'blue',
bg: 'default',
bar: {
bg: 'default',
fg: 'blue'
},
border: {
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-07-18 22:56:05 +00:00
var stext = 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,
2015-04-04 06:43:24 +00:00
border: 'line',
style: {
fg: 'blue',
bg: 'black',
border: {
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,
2013-07-21 10:31:24 +00:00
bottom: 0,
scrollbar: {
inverse: true
}
2013-06-06 09:03:25 +00:00
});
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;
}
});
2013-08-01 00:52:25 +00:00
screen.on('element focus', function(cur, old) {
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-07-18 22:56:05 +00:00
var input = blessed.textbox({
2013-06-09 20:02:12 +00:00
label: ' My Input ',
2013-06-09 18:14:42 +00:00
content: '',
2015-04-04 06:43:24 +00:00
border: 'line',
style: {
fg: 'blue',
bg: 'default',
bar: {
bg: 'default',
fg: 'blue'
},
border: {
fg: 'default',
bg: 'default'
}
2013-06-09 18:14:42 +00:00
},
width: '30%',
height: 3,
right: 0,
2013-07-29 20:49:25 +00:00
top: 2,
keys: true,
vi: true,
mouse: true
//inputOnFocus: true
2013-06-09 18:14:42 +00:00
});
2013-07-29 20:49:25 +00:00
input.on('submit', function(value) {
if (value) screen.children[0].setContent(value);
2013-07-19 03:49:06 +00:00
input.clearInput();
2013-07-29 20:49:25 +00:00
screen.render();
2013-07-19 03:49:06 +00:00
});
2013-06-09 18:14:42 +00:00
screen.append(input);
2013-08-01 00:21:10 +00:00
var button = blessed.button({
2013-06-16 09:21:57 +00:00
//content: 'Click me!',
content: 'Click\nme!',
shrink: true,
2013-08-01 00:21:10 +00:00
mouse: true,
2015-04-04 06:43:24 +00:00
border: 'line',
style: {
fg: 'red',
bg: 'blue'
2013-06-16 09:21:57 +00:00
},
//height: 3,
right: 4,
//bottom: 6,
bottom: 2,
padding: 0
});
button.on('press', function() {
button.setContent('Clicked!');
2013-07-29 20:49:25 +00:00
screen.render();
2013-06-16 09:21:57 +00:00
});
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
2013-07-29 20:49:25 +00:00
? screen.focusPrevious()
2013-06-06 09:03:25 +00:00
: screen.focusNext();
}
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.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);
progress.atop -= 2;
2013-06-06 09:03:25 +00:00
})();