Skip to content
Browse files

font() chaining & optional style param

  • Loading branch information...
1 parent 93f13fc commit 3bd388c5c7268d49114ab9f65b5116ff9f009196 @khrome committed Feb 2, 2013
Showing with 82 additions and 4 deletions.
  1. +20 −2 README.md
  2. +61 −1 ascii-art.js
  3. +1 −1 package.json
View
22 README.md
@@ -5,7 +5,7 @@ A bunch of utilities for outputting textual art
Figlet fonts
------------
-A slight mod of [Figlet.js](https://github.com/scottgonzalez/figlet-js)
+A slight mod of [Figlet.js](https://github.com/scottgonzalez/figlet-js) which I hope will make it to NPM in pure js form someday, rather than requiring a command line dependency.
Figlet fonts are an ASCII art font standard, widely used for terminal output.
@@ -48,7 +48,25 @@ Control styles are: off, hidden
Images
------
-Will be coming soon!
+Will be coming soon™!
+
+Chaining
+--------
+
+The font method also allows you to optionally pass styles and supports chaining, so if I want something a little more complex I could do something like this:
+
+ art.font('Prompt', 'Basic', 'red').font('v1', 'Doom', 'magenta', function(rendered){
+ console.log(rendered);
+ });
+
+and that will look like this (in color, of course) and it will totally respect the multiline chars and add in ansi codes per line, so things look how you'd expect:
+
+ d8888b. d8888b. .d88b. .88b d88. d8888b. d888888b __
+ 88 `8D 88 `8D .8P Y8. 88'YbdP`88 88 `8D `~~88~~' / |
+ 88oodD' 88oobY' 88 88 88 88 88 88oodD' 88 __ __`| |
+ 88~~~ 88`8b 88 88 88 88 88 88~~~ 88 \ \ / / | |
+ 88 88 `88. `8b d8' 88 88 88 88 88 \ V / _| |_
+ 88 88 YD `Y88P' YP YP YP 88 YP \_/ \___/
Hope that helps, please report any rough edges!
View
62 ascii-art.js
@@ -138,7 +138,67 @@ AsciiArt.Figlet = {
});
}
};
-AsciiArt.font = AsciiArt.Figlet.write;
+//todo: optional styling on font callback
+var combine = function(blockOne, blockTwo, style){
+ var linesOne = blockOne.split("\n");
+ var linesTwo = blockTwo.split("\n");
+ var diff = Math.max(linesOne.length - linesTwo.length, 0);
+ linesOne.forEach(function(line, index){
+ if(index >= diff){
+ if(style){
+ linesOne[index] = linesOne[index]+AsciiArt.ansiCodes(linesTwo[index-diff], style);
+ }else{
+ linesOne[index] = linesOne[index]+linesTwo[index-diff];
+ }
+ }
+ });
+ return linesOne.join("\n");
+};
+var fontChain = function(){
+ var cb;
+ var chain = [];
+ var result;
+ var check = function(){
+ if(!this.checking) this.checking = true;
+ if(result && cb && chain.length === 0) cb(result);
+ if(chain.length){
+ var item = chain.shift();
+ AsciiArt.Figlet.write(item.text, item.font, function(text){
+ result = combine(result||(new Array(text.split("\n").length)).join("\n"), text, item.style);
+ check();
+ });
+ }
+ }
+ ob = this;
+ this.font = function(str, fontName, style, callback){
+ if(typeof style == 'function'){
+ callback = style;
+ style = undefined;
+ }
+ if(callback) cb = callback;
+ chain.push({
+ font : fontName,
+ text : str,
+ style : style
+ });
+ if(!this.checking) check();
+ return ob;
+ };
+ return this;
+};
+
+AsciiArt.font = function(str, fontName, style, callback){
+ if(!callback){
+ var chain = fontChain();
+ return chain.font(str, fontName, style);
+ }else{
+ return AsciiArt.Figlet.write(str, fontName, function(text){
+ if(style) text = AsciiArt.ansiCodes(text, style);
+ callback(text);
+ });
+ }
+}
+//AsciiArt.font = AsciiArt.Figlet.write;
AsciiArt.style = AsciiArt.ansiCodes;
//todo: AsciiArt.image
module.exports = AsciiArt;
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "ascii-art",
"homepage": "https://github.com/khrome/ascii-art",
- "version": "0.0.1-alpha",
+ "version": "0.0.2-alpha",
"main": "ascii-art.js",
"description": "A package for ansi codes, figlet fonts, and ascii art",
"keywords": [

0 comments on commit 3bd388c

Please sign in to comment.
Something went wrong with that request. Please try again.