Comparing version 0.3.0 to 1.0.0
233
ascli.js
@@ -17,135 +17,122 @@ /* | ||
var util = require("util"), | ||
path = require("path"), | ||
colour = require("colour"); | ||
// Default alphabet | ||
var alphabet = require(path.join(__dirname, "alphabet", "straight.json")); | ||
module.exports = function(appName) { | ||
ascli.appName = appName; | ||
return ascli; | ||
}; | ||
module.exports.app = module.exports; // For backward compatibility | ||
/** | ||
* @license ascli (c) 2013 Daniel Wirtz <dcode@dcode.io> | ||
* Released under the Apache License, Version 2.0 | ||
* see: https://github.com/dcodeIO/ascli for details | ||
* Builds a banner. | ||
* @param {string=} title App name | ||
* @param {string=} appendix Appendix, e.g. version | ||
* @returns {string} | ||
*/ | ||
module.exports = (function() { | ||
var util = require("util"), | ||
path = require("path"), | ||
colour = require("colour"); | ||
// Enforce one blank before each line | ||
function indent1() { | ||
this.write(" "+util.format.apply(null, arguments).replace(/\n/g, "\n ")+"\n"); | ||
} | ||
console.log = indent1.bind(process.stdout); | ||
console.info = indent1.bind(process.stdout); | ||
console.warn = indent1.bind(process.stderr); | ||
console.error = indent1.bind(process.stderr); | ||
// Default alphabet | ||
var alphabet = require(path.join(__dirname, "alphabet", "straight.json")); | ||
/** | ||
* Builds a banner. | ||
* @param {string=} title App name | ||
* @param {string=} appendix Appendix, e.g. version | ||
* @returns {string} | ||
*/ | ||
function ascli(title, appendix) { | ||
title = title || ascli.appName; | ||
appendix = appendix || ""; | ||
var lines = ["", "", ""], c, a, j, ac = ""; | ||
for (var i=0; i<title.length; i++) { | ||
c = title.charAt(i); | ||
if (c == '\x1B') { | ||
while ((c=title.charAt(i)) != 'm') { | ||
ac += c; | ||
i++; | ||
} | ||
function ascli(title, appendix) { | ||
title = title || ascli.appName; | ||
appendix = appendix || ""; | ||
var lines = ["", "", ""], c, a, j, ac = ""; | ||
for (var i=0; i<title.length; i++) { | ||
c = title.charAt(i); | ||
if (c == '\x1B') { | ||
while ((c=title.charAt(i)) != 'm') { | ||
ac += c; | ||
} else if ((a=alphabet[c])||(a=alphabet[c.toLowerCase()])) | ||
for (j=0; j<3; j++) | ||
lines[j] += ac+a[j]; | ||
} | ||
for (i=0; i<lines.length; i++) lines[i] = lines[i]+"\x1B[0m"; | ||
lines[1] += " "+appendix; | ||
if (lines[lines.length-1].strip.trim().length == 0) { | ||
lines.pop(); | ||
} | ||
return '\n'+lines.join('\n')+'\n'; | ||
i++; | ||
} | ||
ac += c; | ||
} else if ((a=alphabet[c])||(a=alphabet[c.toLowerCase()])) | ||
for (j=0; j<3; j++) | ||
lines[j] += ac+a[j]; | ||
} | ||
for (i=0; i<lines.length; i++) lines[i] = lines[i]+"\x1B[0m"; | ||
lines[1] += " "+appendix; | ||
if (lines[lines.length-1].strip.trim().length == 0) { | ||
lines.pop(); | ||
} | ||
return '\n'+lines.join('\n')+'\n'; | ||
} | ||
/** | ||
* App name. | ||
* @type {string} | ||
*/ | ||
ascli.appName = "app"; | ||
// Indent by one | ||
function indent1() { | ||
this.write(" "+util.format.apply(null, arguments).replace(/\n/g, "\n ")+"\n"); | ||
} | ||
ascli.log = indent1.bind(process.stdout); | ||
ascli.info = indent1.bind(process.stdout); | ||
ascli.warn = indent1.bind(process.stderr); | ||
ascli.error = indent1.bind(process.stderr); | ||
/** | ||
* Prints a banner to console. | ||
* @param {string=} title Title in dojo alphabet | ||
* @param {string=} appendix Title appendix | ||
* @returns {Function} ascli | ||
*/ | ||
ascli.banner = function(title, appendix) { | ||
console.log(ascli(title, appendix)); | ||
return ascli; | ||
}; | ||
/** | ||
* App name. | ||
* @type {string} | ||
*/ | ||
ascli.appName = "app"; | ||
/** | ||
* Uses another alphabet. | ||
* @param {string|Object.<string,Array.<string>} alpha File name or alphabet to use | ||
* @returns {Function} ascli | ||
*/ | ||
ascli.use = function(alpha) { | ||
if (typeof alpha === 'string') { | ||
alphabet = require(alpha); | ||
} else { | ||
alphabet = alpha; | ||
} | ||
return ascli; | ||
}; | ||
/** | ||
* Prints a banner to console. | ||
* @param {string=} title Title in dojo alphabet | ||
* @param {string=} appendix Title appendix | ||
* @returns {Function} ascli | ||
*/ | ||
ascli.banner = function(title, appendix) { | ||
console.log(ascli(title, appendix)); | ||
return ascli; | ||
}; | ||
/** | ||
* Sets the app name. | ||
* @param {string} appName | ||
* @returns {Function} ascli | ||
*/ | ||
ascli.app = function(appName) { | ||
ascli.appName = appName; | ||
return ascli; | ||
}; | ||
/** | ||
* Uses another alphabet. | ||
* @param {string|Object.<string,Array.<string>} alpha File name or alphabet to use | ||
* @returns {Function} ascli | ||
*/ | ||
ascli.use = function(alpha) { | ||
if (typeof alpha === 'string') | ||
alphabet = require(alpha); | ||
else | ||
alphabet = alpha; | ||
return ascli; | ||
}; | ||
/** | ||
* Terminates the application with OK. | ||
* @param {string} msg Message text | ||
* @param {number=} code Exit code, defaults to 0 | ||
*/ | ||
ascli.ok = function(msg, code) { | ||
process.stderr.write('\n '+ascli.appName.green.bold+' OK'.white.bold+(msg ? ' '+msg : '')+'\n'); | ||
process.exit(typeof code != 'undefined' ? code : 0); | ||
}; | ||
/** | ||
* Prints a final success message. | ||
* @param {string} msg Message text | ||
* @param {number=} code Exit code, defaults not to send it explicitly | ||
*/ | ||
ascli.ok = function(msg, code) { | ||
process.stderr.write('\n '+ascli.appName.green.bold+' OK'.white.bold+(msg ? ' '+msg : '')+'\n'); | ||
if (typeof code !== 'undefined') | ||
process.exit(code); | ||
}; | ||
/** | ||
* Terminates the application with an ERROR. | ||
* @param {string} msg Message text | ||
* @param {number=} code Exit code | ||
*/ | ||
ascli.fail = function(msg, code) { | ||
process.stderr.write('\n '+ascli.appName.red.bold+' ERROR'.white.bold+(msg ? ' '+msg : '')+'\n'); | ||
process.exit(typeof code != 'undefined' ? code : 1); | ||
}; | ||
/** | ||
* Prints a final failure message. | ||
* @param {string} msg Message text | ||
* @param {number=} code Exit code, defaults to not send it explicitly | ||
*/ | ||
ascli.fail = function(msg, code) { | ||
process.stderr.write('\n '+ascli.appName.red.bold+' ERROR'.white.bold+(msg ? ' '+msg : '')+'\n'); | ||
if (typeof code !== 'undefined') | ||
process.exit(code); | ||
}; | ||
/** | ||
* opt.js | ||
* @param {Array.<string>=} argv | ||
* @returns {{node: string, script: string, argv: Array.<string>, opt: Object.<string,boolean|string>}} | ||
*/ | ||
ascli.optjs = require("optjs"); | ||
// Pre-run it | ||
var opt = ascli.optjs(); | ||
ascli.node = opt.node; | ||
ascli.script = opt.script; | ||
ascli.argv = opt.argv; | ||
ascli.opt = opt.opt; | ||
// Expose colour.js | ||
ascli.colour = ascli.colors = colour; | ||
return ascli; | ||
})(); | ||
/** | ||
* opt.js | ||
* @param {Array.<string>=} argv | ||
* @returns {{node: string, script: string, argv: Array.<string>, opt: Object.<string,boolean|string>}} | ||
*/ | ||
ascli.optjs = require("optjs"); | ||
// Pre-run it | ||
var opt = ascli.optjs(); | ||
ascli.node = opt.node; | ||
ascli.script = opt.script; | ||
ascli.argv = opt.argv; | ||
ascli.opt = opt.opt; | ||
// Expose colour.js | ||
ascli.colour = ascli.colors = colour; |
{ | ||
"name": "ascli", | ||
"description": "A uniform foundation for unobtrusive (ASCII art in) cli apps.", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"author": "Daniel Wirtz <dcode@dcode.io>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -19,9 +19,12 @@ ![ascli](https://raw.github.com/dcodeIO/ascli/master/ascli.png) | ||
```js | ||
var ascli = require("ascli").app("myApp"); | ||
ascli.banner(ascli.appName.green.bold, "v1.0.0 by Foo Bar <foobar@example.com>"); | ||
console.log("Hello!"); | ||
var cli = require("ascli")("myAppName"); | ||
cli.banner(ascli.appName.green.bold, "v1.0.0 by Foo Bar <foobar@example.com>"); | ||
cli.log("Hello!"); | ||
cli.info("World!"); | ||
cli.warn("of"); | ||
cli.error("ascli."); | ||
// If it worked: | ||
ascli.ok("It worked!"); | ||
cli.ok("It worked!", /* optional exit code */ 0); | ||
// If it didn't: | ||
ascli.fail("Nope, sorry.", /* exit code */ 1); | ||
cli.fail("Nope, sorry.", /* optional exit code */ 1); | ||
``` | ||
@@ -34,6 +37,6 @@ | ||
```js | ||
ascli.use("/path/to/my/alphabet.json"); | ||
cli.use("/path/to/my/alphabet.json"); | ||
// or | ||
var myAlphabet = { ... }; | ||
ascli.use(myAlphabet); | ||
cli.use(myAlphabet); | ||
``` | ||
@@ -46,15 +49,15 @@ | ||
[colour.js](https://github.com/dcodeIO/colour.js) which is also exposed as a property of the ascli namespace: | ||
`ascli.colour` / `ascli.colors`. Also means: You don't need another ANSI terminal colors dependency. | ||
`cli.colour` / `cli.colors`. Also means: You don't need another ANSI terminal colors dependency. | ||
#### Indentation | ||
ascli automatically indents all console output by one space just because it looks better with the banner. | ||
`cli.log` etc. indents all console output by one space just because it looks better with the banner. | ||
Parsing command line arguments | ||
------------------------------ | ||
[opt.js](https://github.com/dcodeIO/opt.js) will be pre-run on the `ascli` namespace and also exposed as `ascli.optjs()`. | ||
[opt.js](https://github.com/dcodeIO/opt.js) will be pre-run on the `cli` namespace and also exposed as `cli.optjs()`. | ||
```js | ||
ascli.node // Node executable | ||
ascli.script // Executed script | ||
ascli.opt // Options as a hash | ||
ascli.argv // Remaining non-option arguments | ||
cli.node // Node executable | ||
cli.script // Executed script | ||
cli.opt // Options as a hash | ||
cli.argv // Remaining non-option arguments | ||
``` | ||
@@ -61,0 +64,0 @@ |
@@ -1,9 +0,10 @@ | ||
var ascli = require("../ascli.js").app("myapp"); | ||
var cli = require("../ascli.js")("myapp"); | ||
ascli.banner("staying straight".green.bold, "v1.0.0 through ascli"); | ||
console.log("Hello world!".white.bold); | ||
console.log("...of ascli\n"); | ||
console.log("Command line arguments".white.bold); | ||
console.log(ascli.opt, ascli.argv); | ||
ascli.banner("abcdefghijklmnopqrstuvwxyz 0123456789"); | ||
ascli.ok("yep, that worked."); | ||
cli.banner("staying straight".green.bold, "v1.0.0 through ascli"); | ||
cli.log("Hello world!".white.bold); | ||
cli.log("...of ascli\n"); | ||
cli.log("Command line arguments".white.bold); | ||
cli.log(cli.opt, cli.argv); | ||
cli.banner("abcdefghijklmnopqrstuvwxyz 0123456789"); | ||
cli.ok("yep, that worked."); | ||
cli.fail("nope, that didn't."); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
38537
15
0
65
341