diff --git a/Gruntfile.js b/Gruntfile.js index aae9e96e..b2c07e8f 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -37,22 +37,27 @@ module.exports = function(grunt) { // TEST //---------------------------------------------------------------------------------------------------------------------------------------------------------- exec: { - test01: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü"', - test02: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -c red,magenta -m 15', + test01: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü"', + test02: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -c red,magenta -m 15', - test03: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "simple"', - test04: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "simple" -c yellow -m 20', + test03: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simple"', + test04: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simple" -c yellow -m 20', - test05: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "3d"', - test06: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "3d" -c magenta,yellow -m 7', + test05: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "3d"', + test06: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "3d" -c magenta,yellow -m 7', - test07: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "console"', - test08: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "console" -c blue -b white', + test07: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "console"', + test08: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "console" -c blue -b white', - test09: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "simple3d"', - test10: 'cfonts -t "a b c defghijklmnopqrstuvwxyz|0123456789|\!?.+-_=@#$%&()/:;ü" -f "simple3d" -c cyan -b white', - }, + test09: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simple3d"', + test10: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simple3d" -c cyan -b white', + + test11: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simpleBlock"', + test12: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -f "simpleBlock" -c green -b white', + test13: 'cfonts "a b c defghijklmnopqrstuvwxyz0123456789\!?.+-_=@#$%&()/:;ü" -a "center"', + test13: 'cfonts "a b c" -a right', + }, }); diff --git a/README.md b/README.md index 1e4187a8..5a54e8ef 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ ```shell - ██████╗ ███████╗ ██████╗ ███╗ ██╗ ████████╗ ███████╗ - ██╔════╝ ██╔════╝ ██╔═══██╗ ████╗ ██║ ╚══██╔══╝ ██╔════╝ - ██║ █████╗ ██║ ██║ ██╔██╗ ██║ ██║ ███████╗ - ██║ ██╔══╝ ██║ ██║ ██║╚██╗██║ ██║ ╚════██║ - ╚██████╗ ██║ ╚██████╔╝ ██║ ╚████║ ██║ ███████║ - ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚══════╝ + ██████╗ ███████╗ ██████╗ ███╗ ██╗ ████████╗ ███████╗ + ██╔════╝ ██╔════╝ ██╔═══██╗ ████╗ ██║ ╚══██╔══╝ ██╔════╝ + ██║ █████╗ ██║ ██║ ██╔██╗ ██║ ██║ ███████╗ + ██║ ██╔══╝ ██║ ██║ ██║╚██╗██║ ██║ ╚════██║ + ╚██████╗ ██║ ╚██████╔╝ ██║ ╚████║ ██║ ███████║ + ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚══════╝ ``` [![NPM](https://nodei.co/npm/cfonts.png?downloads=true)](https://nodei.co/npm/cfonts/) -> This is a silly little command line tool for sexy fonts in the console. +> This is a silly little command line tool for sexy fonts in the console. **Give your cli some love.** ## Examples @@ -28,26 +28,35 @@ Real world example: To install the CLI app, simply NPM install it globally. ```shell -$ sudo npm install cfonts -g +$ npm install cfonts -g ``` -To use it in your project: +To use it in your shell: + +```shell +$ cfonts "Hello|World\!" +``` + +_Remember to escape the `!` character with `\` in the shell_ + +Or use it in your project: ```js -var FONTS = require('cfonts'); - -var fonts = new FONTS({ - 'text': 'Hello World', //text to be converted - 'font': 'block', //define the font face - 'colors': '', //define all colors - 'background': 'Black', //define the background color - 'letterSpacing': 1, //define letter spacing - 'space': true, //define if the output text should have empty lines on top and on the bottom - 'maxLength': '10' //define how many character can be on one line +var CFonts = require('cfonts'); + +CFonts.say('Hello|world!', { + 'font': 'block', //define the font face + 'align': 'left', //define text alignment + 'colors': ['white'], //define all colors + 'background': 'Black', //define the background color + 'letterSpacing': 1, //define letter spacing + 'lineHeight': 1, //define the line height + 'space': true, //define if the output text should have empty lines on top and on the bottom + 'maxLength': '0' //define how many character can be on one line }); ``` -_Remember to escape the `!` character with `\` in the shell_ +_All settings are optional and shown here with their default_ ## Usage @@ -55,7 +64,7 @@ _Remember to escape the `!` character with `\` in the shell_ Using the CLI is easy. ``` -Usage: cfonts , etc... +Usage: cfonts "" [option1] [option2] , [option3] etc... ``` At any point you can run the *help* command to get a full list of commands and @@ -129,7 +138,7 @@ _The `|` character will be replaced with a line break_ ## Options #### -h, --help -Type: `` +Type: `` Default value: `none` This shows a list of all available options. @@ -140,7 +149,7 @@ $ cfonts --help #### -V, --version -Type: `` +Type: `` Default value: `none` This shows the installed version. @@ -150,40 +159,56 @@ $ cfonts --version ``` -#### -t, --text -Type: `` +#### text +Type: `` Default value: `""` This is the "textinput" to be converted into a nice font ```shell -$ cfonts --text "Hello world!" +$ cfonts "Hello world" ``` #### -f, --font -Type: `` +Type: `` Default value: `"block"` This is the font face you want to use. So far this plugin ships with with following font faces: -* `console` [colors: 1] -* `block` [colors: 2] -* `simple` [colors: 1] -* `3d` [colors: 2] -* `simple3d` [colors: 1] +- `console` [colors: 1] +- `block` [colors: 2] _(default)_ +- `simpleBlock` [colors: 1] +- `simple` [colors: 1] +- `3d` [colors: 2] +- `simple3d` [colors: 1] ```shell -$ cfonts --font "block" +$ cfonts "text" -f "block3d" +``` + + +#### -a, --align +Type: `` +Default value: `"left"` + +You can align your text in the terminal with this option. Use the keywords below: + +- `left` _(default)_ +- `center` +- `right` + +```shell +$ cfonts "text" -a "center" ``` #### -c, --colors -Type: `` +Type: `` Default value: `[]` -In this setting you can set the colors for each font face. Use the below color strings build in by [chalk](https://github.com/sindresorhus/chalk). -Provide colors in a comma-separated string, eg: `red,blue` +With this setting you can set the colors for your font. Use the below color strings built in by [chalk](https://github.com/sindresorhus/chalk). +Provide colors in a comma-separated string, eg: `red,blue` _(no spaces)_ - `black` - `red` @@ -192,22 +217,22 @@ Provide colors in a comma-separated string, eg: `red,blue` - `blue` - `magenta` - `cyan` -- `white` +- `white` _(default)_ - `gray` ```shell -$ cfonts --colors white,blue +$ cfonts "text" --colors white,blue ``` #### -b, --background -Type: `` +Type: `` Default value: `"Black"` -In this setting you can set the background colors for the output. Use the below color strings build in by [chalk](https://github.com/sindresorhus/chalk). -Provide the background color from the below supported list, eg: 'white' +With this setting you can set the background colors for the output. Use the below color strings built in by [chalk](https://github.com/sindresorhus/chalk). +Provide the background color from the below supported list, eg: 'White' -- `Black` +- `Black` _(default)_ - `Red` - `Green` - `Yellow` @@ -217,41 +242,51 @@ Provide the background color from the below supported list, eg: 'white' - `White` ```shell -$ cfonts --background "Green" +$ cfonts "text" --background "Green" ``` #### -l, --letter-spacing -Type: `` +Type: `` Default value: `1` Set this option to widen the space between characters. ```shell -$ cfonts --letter-spacing 2 +$ cfonts "text" --letter-spacing 2 +``` + + +#### -z, --line-height +Type: `` +Default value: `1` + +Set this option to widen the space between lines. + +```shell +$ cfonts "text" --line-height 2 ``` -#### -s, --space -Type: `` +#### -s, --spaceless +Type: `` Default value: `true` Set this option to false if you don't want the plugin to insert two empty lines on top and on the bottom of the output. ```shell -$ cfonts --space false +$ cfonts "text" --spaceless ``` #### -m, --max-length -Type: `` -Default value: `10` +Type: `` +Default value: `0` -This option sets the maximum characters that will be printed on one line. As the shell usually doesn't give you access to its width, this is nessesary -to not end up with a scrambled text output. +This option sets the maximum characters that will be printed on one line. 0 means no max width and the text will break at the edge of the terminal window. ```shell -$ cfonts --max-length 15 +$ cfonts "text" --max-length 15 ``` @@ -260,6 +295,7 @@ Please look at the coding style and work with it, not against it ;) ## Release History +* 1.0.0 - refactor, added alignment and line height option, new cli commands, added simpleBlock * 0.0.13 - fixed simple3d * 0.0.12 - fixed simple3d and added to grunt test * 0.0.11 - added simple3d font @@ -276,4 +312,4 @@ Please look at the coding style and work with it, not against it ;) ## License -Copyright (c) 2015 Dominik Wilkowski. Licensed under the [GNU GPL](https://github.com/dominikwilkowski/cfonts/blob/master/LICENSE). \ No newline at end of file +Copyright (c) 2016 Dominik Wilkowski. Licensed under the [GNU GPLv2](https://github.com/dominikwilkowski/cfonts/blob/master/LICENSE). \ No newline at end of file diff --git a/bin/font.js b/bin/font.js old mode 100644 new mode 100755 index 8d06a534..feeb6d06 --- a/bin/font.js +++ b/bin/font.js @@ -1,11 +1,15 @@ #!/usr/bin/env node -/* +/*************************************************************************************************************************************************************** + * * cfonts - * https://github.com/dominikwilkowski/cfonts * - * Copyright (c) 2015 Dominik Wilkowski - * Licensed under the MIT license. - */ + * Sexy fonts for the console. (CLI output) + * + * @license https://github.com/dominikwilkowski/cfonts/blob/master/LICENSE GNU GPLv2 + * @author Dominik Wilkowski hi@dominik-wilkowski.com + * @repository https://github.com/dominikwilkowski/cfonts + * + **************************************************************************************************************************************************************/ 'use strict'; @@ -13,55 +17,87 @@ //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Dependencies //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -var fs = require('fs'); -var chalk = require('chalk'); -var program = require('commander'); -var CFONTS = require('./../index.js'); +const Fs = require('fs'); +const Chalk = require('chalk'); +const Program = require('commander'); +const CFonts = require('./../index.js'); -var $package = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')); -var $version = $package.version; +const Package = JSON.parse(Fs.readFileSync(__dirname + '/../package.json', 'utf8')); +const Version = Package.version; //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Custom functions +// Setting up command line tool //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -function list(val) { - return val.split(','); -} +Program + .description(`This is a tool for sexy fonts in the console. Give your cli some love.`) + .version(`v${Version}`) + .usage(`"" [option1] [option2] , [option3]`) + .option(`-f, --font `, `define "font face"`, `block`) + .option(`-a, --align `, `define "alignment" for the text`, `left`) + .option(`-c, --colors ,...`, `provide colors for text`, `white`) + .option(`-b, --background `, `provide background color`, `Black`) + .option(`-l, --letter-spacing `, `define letter spacing {integer}`) + .option(`-z, --line-height `, `define line height {integer}`, 1) + .option(`-s, --spaceless`, `surpress space on top and on the bottom`) + .option(`-m, --max-length `, `define how many character can be on one line`) + .action(function( text ) { + Program.text = text; //add flagless option for text + }) + .on('--help', function() { //adding options for each keyword section + console.log( Chalk.bold(` Font face options:`) ); + console.log(` [ ${CFonts.FONTFACES.join(', ')} ]\n`); + console.log( Chalk.bold(` Alignment options:`) ); + console.log(` [ ${CFonts.ALIGNMENT.join(', ')} ]\n`); -//-------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Command line -//-------------------------------------------------------------------------------------------------------------------------------------------------------------- -program - .version( $version ) - .usage('[option1] [options2] ,') - .option('-t, --text ', '"textinput" to be converted into a nice font') - .option('-f, --font ', '"fontname" to be used') - .option('-c, --colors ,...', 'provide colors in format: red,blue etc.', list) - .option('-b, --background ', 'provide background color in format: \'white\'') - .option('-l, --letter-spacing ', 'letterSpacing to be used as integer') - .option('-s, --space ', 'define if the output text should have empty lines on top and on the bottom') - .option('-m, --max-length \\", " /\\___/", - " \\/__/" + " \\/__/ " ], "Z": [ - "", - " ____", - "/\\_ ,`\\", - "\\/_/ /_", + " ", + " ____ ", + "/\\_ ,`\\ ", + "\\/_/ /_ ", " /\\____\\", " \\/____/", - "" + " " ], "0": [ - " __", - " /'__`\\", - "/\\ \\/\\ \\", - "\\ \\ \\ \\ \\", + " __ ", + " /'__`\\ ", + "/\\ \\/\\ \\ ", + "\\ \\ \\ \\ \\ ", " \\ \\ \\_\\ \\", " \\ \\____/", - " \\/___/" + " \\/___/ " ], "1": [ - " _", - " /' \\", - "/\\_, \\", - "\\/_/\\ \\", - " \\ \\ \\", + " _ ", + " /' \\ ", + "/\\_, \\ ", + "\\/_/\\ \\ ", + " \\ \\ \\ ", " \\ \\_\\", " \\/_/" ], "2": [ - " ___", - " /'___`\\", - "/\\_\\ /\\ \\", - "\\/_/// /__", + " ___ ", + " /'___`\\ ", + "/\\_\\ /\\ \\ ", + "\\/_/// /__ ", " // /_\\ \\", " /\\______/", - " \\/_____/" + " \\/_____/ " ], "3": [ - " __", - " /'__`\\", - "/\\_\\L\\ \\", - "\\/_/_\\_<_", + " __ ", + " /'__`\\ ", + "/\\_\\L\\ \\ ", + "\\/_/_\\_<_ ", " /\\ \\L\\ \\", " \\ \\____/", - " \\/___/" + " \\/___/ " ], "4": [ - " __ __", - "/\\ \\\\ \\", - "\\ \\ \\\\ \\", - " \\ \\ \\\\ \\_", + " __ __ ", + "/\\ \\\\ \\ ", + "\\ \\ \\\\ \\ ", + " \\ \\ \\\\ \\_ ", " \\ \\__ ,__\\", " \\/_/\\_\\_/", - " \\/_/" + " \\/_/ " ], "5": [ - " ______", - "/\\ ___\\", - "\\ \\ \\__/", - " \\ \\___``\\", + " ______ ", + "/\\ ___\\ ", + "\\ \\ \\__/ ", + " \\ \\___``\\ ", " \\/\\ \\L\\ \\", " \\ \\____/", - " \\/___/" + " \\/___/ " ], "6": [ - " ____", - " /'___\\", - "/\\ \\__/", - "\\ \\ _``\\", + " ____ ", + " /'___\\ ", + "/\\ \\__/ ", + "\\ \\ _``\\ ", " \\ \\ \\L\\ \\", " \\ \\____/", - " \\/___/" + " \\/___/ " ], "7": [ - " ________", + " ________ ", "/\\____ \\", "\\/___/' /'", - " /' /'", - " /' /'", - " /\\_/", - " \\//" + " /' /' ", + " /' /' ", + " /\\_/ ", + " \\// " ], "8": [ - " __", - " /' _`\\", - "/\\ \\L\\ \\", - "\\/_> _ <_", + " __ ", + " /' _`\\ ", + "/\\ \\L\\ \\ ", + "\\/_> _ <_ ", " /\\ \\L\\ \\", " \\ \\____/", - " \\/___/" + " \\/___/ " ], "9": [ - " __", - " /'_ `\\", - "/\\ \\L\\ \\", - "\\ \\___, \\", - " \\/__,/\\ \\", + " __ ", + " /'_ `\\ ", + "/\\ \\L\\ \\ ", + "\\ \\___, \\ ", + " \\/__,/\\ \\ ", " \\ \\_\\", " \\/_/" ], "!": [ - " __", - "/\\ \\", - "\\ \\ \\", - " \\ \\ \\", - " \\ \\_\\", + " __ ", + "/\\ \\ ", + "\\ \\ \\ ", + " \\ \\ \\ ", + " \\ \\_\\ ", " \\/\\_\\", " \\/_/" ], "?": [ - " _", - " /'_`\\", + " _ ", + " /'_`\\ ", "/\\_\\/\\`\\", "\\/_//'/'", - " /\\_\\", + " /\\_\\ ", " \\/\\_\\", " \\/_/" ], ".": [ - "", - "", - "", - "", - " __", + " ", + " ", + " ", + " ", + " __ ", "/\\_\\", "\\/_/" ], "+": [ - " __", - " /\\ \\", - " \\_\\ \\___", + " __ ", + " /\\ \\ ", + " \\_\\ \\___ ", "/\\___ __\\", "\\/__/\\ \\_/", - " \\ \\_\\", - " \\/_/" + " \\ \\_\\ ", + " \\/_/ " ], "-": [ - "", - "", - " _______", + " ", + " ", + " _______ ", "/\\______\\", "\\/______/", - "", - "" + " ", + " " ], "_": [ - "", - "", - "", - "", - " _______", + " ", + " ", + " ", + " ", + " _______ ", " /\\______\\", " \\/______/" ], "=": [ - "", - " _______", - "/\\______\\", - "\\/______/_", + " ", + " _______ ", + "/\\______\\ ", + "\\/______/_ ", " /\\______\\", " \\/______/", - "" + " " ], "@": [ - " __", - " /'_`\\_", - " /'/'_` \\", - "/\\ \\ \\L\\ \\", + " __ ", + " /'_`\\_ ", + " /'/'_` \\ ", + "/\\ \\ \\L\\ \\ ", "\\ \\ `\\__,_\\", " \\ `\\_____\\", " `\\/_____/" ], "#": [ - " __ __", - " _\\ \\\\ \\__", - "/\\__ _ _\\", - "\\/_L\\ \\\\ \\L_", + " __ __ ", + " _\\ \\\\ \\__ ", + "/\\__ _ _\\ ", + "\\/_L\\ \\\\ \\L_ ", " /\\_ _ _\\", " \\/_/\\_\\\\_\\/", - " \\/_//_/" + " \\/_//_/ " ], "$": [ - " _", - " /|_\\_", - " /' _ `\\", + " _ ", + " /|_\\_ ", + " /' _ `\\ ", " \\ \\___ \\", " \\ `\\_ _/", " `\\_/\\_\\", " \\/_/" ], "%": [ - " __ __", - "/\\_\\ /\\_\\", - "\\/_/ / / /", - " / / /", - " / / / __", + " __ __ ", + "/\\_\\ /\\_\\ ", + "\\/_/ / / / ", + " / / / ", + " / / / __ ", " / / / /\\_\\", " \\/_/ \\/_/" ], "&": [ - " ____", - " /| _ \\", - " |/\\ ` |", - " \\ / __`\\/\\", + " ____ ", + " /| _ \\ ", + " |/\\ ` | ", + " \\ / __`\\/\\ ", " /| \\L> <_", " | \\_____/\\/", " \\/____/\\/ " ], "(": [ - " _", - " /' \\", - "/\\ ,/", - "\\ \\ \\", - " \\ \\ `\\", + " _ ", + " /' \\ ", + "/\\ ,/ ", + "\\ \\ \\ ", + " \\ \\ `\\ ", " \\ `\\__\\", - " `\\/_/" + " `\\/_/ " ], ")": [ - " __", - "/\\ `\\", - "\\`\\ \\", - " `\\`\\ \\", + " __ ", + "/\\ `\\ ", + "\\`\\ \\ ", + " `\\`\\ \\ ", " `\\/' \\", " /\\__/", - " \\/_/" + " \\/_/ " ], "/": [ - " __", + " __ ", " /\\_\\", " / / /", - " / / /", - " / / /", - "/ / /", - "\\/_/" + " / / / ", + " / / / ", + "/ / / ", + "\\/_/ " ], ":": [ - "", - " __", - "/\\_\\", - "\\/_/_", + " ", + " __ ", + "/\\_\\ ", + "\\/_/_ ", " /\\_\\", " \\/_/", - "" + " " ], ";": [ - "", - " __", - "/\\_\\", - "\\/_/_", + " ", + " __ ", + "/\\_\\ ", + "\\/_/_ ", " /\\ \\", " \\ \\/", - " \\/" + " \\/ " ], " ": [ " ", diff --git a/fonts/simpleBlock.json b/fonts/simpleBlock.json new file mode 100644 index 00000000..d452fac2 --- /dev/null +++ b/fonts/simpleBlock.json @@ -0,0 +1,516 @@ +{ + "name": "simpleBlock", + "version": "0.0.1", + "homepage": "https://github.com/dominikwilkowski/cfonts", + "colors": 1, + "lines": 7, + "buffer": [ + "", + "", + "", + "", + "", + "", + "" + ], + "letterspace": [ + " ", + " ", + " ", + " ", + " ", + " ", + " " + ], + "chars": { + "A": [ + " ", + " _|_| ", + " _| _|", + " _|_|_|_|", + " _| _|", + " _| _|", + " " + ], + "B": [ + " ", + " _|_|_| ", + " _| _|", + " _|_|_| ", + " _| _|", + " _|_|_| ", + " ", + " " + ], + "C": [ + " ", + " _|_|_|", + " _| ", + " _| ", + " _| ", + " _|_|_|", + " " + ], + "D": [ + " ", + " _|_|_| ", + " _| _|", + " _| _|", + " _| _|", + " _|_|_| ", + " " + ], + "E": [ + " ", + " _|_|_|_|", + " _| ", + " _|_|_| ", + " _| ", + " _|_|_|_|", + " " + ], + "F": [ + " ", + " _|_|_|_|", + " _| ", + " _|_|_| ", + " _| ", + " _| ", + " " + ], + "G": [ + " ", + " _|_|_|", + " _| ", + " _| _|_|", + " _| _|", + " _|_|_|", + " " + ], + "H": [ + " ", + " _| _|", + " _| _|", + " _|_|_|_|", + " _| _|", + " _| _|", + " " + ], + "I": [ + " ", + " _|_|_|", + " _| ", + " _| ", + " _| ", + " _|_|_|", + " " + ], + "J": [ + " ", + " _|", + " _|", + " _|", + " _| _|", + " _|_| ", + " " + ], + "K": [ + " ", + " _| _|", + " _| _| ", + " _|_| ", + " _| _| ", + " _| _|", + " " + ], + "L": [ + " ", + " _| ", + " _| ", + " _| ", + " _| ", + " _|_|_|_|", + " " + ], + "M": [ + " ", + " _| _|", + " _|_| _|_|", + " _| _| _|", + " _| _|", + " _| _|", + " " + ], + "N": [ + "", + " _| _|", + " _|_| _|", + " _| _| _|", + " _| _|_|", + " _| _|", + "" + ], + "O": [ + " ", + " _|_| ", + " _| _|", + " _| _|", + " _| _|", + " _|_| ", + " " + ], + "P": [ + " ", + " _|_|_| ", + " _| _|", + " _|_|_| ", + " _| ", + " _| ", + " " + ], + "Q": [ + " ", + " _|_| ", + " _| _| ", + " _| _|_| ", + " _| _| ", + " _|_| _|", + " " + ], + "R": [ + " ", + " _|_|_| ", + " _| _|", + " _|_|_| ", + " _| _|", + " _| _|", + " " + ], + "S": [ + " ", + " _|_|_|", + " _| ", + " _|_| ", + " _|", + " _|_|_| ", + " " + ], + "T": [ + " ", + " _|_|_|_|_|", + " _| ", + " _| ", + " _| ", + " _| ", + " " + ], + "U": [ + " ", + " _| _|", + " _| _|", + " _| _|", + " _| _|", + " _|_| ", + " " + ], + "V": [ + " ", + " _| _|", + " _| _|", + " _| _|", + " _| _| ", + " _| ", + " " + ], + "W": [ + " ", + " _| _|", + " _| _|", + " _| _| _|", + " _| _| _| ", + " _| _| ", + " " + ], + "X": [ + " ", + " _| _|", + " _| _| ", + " _| ", + " _| _| ", + " _| _|", + " " + ], + "Y": [ + " ", + " _| _|", + " _| _| ", + " _| ", + " _| ", + " _| ", + " " + ], + "Z": [ + " ", + " _|_|_|_|_|", + " _| ", + " _| ", + " _| ", + " _|_|_|_|_|", + " " + ], + "0": [ + " ", + " _| ", + " _| _|", + " _| _|", + " _| _|", + " _| ", + " " + ], + "1": [ + " ", + " _|", + " _|_|", + " _|", + " _|", + " _|", + " " + ], + "2": [ + " ", + " _|_| ", + " _| _|", + " _| ", + " _| ", + " _|_|_|_|", + " " + ], + "3": [ + " ", + " _|_|_| ", + " _|", + " _|_| ", + " _|", + " _|_|_| ", + " " + ], + "4": [ + " ", + " _| _| ", + " _| _| ", + " _|_|_|_|", + " _| ", + " _| ", + " " + ], + "5": [ + " ", + " _|_|_|_|", + " _| ", + " _|_|_| ", + " _|", + " _|_|_| ", + " " + ], + "6": [ + " ", + " _|_|_|", + " _| ", + " _|_|_| ", + " _| _|", + " _|_| ", + " " + ], + "7": [ + " ", + " _|_|_|_|_|", + " _|", + " _| ", + " _| ", + " _| ", + " " + ], + "8": [ + " ", + " _|_| ", + " _| _|", + " _|_| ", + " _| _|", + " _|_| ", + " " + ], + "9": [ + " ", + " _|_| ", + " _| _|", + " _|_|_|", + " _|", + " _|_|_| ", + " " + ], + "!": [ + " ", + " _|", + " _|", + " _|", + " ", + " _|", + " " + ], + "?": [ + " ", + " _|_| ", + " _|", + " _|_| ", + " ", + " _| ", + " " + ], + ".": [ + " ", + " ", + " ", + " ", + " ", + " ", + "_|" + ], + "+": [ + " ", + " _| ", + " _| ", + " _|_|_|_|_|", + " _| ", + " _| ", + " " + ], + "-": [ + " ", + " ", + " ", + " _|_|_|_|_|", + " ", + " ", + " " + ], + "_": [ + " ", + " ", + " ", + " ", + " ", + " _|_|_|_|_|", + " " + ], + "=": [ + " ", + " ", + " _|_|_|_|_|", + " ", + " _|_|_|_|_|", + " ", + " " + ], + "@": [ + " _|_|_|_|_| ", + " _| _|", + " _| _|_|_| _|", + " _| _| _| _|", + " _| _|_|_|_| ", + " _| ", + " _|_|_|_|_|_|" + ], + "#": [ + " ", + " _| _| ", + " _|_|_|_|_|", + " _| _| ", + " _|_|_|_|_|", + " _| _| ", + " " + ], + "#": [ + " _| ", + " _|_|_|", + " _|_| ", + " _|_|", + " _|_|_|", + " _| ", + " " + ], + "%": [ + " ", + " _|_| _|", + " _|_| _| ", + " _| ", + " _| _|_|", + " _| _|_|", + " " + ], + "&": [ + " ", + " _| ", + " _| _| ", + " _|_| _|", + " _| _| ", + " _|_| _|", + " " + ], + "(": [ + " _|", + " _| ", + " _| ", + " _| ", + " _| ", + " _| ", + " _|" + ], + ")": [ + " _| ", + " _|", + " _|", + " _|", + " _|", + " _|", + " _| " + ], + "/": [ + " ", + " _|", + " _| ", + " _| ", + " _| ", + " _| ", + " " + ], + ":": [ + " ", + " ", + "_|", + " ", + "_|", + " ", + " " + ], + ";": [ + " ", + " _|", + " ", + " ", + " _|", + " _| ", + " " + ], + " ": [ + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " " + ] + } +} \ No newline at end of file diff --git a/img/example1.png b/img/example1.png index 064f562e..c0870191 100644 Binary files a/img/example1.png and b/img/example1.png differ diff --git a/index.js b/index.js index d70e7e32..6ef4dccc 100644 --- a/index.js +++ b/index.js @@ -1,173 +1,607 @@ -/* +/*************************************************************************************************************************************************************** + * * cfonts - * https://github.com/dominikwilkowski/cfonts * - * Copyright (c) 2015 Dominik Wilkowski - * Licensed under the MIT license. - */ + * Sexy fonts for the console. (CLI output) + * + * @license https://github.com/dominikwilkowski/cfonts/blob/master/LICENSE GNU GPLv2 + * @author Dominik Wilkowski hi@dominik-wilkowski.com + * @repository https://github.com/dominikwilkowski/cfonts + * + **************************************************************************************************************************************************************/ + +'use strict'; -// 'use strict'; //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Dependencies //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -var fs = require('fs'); -var chalk = require('chalk'); -var changeCase = require('change-case'); +const ChangeCase = require('change-case'); +const WinSize = require('window-size'); +const Chalk = require(`chalk`); +const Fs = require(`fs`); //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Custom functions +// Constructor //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -function colorize($font, OPTIONS, $character) { +const CFonts = (() => { //constructor factory - if($font.colors > 1) { - for(var o = 0, ll = $font.colors; o < ll; o++) { //convert colors - var open = new RegExp('', 'g'); - var close = new RegExp('', 'g'); +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// GetFont, Get and set a selected JSON font-file object into global namespace +// +// @param font {string} The name of the font to be returned +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const GetFont = ( font ) => { + CFonts.debugging.report(`Running GetFont`, 1); - var color = OPTIONS.colors[o] || "white"; + //check selected font exists in preset + if( CFonts.FONTFACES.indexOf( font ) === -1 ) { + CFonts.log.error(`Please use a font from the supported stack:\n${Chalk.green(`[ ${CFonts.FONTFACES.join(' | ')} ]`)}`); - $character = $character.replace(open, chalk.styles[color.toLowerCase()].open); - $character = $character.replace(close, chalk.styles[color.toLowerCase()].close); + process.exit(1); //exit program with failure code } - } - return $character; + //try loading the font file + try { + let fontFile = __dirname + '/fonts/' + font + '.json'; //build font path + let FONTFACE = JSON.parse( Fs.readFileSync(fontFile, 'utf8') ); //read font file -} + CFonts.debugging.report(`GetFont: Fontface path selected: "${fontFile}"`, 2); + CFonts.FONTFACE = FONTFACE; + } + catch( error ) { + CFonts.debugging.error(`Font file for "${font}" errored out: ${error}`, 2); -function equalWidth($character) { - var charWidth = 0; + CFonts.log.error(`Font file for "${font}" failed to connect to us.\nTry reinstalling this package.`); - for(var w = $character.length - 1; w >= 0; w--) { - $char = $character[w].replace(/(<([^>]+)>)/ig, ''); + process.exit(1); //exit program with failure code + } + }; + + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// CharLength, return the max width of a character by looking at its longest line +// +// @param character {array} The character array from the font face object +// +// @return {integer} The length of a longest line in a charater +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const CharLength = ( character ) => { + CFonts.debugging.report(`Running CharLength`, 1); + + let charWidth = 0; - if( $char.length > charWidth ) { - charWidth = $char.length; + for(let i = 0; i < CFonts.FONTFACE.lines; i++) { + let char = character[ i ].replace(/(<([^>]+)>)/ig, ''); //get character and strip color infos + + if( char.length > charWidth ) { + charWidth = char.length; //assign only largest + } + }; + + if( charWidth === 0 && CFonts.OPTIONS.letterSpacing > 0 ) { + CFonts.debugging.report(`CharLength: Adding space to letter spacing`, 1); + + charWidth = 1; + } + + return charWidth; + }; + + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// AddLine, Add a new line to the output array +// +// @param output {array} The output array the line shall be appended to +// +// @return {array} The output array with new line +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const AddLine = ( output ) => { + CFonts.debugging.report(`Running AddLine`, 1); + + let lineHeight = CFonts.OPTIONS.lineHeight; + if( output.length === 0 ) { + lineHeight = 0; + } + + let lines = CFonts.FONTFACE.lines + output.length + lineHeight; + let length = output.length; + + for(let i = length; i < lines; i++) { + let index = i - length; + + if( index > lineHeight ) { + output[ i ] = CFonts.FONTFACE.buffer[ ( index - lineHeight ) ]; + } + else { + output[ i ] = ''; + } } + + return output; }; - return charWidth; -} + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// AddChar, Add a new character to the output array +// +// @param CHAR {string} The character to be added +// @param output {array} The output array the line shall be appended to +// +// @return {array} The output array with new line +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const AddChar = ( CHAR, output ) => { + CFonts.debugging.report(`Running AddChar with "${CHAR}"`, 1); + + let lines = output.length - CFonts.FONTFACE.lines; //last line is CFonts.FONTFACE.lines tall and is located at the bottom of the output array + + for(let i = lines; i < output.length; i++) { //iterate over last line + let index = i - lines; + + output[ i ] += Colorize( CFonts.FONTFACE.chars[ CHAR ][ index ] ); + } + + return output; + }; //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Main logic +// Private function +// AddLetterSpacing, Add letter spacing for the next character +// +// @param output {array} The output array the line shall be appended to +// +// @return {array} The output array with space //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -function cfonts($SETTINGS) { + const AddLetterSpacing = ( output ) => { + CFonts.debugging.report(`Running AddLetterSpacing`, 1); + + let lines = output.length - CFonts.FONTFACE.lines; //last line is CFonts.FONTFACE.lines tall and is located at the bottom of the output array + + for(let i = lines; i < output.length; i++) { //iterate over last line + let index = i - lines; + let space = Colorize( CFonts.FONTFACE.letterspace[ index ] ) + + if( space.length === 0 && CFonts.OPTIONS.letterSpacing > 0 ) { + CFonts.debugging.report(`AddLetterSpacing: Adding space to letter spacing`, 1); + + space = ' '; + } + + output[ i ] += space.repeat( CFonts.OPTIONS.letterSpacing ); + } - //options - var OPTIONS = { - font: $SETTINGS.font || 'block', //define the font face - colors: $SETTINGS.colors || [], //define all colors - background: $SETTINGS.background || 'Black', //define the background color - letterSpacing: $SETTINGS.letterSpacing === undefined ? 1 : $SETTINGS.letterSpacing, //define letter spacing - space: $SETTINGS.space === undefined ? true : $SETTINGS.space, //define if the output text should have empty lines on top and on the bottom - maxLength: $SETTINGS.maxLength || 10 //define how many character can be on one line + return output; }; - var $input = $SETTINGS.text; //text to be converted - var FONTS = '*console*block*simple*3d*simple3d*'; //all supported font files - var $font = {}; //the font object - OPTIONS.background = changeCase.upperCaseFirst(OPTIONS.background); //background color case convertion +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// Colorize, replace placeholders with color information +// +// @param character {string} The string to be converted +// +// @return {string} The character with color information for CLI +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const Colorize = ( character ) => { + CFonts.debugging.report(`Running Colorize`, 1); - if( FONTS.indexOf(OPTIONS.font) && OPTIONS.font !== 'console') { //getting font file + if( CFonts.FONTFACE.colors > 1 && character !== undefined ) { + for(let i = 0; i < CFonts.FONTFACE.colors; i++) { //convert all colors + let open = new RegExp(``, 'g'); + let close = new RegExp(``, 'g'); - var fontFile = __dirname + '/fonts/' + OPTIONS.font + '.json'; - $font = JSON.parse( fs.readFileSync(fontFile, 'utf8') ); + let color = CFonts.OPTIONS.colors[ i ] || 'white'; - } - else if(OPTIONS.font === 'console') { //writing in console text + character = character.replace( open, Chalk.styles[ color.toLowerCase() ].open ); + character = character.replace( close, Chalk.styles[ color.toLowerCase() ].close ); + } + } - var $write = $input.replace('|', "\n"); - $font['colors'] = 0; + return character; + }; - } - else { //throw error if the font does not exist - console.log("\n" + ' Please use a font from the support stack: ' + - chalk.styles.green.open + '[ console | block | simple | 3d | simple3d ]' + chalk.styles.green.close + "\n"); - process.exit(1); - } +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// AlignText, calculate the spaces to be added to the left of each line to align them either center or right +// +// @param output {array} The output array the line shall be appended to +// @param lineLength {integer} the current line length +// +// @return {array} The output array with space added on the left for alignment +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const AlignText = ( output, lineLength ) => { + CFonts.debugging.report(`Running AlignText`, 1); - if(OPTIONS.font !== 'console') { //only render a font if it isn't the console font + let space = 0; - var $output = []; - for(var i = 0, length = $font.lines; i < length; i++) { //create first lines with buffer - $output[i] = $font.buffer[i]; + if( CFonts.OPTIONS.align === 'center' ) { //calculate the size for center alignment + space = Math.floor(( WinSize.width - lineLength ) / 2); + + CFonts.debugging.report(`AlignText: Center lineLength: ${lineLength}, WinSize.width: ${WinSize.width}, space: ${space}`, 2); } - var $charLength = 0; //use for max character per line - var $line = 0; //start with line 0 - var $letterSpacing = ""; + if( CFonts.OPTIONS.align === 'right' ) { //calculate the size for right alignment + space = WinSize.width - lineLength; - for(var i = 0, length = OPTIONS.letterSpacing; i < length; i++) { //letter spacing - $letterSpacing += colorize($font, OPTIONS, $font.letterspace[i]); + CFonts.debugging.report(`AlignText: Right lineLength: ${lineLength}, WinSize.width: ${WinSize.width}, space: ${space}`, 2); } - for(var i = 0, length = $input.length; i < length; i++) { //iterate through the message + if( space > 0 ) { //only add if there is something to add + let lines = output.length - CFonts.FONTFACE.lines; //last line is CFonts.FONTFACE.lines tall and is located at the bottom of the output array + space = ' '.repeat( space ); + + for(let i = lines; i < output.length; i++) { //iterate over last line + output[ i ] = space + output[ i ]; + } + } - var $char = $input.charAt(i).toUpperCase(); //only upper case is supported at this time + return output; + }; - if($charLength >= OPTIONS.maxLength || $char === "|") { //jump to next line after OPTIONS.maxLength characters or when line break is found - $charLength = 0; - $line += $font.lines; - for(var l = $line, lll = $line + $font.lines; l < lll; l++) { //create new lines with buffer - $output[l] = $font.buffer[ (l - $line) ]; +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + return { +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// settings +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + DEBUG: false, //Debug setting + DEBUGLEVEL: 2, //Debug level setting + COLORS: [ //All allowed font colors + 'black', + 'red', + 'green', + 'yellow', + 'blue', + 'magenta', + 'cyan', + 'white', + 'gray', + ], + BGCOLORS: [ //All allowed background colors + 'black', + 'red', + 'green', + 'yellow', + 'blue', + 'magenta', + 'cyan', + 'white', + ], + ALIGNMENT: [ //All allowed alignment options + 'left', + 'center', + 'right', + ], + FONTFACES: [ //All allowed fonts + 'console', + 'block', + 'simpleBlock', + 'simple', + '3d', + 'simple3d', + ], + FONTFACE: {}, //Font face object to be filled with selected fontface + OPTIONS: {}, //User options + + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Public function +// say, main method to write out your string +// +// @param INPUT {string} The string you want to write out +// @param SETTINGS {object} (optional) Settings object +// font {string} Font face, Default 'block' +// align {string} Text alignment, Default: 'left' +// colors {array} Colors for font, Default: [] +// background {string} Chalk color string for background, Default 'Black' +// letterSpacing {integer} Space between letters, Default: set by selected font face +// lineHeight {integer} Space between lines, Default: 1 +// space {boolean} Output space before and after output, Default: true +// maxLength {integer} Maximum amount of characters per line, Default width of console window +// +// @return {string} CLI output of INPUT +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + say: ( INPUT = '', SETTINGS = {} ) => { + CFonts.debugging.report(`Running say`, 1); + + let write = ''; //output to be build + CFonts.OPTIONS = { //SETTINGS and defaults + font: SETTINGS.font || 'block', + align: SETTINGS.align || 'left', + colors: SETTINGS.colors || [], + background: ChangeCase.upperCaseFirst( SETTINGS.background ) || 'Black', + letterSpacing: SETTINGS.letterSpacing === undefined ? 1 : SETTINGS.letterSpacing, + lineHeight: SETTINGS.lineHeight === undefined ? 1 : parseInt( SETTINGS.lineHeight ), + space: SETTINGS.space === undefined ? true : SETTINGS.space, + maxLength: SETTINGS.maxLength || 0, + }; + + if( INPUT === undefined || INPUT === '' ) { + CFonts.log.error(`Please provide text to convert`); + + process.exit(1); //exit program with failure code + } + + for( let color in CFonts.OPTIONS.colors ) { //check color usage + if( CFonts.COLORS.indexOf( CFonts.OPTIONS.colors[ color ] ) === -1 ) { + CFonts.log.error( + `"${Chalk.red( CFonts.OPTIONS.colors[ color ] )}" is not a valid font color option.\n` + + `Please use a color from the supported stack:\n${Chalk.green(`[ ${CFonts.COLORS.join(' | ')} ]`)}` + ); + + process.exit(1); //exit program with failure code } } - if( $font.chars[ $char ] !== undefined) { //make sure this character exists in the font - $charLength++; //counting all printed characters + if( CFonts.BGCOLORS.indexOf( CFonts.OPTIONS.background.toLowerCase() ) === -1 ) { + CFonts.log.error( + `"${Chalk.red( CFonts.OPTIONS.background )}" is not a valid background option.\n` + + `Please use a color from the supported stack:\n${Chalk.green(`[ ${CFonts.BGCOLORS.join(' | ')} ]`)}` + ); - var charWidth = equalWidth( $font.chars[ $char ] ); + process.exit(1); //exit program with failure code + } + + if( CFonts.ALIGNMENT.indexOf( CFonts.OPTIONS.align ) === -1 ) { + CFonts.log.error( + `"${Chalk.red( CFonts.OPTIONS.align )}" is not a valid alignment option.\n` + + `Please use an alignment option from the supported stack:\n${Chalk.green(`[ ${CFonts.ALIGNMENT.join(' | ')} ]`)}` + ); + + process.exit(1); //exit program with failure code + } - for(var c = 0, l = $font.lines; c < l; c++) { //iterate over font face lines - var $character = $font.chars[ $char ][c]; + //log OPTIONS for debugging + if( CFonts.DEBUG ) { + let outOption = `OPTIONS:\n Text: ${INPUT}`; + + for( let key in CFonts.OPTIONS ) { + outOption += `\n Options.${key}: ${CFonts.OPTIONS[ key ]}`; + } + + CFonts.debugging.report( outOption, 2 ); + } - if( $character.length < charWidth ) { - for(var w = (charWidth - $character.length) - 1; w >= 0; w--) { - $character += ' '; - }; + + if( CFonts.OPTIONS.font === 'console' ) { //console fontface is pretty easy to process + let color = CFonts.OPTIONS.colors[0].toLowerCase() || 'white'; //font color + let lines = INPUT.split('|'); //each line + let output = [] + + CFonts.FONTFACE.lines = 1; + + for(let line in lines) { + let length = lines[ line ].length; + + lines[ line ] = Chalk[ color ]( lines[ line ] ); + output.push( lines[ line ] ); + + output = AlignText( output, length ); //calculate alignment based on lineLength + } + + + write = output.join(`\n`); //convert to one line + } + else { //all other fontfaces need the font-file and some more work + GetFont( CFonts.OPTIONS.font ); //get fontface object and make it global + + //setting the letterspacing preference from font face if there is no user overwrite + if( SETTINGS.letterSpacing === undefined ) { + CFonts.debugging.report(`Looking up letter spacing from font face`, 1); + + let width = 0; + + for( let i in CFonts.FONTFACE.letterspace ) { + let char = CFonts.FONTFACE.letterspace[ i ].replace(/(<([^>]+)>)/ig, ''); //get character and strip color infos + + if( width < char.length ) { + width = char.length; + } } - $character = colorize($font, OPTIONS, $character); + CFonts.debugging.report(`Letter spacing set to font face default: "${width}"`, 2); + CFonts.OPTIONS.letterSpacing = width; + } + + let output = AddLine( [] ); //create first lines with buffer + let lineLength = CharLength( CFonts.FONTFACE.buffer ); //count each output character per line and start with the buffer + let maxChars = 0; //count each character we print for maxLength option + + output = AddLetterSpacing( output ); //add letter spacing to the beginning + lineLength += CharLength( CFonts.FONTFACE.letterspace ) * CFonts.OPTIONS.letterSpacing; //count the space for the letter spacing - $output[ ($line + c) ] += $character + $letterSpacing; //save output per character + for(let i = 0; i < INPUT.length; i++) { //iterate through the message + + let CHAR = INPUT.charAt( i ).toUpperCase(); //the current character we convert, only upper case is supported at this time + + if( CFonts.FONTFACE.chars[ CHAR ] === undefined && CHAR !== `|` ) { //make sure this character exists in the font + CFonts.debugging.error(`Character not found in font: "${CHAR}"`, 2); //fail silently + } + else { + CFonts.debugging.report(`Character found in font: "${CHAR}"`, 2); + + let lastLineLength = lineLength; //we need the lineLength for alignment before we look up if the next char fits + + if( CHAR !== `|` ) { //what will the line length be if we add the next char? + lineLength += CharLength( CFonts.FONTFACE.chars[ CHAR ] ); //get the length of this character + lineLength += CharLength( CFonts.FONTFACE.letterspace ) * CFonts.OPTIONS.letterSpacing; //new line, new line length + } + + //jump to next line after OPTIONS.maxLength characters or when line break is found or the console windows would has ran out of space + if(maxChars >= CFonts.OPTIONS.maxLength && CFonts.OPTIONS.maxLength != 0 || CHAR === `|` || lineLength > WinSize.width) { + CFonts.debugging.report( + `NEWLINE: maxChars: ${maxChars}, ` + + `CFonts.OPTIONS.maxLength: ${CFonts.OPTIONS.maxLength}, ` + + `CHAR: ${CHAR}, ` + + `lineLength: ${lineLength}, ` + + `WinSize.width: ${WinSize.width} `, 2 + ); + + output = AlignText( output, lastLineLength ); //calculate alignment based on lineLength + + lineLength = CharLength( CFonts.FONTFACE.buffer ); //new line: new line length + lineLength += CharLength( CFonts.FONTFACE.letterspace ) * CFonts.OPTIONS.letterSpacing; //each new line starts with letter spacing + if( CHAR !== `|` ) { //if this is a character + lineLength += CharLength( CFonts.FONTFACE.chars[ CHAR ] ); //get the length of this character + lineLength += CharLength( CFonts.FONTFACE.letterspace ) * CFonts.OPTIONS.letterSpacing; //add letter spacing at the end + } + maxChars = 0; //new line, new maxLength goal + + output = AddLine( output ); //adding new line + output = AddLetterSpacing( output ); //add letter spacing to the beginning + } + + CFonts.debugging.report(`lineLength at: "${lineLength}"`, 2); + + if( CHAR !== `|` ) { + maxChars++; //counting all printed characters + output = AddChar( CHAR, output ); //add new character + output = AddLetterSpacing( output ); //add letter spacing + } + } } + + output = AlignText( output, lineLength ); //alignment last line + + write = output.join(`\n`); //convert to one line } - } + if( CFonts.FONTFACE.colors <= 1 ) { //add text color if only one + let color = CFonts.OPTIONS.colors[0] || `white`; - var $write = $output.join("\n"); //convert to one line - } + write = Chalk[ color.toLowerCase() ]( write ); + } - if($font.colors <= 1) { //add text color if only one - var color = OPTIONS.colors[0] || "white"; + if( CFonts.OPTIONS.space ) { //add space + write = `\n\n` + write + `\n\n`; + } - $write = chalk.styles[color.toLowerCase()].open + $write + chalk.styles[color.toLowerCase()].close; - } + console.log( Chalk[ 'bg' + CFonts.OPTIONS.background ]( write ) ); //write out + }, - if(OPTIONS.space) { //add space - $write = "\n\n" + $write + "\n\n"; - } + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Debugging prettiness +// +// debugging, Print debug message that will be logged to console. +// +// @method headline Return a headline preferably at the beginning of your app +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +// +// @method report Return a message to report starting a process +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +// +// @method error Return a message to report an error +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +// +// @method interaction Return a message to report an interaction +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +// +// @method send Return a message to report data has been sent +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +// +// @method received Return a message to report data has been received +// @param [text] {string} The sting you want to log +// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99 +// @return [ansi] {output} +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + debugging: { + + headline: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.bold(' \u2611 ')} ${text}`) + ); + } + }, + + report: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.bold.green(' \u2611 ')} ${Chalk.black(`${text} `)}`) + ); + } + }, + + error: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.red(' \u2612 ')} ${Chalk.black(`${text} `)}`) + ); + } + }, + + interaction: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.blue(' \u261C ')} ${Chalk.black(`${text} `)}`) + ); + } + }, + + send: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.bold.cyan(' \u219D ')} ${Chalk.black(`${text} `)}`) + ); + } + }, + + received: ( text, level = 99 ) => { + if( CFonts.DEBUG && level >= CFonts.DEBUGLEVEL ) { + console.log( + Chalk.bgWhite(`\n${Chalk.bold.cyan(' \u219C ')} ${Chalk.black(`${text} `)}`) + ); + } + } + }, - console.log( chalk['bg' + OPTIONS.background]( $write ) ); //write out +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Logging prettiness +// +// log, Print error message to console. +// +// @method error Return a headline preferably at the beginning of your app +// @param [text] {string} The sting you want to log +// @return [ansi] {output} +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + log: { + error: ( text ) => { + text = text.replace(/(?:\r\n|\r|\n)/g, '\n '); //indent each line + + console.log(`\n ${Chalk.bold.red('Ouch:')} ${text}\n`); + }, + }, + } -} +})(); //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Module export //-------------------------------------------------------------------------------------------------------------------------------------------------------------- -module.exports = cfonts; \ No newline at end of file +module.exports = CFonts; \ No newline at end of file diff --git a/package.json b/package.json index 9e1e1fdc..15388b02 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,18 @@ { "name": "cfonts", "description": "Sexy fonts for the console", - "version": "0.0.13", + "version": "1.0.0", "homepage": "https://github.com/dominikwilkowski/cfonts", "author": { "name": "Dominik Wilkowski", "email": "Hi@Dominik-Wilkowski.com", "url": "http://dominik-wilkowski.com/" }, + "contributors": { + "name": "Dominik Wilkowski", + "email": "Hi@Dominik-Wilkowski.com", + "url": "http://dominik-wilkowski.com/" + }, "repository": { "type": "git", "url": "git://github.com/dominikwilkowski/cfonts.git" @@ -22,21 +27,22 @@ } ], "engines": { - "node": ">= 0.8.0" + "node": ">=6.0.0" }, "scripts": { "test": "grunt test" }, "devDependencies": { - "grunt": "~0.4.5", - "grunt-contrib-jshint": "^0.9.2", - "grunt-exec": "^0.4.6" + "grunt": "~1.0.1", + "grunt-contrib-jshint": "^1.0.0", + "grunt-exec": "^1.0.0" }, "peerDependencies": {}, "dependencies": { - "change-case": "^2.1.6", "chalk": "^1.0.0", - "commander": "^2.6.0" + "change-case": "^3.0.0", + "commander": "^2.6.0", + "window-size": "^0.2.0" }, "keywords": [ "font", @@ -51,5 +57,5 @@ "bin": { "cfonts": "./bin/font.js" }, - "license": "GNU-GPL" -} + "license": "GNU-GPLv2" +} \ No newline at end of file