terminal-kit
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -29,3 +29,9 @@ /* | ||
var term = {} ; | ||
// Load modules | ||
var tty = require( 'tty' ) ; | ||
var chainable = {} ; | ||
var term = Object.create( chainable ) ; | ||
module.exports = term ; | ||
@@ -53,74 +59,109 @@ | ||
var escapeAttr = {} ; | ||
var escapeCtrl = {} ; | ||
/* Control sequence */ | ||
term.ctrl = {} ; | ||
// Init/Reset | ||
escapeCtrl.RESET = "\x1b[0m" ; | ||
escapeCtrl.FULL_RESET = "\x1bc" ; | ||
term.ctrl.reset = "\x1b[0m" ; | ||
term.ctrl.fullReset = "\x1bc" ; | ||
// Attributes | ||
escapeAttr.BOLD = "\x1b[1m" ; | ||
escapeAttr.NO_BOLD = "\x1b[21m" ; | ||
escapeAttr.DIM = "\x1b[2m" ; // dim: darker | ||
escapeAttr.NO_DIM = "\x1b[22m" ; // it remove bold/bright too | ||
escapeAttr.UNDERLINE = "\x1b[4m" ; | ||
escapeAttr.NO_UNDERLINE = "\x1b[24m" ; | ||
escapeAttr.INVERSE = "\x1b[7m" ; // inverse background and foreground | ||
escapeAttr.NO_INVERSE = "\x1b[27m" ; | ||
escapeAttr.HIDDEN = "\x1b[8m" ; // invisible, but can be copy/paste'd | ||
escapeAttr.NO_HIDDEN = "\x1b[28m" ; | ||
escapeAttr.STROKE = "\x1b[9m" ; | ||
escapeAttr.NO_STROKE = "\x1b[29m" ; | ||
escapeAttr.NO_INTENSITY = "\x1b[22m" ; // remove bold, bright & dim | ||
// Remove colors | ||
term.ctrl.noColor = "\x1b[39m" ; // back to the default color, most of time it is the same than .white | ||
term.ctrl.noBgColor = "\x1b[49m" ; // back to the default color, most of time it is the same than .bgBlack | ||
// Cursors | ||
term.ctrl.moveToLowerLeft = "\x1bF" ; | ||
term.ctrl.saveCursor = "\x1b7" ; | ||
term.ctrl.restoreCursor = "\x1b8" ; | ||
// Emit a beep | ||
term.ctrl.beep = "\x07" ; | ||
/* Attributes sequence */ | ||
term.attr = {} ; | ||
term.attr.bold = { open: "\x1b[1m" , close: "\x1b[21m" } ; | ||
term.attr.dim = { open: "\x1b[2m" , close: "\x1b[22m" } ; // dim: darker, 'close' remove removes also bold/bright | ||
term.attr.italic = { open: "\x1b[3m" , close: "\x1b[23m" } ; | ||
term.attr.underline = { open: "\x1b[4m" , close: "\x1b[24m" } ; | ||
term.attr.blink = { open: "\x1b[5m" , close: "\x1b[25m" } ; | ||
term.attr.inverse = { open: "\x1b[7m" , close: "\x1b[27m" } ; | ||
term.attr.hidden = { open: "\x1b[8m" , close: "\x1b[28m" } ; // invisible, but can be copy/paste'd | ||
term.attr.strike = { open: "\x1b[9m" , close: "\x1b[29m" } ; | ||
// Foreground color | ||
escapeAttr.BLACK = "\x1b[30m" ; | ||
escapeAttr.RED = "\x1b[31m" ; | ||
escapeAttr.GREEN = "\x1b[32m" ; | ||
escapeAttr.YELLOW = "\x1b[33m" ; | ||
escapeAttr.BLUE = "\x1b[34m" ; | ||
escapeAttr.MAGENTA = "\x1b[35m" ; | ||
escapeAttr.CYAN = "\x1b[36m" ; | ||
escapeAttr.WHITE = "\x1b[37m" ; | ||
escapeAttr.BRIGHT_BLACK = "\x1b[90m" ; | ||
escapeAttr.BRIGHT_RED = "\x1b[91m" ; | ||
escapeAttr.BRIGHT_GREEN = "\x1b[92m" ; | ||
escapeAttr.BRIGHT_YELLOW = "\x1b[93m" ; | ||
escapeAttr.BRIGHT_BLUE = "\x1b[94m" ; | ||
escapeAttr.BRIGHT_MAGENTA = "\x1b[95m" ; | ||
escapeAttr.BRIGHT_CYAN = "\x1b[96m" ; | ||
escapeAttr.BRIGHT_WHITE = "\x1b[97m" ; | ||
escapeAttr.NO_COLOR = "\x1b[39m" ; // back to the default color, most of time it is the same than WHITE | ||
term.attr.black = { open: "\x1b[30m" , close: term.ctrl.noColor } ; | ||
term.attr.red = { open: "\x1b[31m" , close: term.ctrl.noColor } ; | ||
term.attr.green = { open: "\x1b[32m" , close: term.ctrl.noColor } ; | ||
term.attr.yellow = { open: "\x1b[33m" , close: term.ctrl.noColor } ; | ||
term.attr.blue = { open: "\x1b[34m" , close: term.ctrl.noColor } ; | ||
term.attr.magenta = { open: "\x1b[35m" , close: term.ctrl.noColor } ; | ||
term.attr.cyan = { open: "\x1b[36m" , close: term.ctrl.noColor } ; | ||
term.attr.white = { open: "\x1b[37m" , close: term.ctrl.noColor } ; | ||
term.attr.brightBlack = { open: "\x1b[90m" , close: term.ctrl.noColor } ; | ||
term.attr.brightRed = { open: "\x1b[91m" , close: term.ctrl.noColor } ; | ||
term.attr.brightGreen = { open: "\x1b[92m" , close: term.ctrl.noColor } ; | ||
term.attr.brightYellow = { open: "\x1b[93m" , close: term.ctrl.noColor } ; | ||
term.attr.brightBlue = { open: "\x1b[94m" , close: term.ctrl.noColor } ; | ||
term.attr.brightMagenta = { open: "\x1b[95m" , close: term.ctrl.noColor } ; | ||
term.attr.brightCyan = { open: "\x1b[96m" , close: term.ctrl.noColor } ; | ||
term.attr.brightWhite = { open: "\x1b[97m" , close: term.ctrl.noColor } ; | ||
// Background color | ||
escapeAttr.BG_BLACK = "\x1b[40m" ; | ||
escapeAttr.BG_RED = "\x1b[41m" ; | ||
escapeAttr.BG_GREEN = "\x1b[42m" ; | ||
escapeAttr.BG_YELLOW = "\x1b[43m" ; | ||
escapeAttr.BG_BLUE = "\x1b[44m" ; | ||
escapeAttr.BG_MAGENTA = "\x1b[45m" ; | ||
escapeAttr.BG_CYAN = "\x1b[46m" ; | ||
escapeAttr.BG_WHITE = "\x1b[47m" ; | ||
escapeAttr.BG_BRIGHT_BLACK = "\x1b[100m" ; | ||
escapeAttr.BG_BRIGHT_RED = "\x1b[101m" ; | ||
escapeAttr.BG_BRIGHT_GREEN = "\x1b[102m" ; | ||
escapeAttr.BG_BRIGHT_YELLOW = "\x1b[103m" ; | ||
escapeAttr.BG_BRIGHT_BLUE = "\x1b[104m" ; | ||
escapeAttr.BG_BRIGHT_MAGENTA = "\x1b[105m" ; | ||
escapeAttr.BG_BRIGHT_CYAN = "\x1b[106m" ; | ||
escapeAttr.BG_BRIGHT_WHITE = "\x1b[107m" ; | ||
escapeAttr.NO_BG_COLOR = "\x1b[49m" ; // back to the default color, most of time it is the same than BG_BLACK | ||
term.attr.bgBlack = { open: "\x1b[40m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgRed = { open: "\x1b[41m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgGreen = { open: "\x1b[42m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgYellow = { open: "\x1b[43m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBlue = { open: "\x1b[44m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgMagenta = { open: "\x1b[45m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgCyan = { open: "\x1b[46m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgWhite = { open: "\x1b[47m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightBlack = { open: "\x1b[100m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightRed = { open: "\x1b[101m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightGreen = { open: "\x1b[102m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightYellow = { open: "\x1b[103m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightBlue = { open: "\x1b[104m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightMagenta = { open: "\x1b[105m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightCyan = { open: "\x1b[106m" , close: term.ctrl.noBgColor } ; | ||
term.attr.bgBrightWhite = { open: "\x1b[107m" , close: term.ctrl.noBgColor } ; | ||
// Shortcuts | ||
escapeAttr.BOLD_BLACK = "\x1b[1;30m" ; | ||
escapeAttr.BOLD_RED = "\x1b[1;31m" ; | ||
escapeAttr.BOLD_GREEN = "\x1b[1;32m" ; | ||
escapeAttr.BOLD_YELLOW = "\x1b[1;33m" ; | ||
escapeAttr.BOLD_BLUE = "\x1b[1;34m" ; | ||
escapeAttr.BOLD_MAGENTA = "\x1b[1;35m" ; | ||
escapeAttr.BOLD_CYAN = "\x1b[1;36m" ; | ||
escapeAttr.BOLD_WHITE = "\x1b[1;37m" ; | ||
/* Input / Output */ | ||
term.io = {} ; | ||
// The terminal will send input into process.STDIN | ||
// Terminal will send the cursor coordinate | ||
term.io.cursor = { open: "\x1b[?6n" , close: '' } ; | ||
// Terminal will send button event and mouse position | ||
term.io.mouseClick = { open: "\x1b[?1000h" , close: "\x1b[?1000l" } ; | ||
// Terminal will send position of the column hilighted | ||
term.io.mouseHilight = { open: "\x1b[?1001h" , close: "\x1b[?1001l" } ; | ||
// ? | ||
term.io.mouseCell = { open: "\x1b[?1002h" , close: "\x1b[?1002l" } ; | ||
// Terminal will send all motion | ||
term.io.mouseMotion = { open: "\x1b[?1003h" , close: "\x1b[?1003l" } ; | ||
// Dev tests for new escape sequences discoveries | ||
escapeCtrl.TEST = "\x1b[1;37m" ; | ||
term.ctrl.test = "\x1b[0 q" ; | ||
@@ -131,42 +172,102 @@ | ||
/* Build utilities */ | ||
/* Apply */ | ||
// Turn ALLCAPS to camelCase | ||
function allCapsToCamelCase( str ) | ||
function applyCtrl( ctrl , string ) { return term.ctrl[ ctrl ] + ( string || '' ) ; } | ||
function applyAttributes( attributes , string ) | ||
{ | ||
return str.replace( /(_([A-Z]))?([A-Z]+)/g , function( match , $1 , $2 , $3 ) { | ||
return ( $2 || '' ) + $3.toLowerCase() ; | ||
} ) ; | ||
var index , open = '' , close = '' ; | ||
console.log( 'Attributes:' , attributes ) ; | ||
for ( index in attributes ) | ||
{ | ||
console.log( 'key:' , attributes[ index ] ) ; | ||
open = open + term.attr[ attributes[ index ] ].open ; | ||
close = term.attr[ attributes[ index ] ].close + close ; | ||
} | ||
return open + ( string || '' ) + close ; | ||
} | ||
function applyEscapeAndReset( seq , string ) { return seq + ( string || '' ) + escapeCtrl.RESET ; } | ||
function applyEscape( seq , string ) { return seq + ( string || '' ) ; } | ||
// Create methods for the 'chainable' prototype | ||
// Create a function for each constant | ||
Object.keys( term.attr ).forEach( function( key ) { | ||
Object.defineProperty( chainable , key , { | ||
configurable: true , | ||
get: function () { | ||
var chain ; | ||
if ( this.chain ) { chain = this.chain.slice() ; chain.push( key ) ; } | ||
else { chain = [ key ] ; } | ||
var fn = applyAttributes.bind( this , chain ) ; | ||
fn.__proto__ = chainable ; | ||
fn.chain = chain ; | ||
// Replace the getter by the newly created function, to speed up further call | ||
Object.defineProperty( this , key , { value: fn } ) ; | ||
//console.log( ' Getter called:' , key ) ; | ||
return fn ; | ||
} | ||
} ) ; | ||
} ) ; | ||
var key , fn ; | ||
for ( key in escapeAttr ) | ||
Object.keys( term.ctrl ).forEach( function( key ) { | ||
term[ key ] = applyCtrl.bind( term , key ) ; | ||
} ) ; | ||
// Code from 'cli-color' | ||
Object.defineProperties( term , { | ||
width: { get: process.stdout.getWindowSize ? | ||
function () { return process.stdout.getWindowSize()[ 0 ] ; } : | ||
function () { return tty.getWindowSize ? tty.getWindowSize()[ 1 ] : 0 ; } | ||
} , | ||
height: { get: process.stdout.getWindowSize ? | ||
function () { return process.stdout.getWindowSize()[ 1 ] ; } : | ||
function () { return tty.getWindowSize ? tty.getWindowSize()[ 0 ] : 0 ; } | ||
} | ||
} ) ; | ||
function positiveOrZero( n ) { return isNaN( n ) ? 0 : Math.max( Math.floor( n ) , 0 ) ; } | ||
function positive( n ) { return isNaN( n ) ? 1 : Math.max( Math.floor( n ) , 1 ) ; } | ||
function floor( n ) { return isNaN( n ) ? 0 : Math.floor( n ) ; } | ||
term.up = function up( n ) { return '\x1b[' + positiveOrZero( n ) + 'A' ; } ; | ||
term.down = function down( n ) { return '\x1b[' + positiveOrZero( n ) + 'B' ; } ; | ||
term.right = function right( n ) { return '\x1b[' + positiveOrZero( n ) + 'C' ; } ; | ||
term.left = function left( n ) { return '\x1b[' + positiveOrZero( n ) + 'D' ; } ; | ||
term.move = function move( x , y ) | ||
{ | ||
term[ key ] = escapeAttr[ key ] ; | ||
fn = allCapsToCamelCase( key ) ; | ||
term[ fn ] = applyEscapeAndReset.bind( term , escapeAttr[ key ] ) ; | ||
x = floor( x ) ; | ||
y = floor( y ) ; | ||
return ( ( x >= 0 ) ? term.right( x ) : term.left( -x ) ) + | ||
( ( y >= 0 ) ? term.down( y ) : term.up( -y ) ) ; | ||
} | ||
for ( key in escapeCtrl ) | ||
term.moveTo = function moveTo( x , y ) | ||
{ | ||
term[ key ] = escapeCtrl[ key ] ; | ||
fn = allCapsToCamelCase( key ) ; | ||
term[ fn ] = applyEscape.bind( term , escapeCtrl[ key ] ) ; | ||
x = positive( x ) ; | ||
y = positive( y ) ; | ||
return '\x1b[' + y + ';' + x + 'H'; | ||
} | ||
// Set the title of the window | ||
term.windowTitle = function windowTitle( title ) { return '\x1b]0;' + title + '\x1b\\' ; } | ||
// The following is PHP code from CSK PHP lib, not backported ATM. | ||
@@ -179,6 +280,2 @@ | ||
// Reset | ||
static function reset() { echo self::RESET ; } | ||
static function full_reset() { echo self::FULL_RESET ; } | ||
// Fixe la couleur par un index de 256 couleurs | ||
@@ -221,8 +318,3 @@ static function set_color_index( $c ) { echo "\x1b[38;5;{$c}m" ; } | ||
static function set_cursor_yx( $y , $x ) { $y = intval( $y ) ; $x = intval( $x ) ; echo "\x1b[{$y};{$x}f" ; } | ||
static function save_cursor() { echo "\x1b7" ; } | ||
static function restore_cursor() { echo "\x1b8" ; } | ||
// Change le titre du terminal | ||
static function set_window_title( $title ) { echo "\x1b]0;{$title}\x1b\\" ; } | ||
// bugs: | ||
@@ -229,0 +321,0 @@ // il faudrait ré-injecter dans le flux les caractères hors contrôle |
{ | ||
"name": "terminal-kit", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Terminal utilities.", | ||
@@ -5,0 +5,0 @@ "main": "lib/terminal.js", |
@@ -33,2 +33,3 @@ /* | ||
var expect = require( 'expect.js' ) ; | ||
var print = process.stdout.write.bind( process.stdout ) ; | ||
@@ -53,6 +54,20 @@ | ||
it( "should" , function() { | ||
console.log( term.BLUE + 'Toto' ) ; | ||
console.log( 'Tata' ) ; | ||
console.log( term.red( 'Titi' ) + ' Tete' ) ; | ||
it( "should" , function( done ) { | ||
print( term.attr.blue.open + 'Toto' ) ; | ||
print( 'Tata' ) ; | ||
print( term.red( 'Titi' ) + ' Tete' ) ; | ||
print( term.red( 'Titi' ) + ' Tete' ) ; | ||
print( term.bold.underline.red( 'Tutu' ) ) ; | ||
print( term.green.strike( 'Tyty' ) ) ; | ||
print( term.magenta.italic( 'Tztz' ) ) ; | ||
print( term.blink( 'Txtx' ) ) ; | ||
print( term.attr.blue.open + 'Toto' + term.reset() + 'tata' ) ; | ||
print( term.windowTitle( 'wonderful title' ) ) ; | ||
print( term.test() ) ; | ||
print( term.moveToLowerLeft() + 'lowerleft!' ) ; | ||
setTimeout( done , 1500 ) ; | ||
} ) ; | ||
@@ -59,0 +74,0 @@ } ) ; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21873
361