From b8e0fafaeb1b8d850a95392b5f6589f547a53cf6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 18 Jul 2013 17:56:05 -0500 Subject: [PATCH] misc. options. refactor tests/bin. --- README.md | 6 +-- bin/tput.js | 3 +- example/index.js | 2 +- example/ping | 32 ++++++++-------- lib/program.js | 8 ++-- lib/tput.js | 5 +++ lib/widget.js | 69 ++++++++++++++++++++++++++++++----- test/insert.js | 10 ++--- test/tput.js | 6 +-- test/widget-csr.js | 2 +- test/widget-file.js | 2 +- test/widget-form.js | 2 +- test/widget-insert.js | 2 +- test/widget-listbar.js | 4 +- test/widget-pos.js | 16 +++----- test/widget-shrink-padding.js | 2 +- test/widget-textarea.js | 6 +-- test/widget.js | 25 ++++++------- 18 files changed, 122 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 72dd6d1..9e28343 100644 --- a/README.md +++ b/README.md @@ -887,8 +887,8 @@ This will actually parse the xterm terminfo and compile every string capability to a javascript function: ``` js -var Tput = require('blessed').Tput - , tput = Tput('xterm'); +var blessed = require('blessed') + , tput = blessed.tput('xterm-256color'); console.log(tput.setaf(4) + 'hello' + tput.sgr0()); ``` @@ -905,7 +905,7 @@ The main functionality is exposed in the main `blessed` module: ``` js var blessed = require('blessed') - , program = blessed(); + , program = blessed.program(); program.key('q', function(ch, key) { program.clear(); diff --git a/bin/tput.js b/bin/tput.js index 002795b..b06ee8a 100755 --- a/bin/tput.js +++ b/bin/tput.js @@ -1,6 +1,7 @@ #!/usr/bin/env node -var tput = require('../lib/tput')(process.env.TERM || 'xterm') +var blessed = require('../') + , tput = blessed.tput() , argv = process.argv.slice(2) , cmd = argv.shift(); diff --git a/example/index.js b/example/index.js index 2d54dce..d0cf1bb 100644 --- a/example/index.js +++ b/example/index.js @@ -5,7 +5,7 @@ */ var blessed = require('blessed') - , program = blessed(); + , program = blessed.program(); process.title = 'blessed'; diff --git a/example/ping b/example/ping index d29ae63..55eef29 100755 --- a/example/ping +++ b/example/ping @@ -24,7 +24,6 @@ if (/^(-h|--help|-\?)$/.test(process.argv[2])) { } var blessed = require('blessed') - , program = blessed() , nssocket; try { @@ -40,18 +39,16 @@ var server * Screen Layout */ -var screen = new blessed.Screen({ - program: program -}); +var screen = blessed.screen(); -var table = new blessed.Box({ +var table = blessed.box({ left: 0, top: 0, width: screen.width, height: screen.height }); -var ball = new blessed.Box({ +var ball = blessed.box({ width: 1, height: 1, bg: 'white', @@ -59,7 +56,7 @@ var ball = new blessed.Box({ left: 0 }); -var lpaddle = new blessed.Box({ +var lpaddle = blessed.box({ width: 1, height: 3, bg: 'yellow', @@ -67,7 +64,7 @@ var lpaddle = new blessed.Box({ left: 0 }); -var rpaddle = new blessed.Box({ +var rpaddle = blessed.box({ width: 1, height: 3, bg: 'yellow', @@ -75,24 +72,25 @@ var rpaddle = new blessed.Box({ right: 0 }); -var score = new blessed.Box({ +var score = blessed.box({ top: 0, left: 4, height: 3, - //width: 26, + width: 'shrink', border: { - type: 'ascii' + type: 'line' }, - shrink: true, //align: 'center', - bold: true, + style: { + bold: true + }, tags: true }); score.lwins = 0; score.rwins = 0; -var net = new blessed.Box({ +var net = blessed.box({ width: 1, height: '100%', bg: 'yellow', @@ -100,17 +98,17 @@ var net = new blessed.Box({ left: 'center' }); -var message = new blessed.Box({ +var message = blessed.box({ width: '50%', height: 3, border: { - type: 'ascii' + type: 'line' }, top: 'center', left: 'center' }); -var text = new blessed.Box({ +var text = blessed.box({ top: 'center', left: 1, right: 1, diff --git a/lib/program.js b/lib/program.js index 2067e9e..ac82f16 100644 --- a/lib/program.js +++ b/lib/program.js @@ -18,12 +18,12 @@ var EventEmitter = require('events').EventEmitter */ function Program(options) { + var self = this; + if (!(this instanceof Program)) { return new Program(options); } - var self = this; - EventEmitter.call(this); if (!options || options.__proto__ !== Object.prototype) { @@ -3319,8 +3319,8 @@ Program.prototype.sigtstp = function(callback) { */ exports = Program; -exports.Program = Program; -exports.Tput = Tput; +exports.Program = exports.program = Program; +exports.Tput = exports.tput = Tput; exports.widget = require('./widget'); Object.keys(exports.widget).forEach(function(name) { diff --git a/lib/tput.js b/lib/tput.js index 34d99a8..909769d 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -34,6 +34,7 @@ function Tput(options) { return new Tput(options); } + options = options || {}; if (typeof options === 'string') { options = { term: options }; } @@ -72,6 +73,10 @@ function Tput(options) { this.detectFeatures(); } +Tput.prototype.term = function(is) { + return this.terminal.indexOf(is) === 0; +}; + /** * Fallback */ diff --git a/lib/widget.js b/lib/widget.js index d6658e2..978cd69 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -25,7 +25,8 @@ function Node(options) { EventEmitter.call(this); - this.options = options || {}; + options = options || {}; + this.options = options; this.screen = this.screen || Screen.global || (function(){throw new Error('No active screen.')})(); @@ -146,7 +147,7 @@ Node.prototype.remove = function(element) { }; Node.prototype.detach = function() { - return this.parent.remove(this); + if (this.parent) this.parent.remove(this); }; Node.prototype.emitDescendants = function() { @@ -237,11 +238,11 @@ function Screen(options) { return new Screen(options); } - if (options && options.rsety && options.listen) { + options = options || {}; + if (options.rsety && options.listen) { options = { program: options }; } - options = options || {}; options.program = options.program || require('./program').global || new (require('./program'))(options); @@ -1365,6 +1366,8 @@ function Element(options) { return new Element(options); } + options = options || {}; + Node.call(this, options); this.name = options.name; @@ -2112,6 +2115,7 @@ function Box(options) { if (!(this instanceof Box)) { return new Box(options); } + options = options || {}; Element.call(this, options); } @@ -2668,6 +2672,7 @@ function Text(options) { if (!(this instanceof Text)) { return new Text(options); } + options = options || {}; options.shrink = true; Box.call(this, options); } @@ -2685,6 +2690,8 @@ function Line(options) { return new Line(options); } + options = options || {}; + var orientation = options.orientation || 'vertical'; delete options.orientation; @@ -2733,6 +2740,8 @@ function ScrollableBox(options) { return new ScrollableBox(options); } + options = options || {}; + Box.call(this, options); this.scrollable = true; @@ -2834,6 +2843,8 @@ function List(options) { return new List(options); } + options = options || {}; + ScrollableBox.call(this, options); this.value = ''; @@ -3134,6 +3145,8 @@ function ScrollableText(options) { return new ScrollableText(options); } + options = options || {}; + options.alwaysScroll = true; ScrollableBox.call(this, options); @@ -3277,6 +3290,8 @@ function Form(options) { return new Form(options); } + options = options || {}; + Box.call(this, options); if (options.keys) { @@ -3475,6 +3490,7 @@ function Input(options) { if (!(this instanceof Input)) { return new Input(options); } + options = options || {}; Box.call(this, options); } @@ -3493,6 +3509,8 @@ function Textbox(options) { return new Textbox(options); } + options = options || {}; + Input.call(this, options); this.screen._listenKeys(this); @@ -3590,6 +3608,11 @@ Textbox.prototype._listener = function(ch, key) { // Tabs only work with textareas. if (ch === '\t') ch = ' '; this.value += ch; + if (this.content.indexOf('defined') !== -1) { + this.program.normalBuffer(); + console.log(this.value); + process.exit(0); + } if (this.secret) return; if (this.value.length < width) { this.screen.program.cuf(); @@ -3659,6 +3682,8 @@ function Textarea(options) { return new Textarea(options); } + options = options || {}; + ScrollableText.call(this, options); this.screen._listenKeys(this); @@ -3851,6 +3876,8 @@ function Button(options) { return new Button(options); } + options = options || {}; + if (options.autoFocus == null) { options.autoFocus = false; } @@ -3887,6 +3914,9 @@ function ProgressBar(options) { if (!(this instanceof ProgressBar)) { return new ProgressBar(options); } + + options = options || {}; + Input.call(this, options); this.filled = options.filled || 0; @@ -4004,12 +4034,13 @@ ProgressBar.prototype.reset = function() { */ function FileManager(options) { + var self = this; + if (!(this instanceof FileManager)) { return new FileManager(options); } - var self = this; - + options = options || {}; options.parseTags = true; List.call(this, options); @@ -4188,6 +4219,8 @@ function Checkbox(options) { return new Checkbox(options); } + options = options || {}; + Input.call(this, options); this.text = options.content || options.text || ''; @@ -4257,12 +4290,12 @@ Checkbox.prototype.toggle = function() { */ function RadioSet(options) { - var self = this; - if (!(this instanceof RadioSet)) { return new RadioSet(options); } - + options = options || {}; + // Possibly inherit parent's style. + // options.style = this.parent.style; Box.call(this, options); } @@ -4282,6 +4315,8 @@ function RadioButton(options) { return new RadioButton(options); } + options = options || {}; + Checkbox.call(this, options); this.on('check', function() { @@ -4310,6 +4345,8 @@ function Prompt(options) { return new Prompt(options); } + options = options || {}; + Box.call(this, options); this._.input = new Textbox({ @@ -4401,6 +4438,8 @@ function Question(options) { return new Question(options); } + options = options || {}; + Box.call(this, options); this._.okay = new Button({ @@ -4490,6 +4529,7 @@ function Message(options) { return new Message(options); } + options = options || {}; options.tags = true; Box.call(this, options); @@ -4532,6 +4572,8 @@ function Info(options) { return new Info(options); } + options = options || {}; + Box.call(this, options); } @@ -4580,6 +4622,8 @@ function Loading(options) { return new Loading(options); } + options = options || {}; + Box.call(this, options); this._.icon = new Text({ @@ -4644,6 +4688,8 @@ function PickList(options) { return new PickList(options); } + options = options || {}; + List.call(this, options); } @@ -4676,6 +4722,8 @@ function Listbar(options) { return new Listbar(options); } + options = options || {}; + this.items = []; this.commands = options.commands; this.leftBase = 0; @@ -4847,6 +4895,8 @@ function DirManager(options) { return new DirManager(options); } + options = options || {}; + FileManager.call(this, options); this.on('cd', function(dir) { @@ -4870,6 +4920,7 @@ function Passbox(options) { return new Passbox(options); } + options = options || {}; options.censor = true; Textbox.call(this, options); diff --git a/test/insert.js b/test/insert.js index ecc0047..dcd09ac 100644 --- a/test/insert.js +++ b/test/insert.js @@ -1,11 +1,7 @@ -var blessed = require('blessed') - , program = blessed(); +var blessed = require('../') + , screen = blessed.screen(); -var screen = new blessed.Screen({ - program: program -}); - -var main = new blessed.Box({ +var main = blessed.box({ width: screen.width, height: screen.height, bg: 'yellow', diff --git a/test/tput.js b/test/tput.js index 8ea0100..6d422f9 100644 --- a/test/tput.js +++ b/test/tput.js @@ -24,7 +24,7 @@ // $ node test/tput.js xterm-256color --ifile ~/.terminfo/x/xterm-256color | tee out // $ cdiff test/terminfo out -var Tput = require('../').Tput; +var blessed = require('../'); // Simple argument parser // Copyright (c) 2012, Christopher Jeffrey (MIT License) @@ -80,7 +80,7 @@ function parseArg() { var argv = parseArg(); -var tput = Tput({ +var tput = blessed.tput({ term: argv[0] || 'xterm', extended: true, debug: true, @@ -95,7 +95,7 @@ console.log('Max colors: %d.', tput.colors); // console.log(tput.strings.acs_chars.split('').map(function(ch) { return ch.charCodeAt(0); })); // console.log(JSON.stringify(tput.strings.acs_chars)); -// process.stdout.write(Tput.sprintf('%-10s\n', 'hello')); +// process.stdout.write(blessed.tput.sprintf('%-10s\n', 'hello')); // tput._compile('%?%p9%t\u001b(0%e\u001b(B%;\u001b[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m'); diff --git a/test/widget-csr.js b/test/widget-csr.js index 9fde4b7..f8fb0a6 100644 --- a/test/widget-csr.js +++ b/test/widget-csr.js @@ -1,4 +1,4 @@ -var blessed = require('blessed') +var blessed = require('../') , screen; screen = blessed.screen({ diff --git a/test/widget-file.js b/test/widget-file.js index 816ee46..0d3cf79 100644 --- a/test/widget-file.js +++ b/test/widget-file.js @@ -1,4 +1,4 @@ -var blessed = require('blessed'); +var blessed = require('../'); var screen = blessed.screen({ tput: true diff --git a/test/widget-form.js b/test/widget-form.js index 04cb113..0b9b01a 100644 --- a/test/widget-form.js +++ b/test/widget-form.js @@ -1,4 +1,4 @@ -var blessed = require('blessed') +var blessed = require('../') , screen = blessed.screen(); var form = blessed.form({ diff --git a/test/widget-insert.js b/test/widget-insert.js index ee8a438..9104346 100644 --- a/test/widget-insert.js +++ b/test/widget-insert.js @@ -1,4 +1,4 @@ -var blessed = require('blessed'); +var blessed = require('../'); var screen = blessed.screen({ tput: true diff --git a/test/widget-listbar.js b/test/widget-listbar.js index baf4177..fafa1ed 100644 --- a/test/widget-listbar.js +++ b/test/widget-listbar.js @@ -1,5 +1,5 @@ -var blessed = require('blessed') - , screen; screen = blessed.screen(); +var blessed = require('../') + , screen = blessed.screen(); var bar = blessed.listbar({ parent: screen, diff --git a/test/widget-pos.js b/test/widget-pos.js index 3112906..f765e28 100644 --- a/test/widget-pos.js +++ b/test/widget-pos.js @@ -1,16 +1,12 @@ -var blessed = require('blessed') - , program = blessed() +var blessed = require('../') + , screen = blessed.screen() , assert = require('assert'); // My terminal size at the time of writing these tests: -program.cols = 154; -program.rows = 19; +screen.program.cols = 154; +screen.program.rows = 19; -var screen = new blessed.Screen({ - program: program -}); - -var main = new blessed.Box({ +var main = blessed.box({ //width: '75%', //height: '75%', width: 115, @@ -23,7 +19,7 @@ var main = new blessed.Box({ screen.append(main); -var inner = new blessed.Box({ +var inner = blessed.box({ width: '50%', height: '50%', //width: 57, diff --git a/test/widget-shrink-padding.js b/test/widget-shrink-padding.js index 6cd8628..12cd406 100644 --- a/test/widget-shrink-padding.js +++ b/test/widget-shrink-padding.js @@ -1,4 +1,4 @@ -var blessed = require('blessed') +var blessed = require('../') , screen = blessed.screen(); var outer = blessed.box({ diff --git a/test/widget-textarea.js b/test/widget-textarea.js index 398cc31..cf2a5a8 100644 --- a/test/widget-textarea.js +++ b/test/widget-textarea.js @@ -1,8 +1,6 @@ -var blessed = require('blessed'); +var blessed = require('../'); -var screen = blessed.screen({ - tput: true -}); +var screen = blessed.screen(); var box = blessed.textarea({ parent: screen, diff --git a/test/widget.js b/test/widget.js index 0e6072e..afb4b8f 100644 --- a/test/widget.js +++ b/test/widget.js @@ -1,10 +1,7 @@ -var blessed = require('blessed'); +var blessed = require('../') + , screen = blessed.screen(); -var screen = new blessed.Screen({ - tput: true -}); - -screen.append(new blessed.Text({ +screen.append(blessed.text({ top: 0, left: 2, width: '100%', @@ -16,7 +13,7 @@ screen.append(new blessed.Text({ align: 'center' })); -screen.append(new blessed.Line({ +screen.append(blessed.line({ orientation: 'horizontal', top: 1, left: 0, @@ -24,7 +21,7 @@ screen.append(new blessed.Line({ })); /* -screen.append(new blessed.Box({ +screen.append(blessed.box({ fg: 4, bg: -1, border: { @@ -39,7 +36,7 @@ screen.append(new blessed.Box({ left: 'center' })); -screen.children[0].append(new blessed.Box({ +screen.children[0].append(blessed.box({ fg: 4, bg: 3, border: { @@ -56,7 +53,7 @@ screen.children[0].append(new blessed.Box({ })); */ -var list = new blessed.List({ +var list = blessed.list({ align: 'center', mouse: true, fg: 'blue', @@ -88,7 +85,7 @@ var list = new blessed.List({ screen.append(list); list.select(0); -list.prepend(new blessed.Text({ +list.prepend(blessed.text({ left: 2, content: ' My list ' })); @@ -105,7 +102,7 @@ list.on('keypress', function(ch, key) { } }); -var progress = new blessed.ProgressBar({ +var progress = blessed.progressbar({ fg: 'blue', bg: 'default', barBg: 'default', @@ -134,7 +131,7 @@ var lorem = require('fs').readFileSync(__dirname + '/git.diff', 'utf8'); //lorem = lorem.replace(/\x1b[^m]*m/g, ''); -var stext = new blessed.ScrollableText({ +var stext = blessed.scrollabletext({ //padding: 1, mouse: true, content: lorem, @@ -184,7 +181,7 @@ screen.on('element mouseout', function(el) { }); */ -var input = new blessed.Textbox({ +var input = blessed.textbox({ mouse: true, label: ' My Input ', content: '',