2013-07-18 22:56:05 +00:00
var blessed = require ( '../' )
2013-07-28 19:29:07 +00:00
, screen ;
screen = blessed . screen ( {
2015-01-30 04:08:23 +00:00
dump : _ _dirname + '/logs/widget.log' ,
2015-03-14 23:35:17 +00:00
title : 'widget test' ,
2015-03-20 06:49:26 +00:00
resizeTimeout : 300 ,
2015-04-07 01:40:33 +00:00
dockBorders : true ,
2015-03-20 06:49:26 +00:00
cursor : {
artificial : true ,
shape : 'line' ,
blink : true ,
color : null
2015-03-31 09:25:18 +00:00
} ,
2015-07-22 02:23:57 +00:00
debug : true ,
warnings : 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 = '' ;
2015-04-02 02:46:08 +00:00
require ( './tail' ) ( _ _dirname + '/logs/widget.log' ) . on ( 'line' , function ( line ) {
2015-04-24 08:37:00 +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 = '' ;
}
2015-04-24 08:37:00 +00:00
screen . render ( ) ;
2015-04-02 02:46:08 +00:00
} ) ;
2015-03-31 09:25:18 +00:00
2015-08-31 10:35:47 +00:00
screen . on ( 'event' , function ( event , el ) {
var type = ( el && el . type ) || Object . prototype . toString . call ( el ) . slice ( 8 , - 1 ) ;
screen . program . log ( 'emit("%s", {%s})' , event , type ) ;
} ) ;
2013-07-18 22:56:05 +00:00
screen . append ( blessed . text ( {
2013-06-06 09:03:25 +00:00
top : 0 ,
left : 2 ,
2013-06-13 09:14:14 +00:00
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 ,
2013-06-13 09:14:14 +00:00
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 ( {
2013-06-13 09:14:14 +00:00
align : 'center' ,
2013-06-06 10:39:44 +00:00
mouse : true ,
2015-02-26 23:37:01 +00:00
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' ,
2015-04-16 20:20:49 +00:00
tags : true ,
invertSelected : false ,
2013-06-01 07:06:04 +00:00
items : [
2013-06-03 03:29:26 +00:00
'one' ,
2015-04-16 20:20:49 +00:00
'{red-fg}two{/red-fg}' ,
2013-06-03 03:29:26 +00:00
'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 ) ;
2013-06-09 13:33:23 +00:00
list . select ( 0 ) ;
2013-06-06 09:03:25 +00:00
2015-02-13 09:18:20 +00:00
list . items . forEach ( function ( item ) {
item . setHover ( item . getText ( ) . trim ( ) ) ;
} ) ;
2015-05-07 00:19:56 +00:00
var item = list . items [ 1 ] ;
list . removeItem ( list . items [ 1 ] ) ;
list . insertItem ( 1 , item . getContent ( ) ) ;
2013-06-06 09:03:25 +00:00
list . on ( 'keypress' , function ( ch , key ) {
2013-06-09 13:33:23 +00:00
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 ;
2013-06-09 13:33:23 +00:00
} 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
} ) ;
2015-02-26 23:37:01 +00:00
list . on ( 'select' , function ( item , select ) {
2015-04-22 23:54:15 +00:00
list . setLabel ( ' ' + item . getText ( ) + ' ' ) ;
2015-02-26 23:37:01 +00:00
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
2013-06-09 13:33:23 +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
} ) ;
2015-04-27 07:14:02 +00:00
setTimeout ( function ( ) {
stext . width = 0 ;
screen . render ( ) ;
setTimeout ( function ( ) {
stext . width = '50%' ;
screen . render ( ) ;
setTimeout ( function ( ) {
stext . height = 0 ;
screen . render ( ) ;
setTimeout ( function ( ) {
stext . height = 6 ;
screen . render ( ) ;
setTimeout ( function ( ) {
stext . width = 0 ;
stext . height = 0 ;
screen . render ( ) ;
setTimeout ( function ( ) {
stext . width = '50%' ;
stext . height = 6 ;
screen . render ( ) ;
} , 1000 ) ;
} , 1000 ) ;
} , 1000 ) ;
} , 1000 ) ;
} , 1000 ) ;
} , 1000 ) ;
2013-06-06 09:03:25 +00:00
screen . append ( stext ) ;
stext . on ( 'keypress' , function ( ch , key ) {
2013-06-09 13:33:23 +00:00
if ( key . name === 'up' || key . name === 'k' ) {
2013-06-06 09:03:25 +00:00
stext . scroll ( - 1 ) ;
screen . render ( ) ;
return ;
2013-06-09 13:33:23 +00:00
} 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 ) {
2013-07-16 22:50:02 +00:00
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 ) ;
2015-04-26 09:34:17 +00:00
screen . key ( 'S-s' , function ( ) {
2015-04-26 09:40:39 +00:00
var rand = function ( min , max ) {
return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
} ;
var xi = rand ( 0 , screen . cols - ( stext . width - stext . iwidth ) ) ;
var xl = xi + stext . width - stext . iwidth ;
var yi = rand ( 0 , screen . rows - ( stext . height - stext . iheight ) ) ;
var yl = yi + stext . height - stext . iheight ;
2015-04-26 09:34:17 +00:00
stext . wrap = false ;
2015-04-26 09:40:39 +00:00
stext . setContent ( screen . screenshot ( xi , xl , yi , xl ) ) ;
2015-04-26 09:34:17 +00:00
screen . render ( ) ;
} ) ;
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 ( ) {
2013-06-09 20:21:58 +00:00
progress . toggle ( ) ;
2013-06-09 16:47:02 +00:00
screen . render ( ) ;
2013-06-09 20:21:58 +00:00
} , 2000 ) ;
2013-06-06 10:38:30 +00:00
2013-06-06 09:03:25 +00:00
( function fill ( ) {
2013-06-09 19:32:29 +00:00
if ( progress . filled === 100 ) {
progress . reset ( ) ;
}
2013-06-06 09:03:25 +00:00
progress . progress ( 2 ) ;
2015-05-02 01:20:16 +00:00
progress . atop -= 2 ;
2013-06-06 09:03:25 +00:00
screen . render ( ) ;
setTimeout ( fill , 300 ) ;
} ) ( ) ;