cfonts-node 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cfonts-node might be problematic. Click here for more details.

Files changed (46) hide show
  1. package/LICENSE +339 -0
  2. package/README.md +484 -0
  3. package/fonts/3d.json +658 -0
  4. package/fonts/block.json +481 -0
  5. package/fonts/chrome.json +304 -0
  6. package/fonts/grid.json +481 -0
  7. package/fonts/huge.json +776 -0
  8. package/fonts/pallet.json +481 -0
  9. package/fonts/shade.json +599 -0
  10. package/fonts/simple.json +363 -0
  11. package/fonts/simple3d.json +540 -0
  12. package/fonts/simpleBlock.json +540 -0
  13. package/fonts/slick.json +481 -0
  14. package/fonts/tiny.json +245 -0
  15. package/package.json +117 -0
  16. package/src/AddChar.js +51 -0
  17. package/src/AddLetterSpacing.js +59 -0
  18. package/src/AddLine.js +58 -0
  19. package/src/AddShortcuts.js +40 -0
  20. package/src/AlignText.js +68 -0
  21. package/src/Chalk.js +40 -0
  22. package/src/CharLength.js +55 -0
  23. package/src/CheckInput.js +192 -0
  24. package/src/CleanInput.js +47 -0
  25. package/src/Color.js +118 -0
  26. package/src/Colorize.js +84 -0
  27. package/src/Config.js +1 -0
  28. package/src/Debugging.js +108 -0
  29. package/src/DisplayHelp.js +50 -0
  30. package/src/DisplayVersion.js +31 -0
  31. package/src/GetFirstCharacterPosition.js +38 -0
  32. package/src/GetFont.js +49 -0
  33. package/src/GetLongestLine.js +31 -0
  34. package/src/Gradient.js +549 -0
  35. package/src/Log.js +42 -0
  36. package/src/Options.js +162 -0
  37. package/src/ParseArgs.js +82 -0
  38. package/src/RemoveChar.js +0 -0
  39. package/src/Render.js +257 -0
  40. package/src/RenderConsole.js +102 -0
  41. package/src/Say.js +50 -0
  42. package/src/Size.js +42 -0
  43. package/src/UpperCaseFirst.js +35 -0
  44. package/src/bin.js +16 -0
  45. package/src/constants.js +255 -0
  46. package/src/index.js +98 -0
package/src/Log.js ADDED
@@ -0,0 +1,42 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * Log
12
+ * Logging prettiness
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const { Chalk } = require('./Chalk.js');
19
+
20
+
21
+ /**
22
+ * Logging prettiness
23
+ *
24
+ * @type {object}
25
+ */
26
+ const Log = {
27
+ /**
28
+ * Print error message to console.
29
+ *
30
+ * @param {string} text - The sting you want to log
31
+ */
32
+ error: ( text ) => {
33
+ text = text.replace( /(?:\r\n|\r|\n)/g, '\n ' ); // indent each line
34
+
35
+ console.error(`\n ${ Chalk.bold.red('Ouch:') } ${ text }\n`);
36
+ },
37
+ };
38
+
39
+
40
+ module.exports = exports = {
41
+ Log,
42
+ };
package/src/Options.js ADDED
@@ -0,0 +1,162 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * Options
12
+ * Merge user settings with default options
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const {
19
+ COLORS,
20
+ BGCOLORS,
21
+ FONTFACES,
22
+ } = require('./constants.js');
23
+
24
+
25
+ /**
26
+ * The options store with getter and setter methods
27
+ *
28
+ * @type {Object}
29
+ */
30
+ const Options = {
31
+ store: {},
32
+
33
+ reset() {
34
+ const defaults = {
35
+ font: 'block',
36
+ align: 'left',
37
+ colors: [],
38
+ background: 'transparent',
39
+ letterSpacing: 1,
40
+ lineHeight: 1,
41
+ space: true,
42
+ maxLength: 0,
43
+ gradient: false,
44
+ independentGradient: false,
45
+ transitionGradient: false,
46
+ env: 'node',
47
+ };
48
+
49
+ this.store = { ...defaults }; // cloning
50
+ },
51
+
52
+ /**
53
+ * Get the current options
54
+ *
55
+ * @return {object} - Our options as hey are stored in our object
56
+ */
57
+ get get() {
58
+ return this.store;
59
+ },
60
+
61
+ /**
62
+ * Merge settings into our options object
63
+ *
64
+ * @param {object} options - The settings object
65
+ * @param {string} options.font - Font face, Default 'block'
66
+ * @param {string} options.align - Text alignment, Default: 'left'
67
+ * @param {array} options.colors - Colors for font, Default: []
68
+ * @param {string} options.background - Chalk color string for background, Default 'Black'
69
+ * @param {string} options.backgroundColor - Alias for background
70
+ * @param {number} options.letterSpacing - Space between letters, Default: set by selected font face
71
+ * @param {number} options.lineHeight - Space between lines, Default: 1
72
+ * @param {boolean} options.space - Output space before and after output, Default: true
73
+ * @param {number} options.maxLength - Maximum amount of characters per line, Default width of console window
74
+ * @param {(string|array|boolean)} options.gradient - Gradient color pair, Default: false
75
+ * @param {boolean} options.independentGradient - A switch to calculate gradient per line or not
76
+ * @param {boolean} options.transitionGradient - A switch for transition gradients
77
+ * @param {string} options.env - The environment we run cfonts in
78
+ * @param {object} options.allowedColors - All allowed font colors
79
+ * @param {object} options.allowedBG - All allowed background colors
80
+ * @param {object} options.allowedFont - All allowed fontfaces
81
+ */
82
+ set set({
83
+ font = '',
84
+ align,
85
+ colors,
86
+ background,
87
+ backgroundColor,
88
+ letterSpacing,
89
+ lineHeight,
90
+ space,
91
+ maxLength,
92
+ gradient,
93
+ independentGradient,
94
+ transitionGradient,
95
+ env,
96
+ allowedColors = COLORS,
97
+ allowedBG = BGCOLORS,
98
+ allowedFont = FONTFACES,
99
+ }) {
100
+ this.store.font = font !== ''
101
+ ? allowedFont[ font.toLowerCase() ] || font
102
+ : this.store.font;
103
+
104
+ this.store.align = align !== undefined
105
+ ? align.toLowerCase()
106
+ : this.store.align;
107
+
108
+ this.store.colors = Array.isArray( colors )
109
+ ? colors.map( color => allowedColors[ color.toLowerCase() ] || color )
110
+ : this.store.colors;
111
+
112
+ const bg = backgroundColor || background;
113
+ this.store.background = bg !== undefined
114
+ ? allowedBG[ bg.toLowerCase() ] || bg
115
+ : this.store.background;
116
+
117
+ this.store.letterSpacing = letterSpacing !== undefined
118
+ ? parseInt( letterSpacing.toString() )
119
+ : font.toLowerCase() === 'console'
120
+ ? 0
121
+ : this.store.letterSpacing;
122
+
123
+ this.store.lineHeight = lineHeight !== undefined
124
+ ? parseInt( lineHeight.toString() )
125
+ : font.toLowerCase() === 'console'
126
+ ? 0
127
+ : this.store.lineHeight;
128
+
129
+ this.store.space = typeof space === 'boolean'
130
+ ? space
131
+ : this.store.space;
132
+
133
+ this.store.maxLength = maxLength !== undefined
134
+ ? maxLength
135
+ : this.store.maxLength;
136
+
137
+ this.store.gradient = gradient !== undefined && typeof gradient !== 'boolean'
138
+ ? Array.isArray( gradient )
139
+ ? gradient
140
+ : gradient.split(',')
141
+ : gradient === false
142
+ ? false
143
+ : this.store.gradient;
144
+
145
+ this.store.independentGradient = independentGradient !== undefined
146
+ ? independentGradient
147
+ : this.store.independentGradient;
148
+
149
+ this.store.transitionGradient = transitionGradient !== undefined
150
+ ? transitionGradient
151
+ : this.store.transitionGradient;
152
+
153
+ this.store.env = env !== undefined
154
+ ? env
155
+ : this.store.env;
156
+ },
157
+ };
158
+
159
+
160
+ module.exports = exports = {
161
+ Options,
162
+ };
@@ -0,0 +1,82 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * ParseArgs
12
+ * Parse cli arguments into a nice object
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const { AddShortcuts } = require('./AddShortcuts.js');
19
+ const { CLIOPTIONS } = require('./constants.js');
20
+ const { Debugging } = require('./Debugging.js');
21
+
22
+
23
+ /**
24
+ * Parse cli arguments into a nice object
25
+ *
26
+ * @param {object} inputOptions - All possible options registered for this app
27
+ * @param {array} inputArgs - The arguments given to us in our cli, default: process.argv
28
+ *
29
+ * @return {object} - An object of all options with at least their default values
30
+ */
31
+ const ParseArgs = ( inputOptions = CLIOPTIONS, inputArgs = process.argv ) => {
32
+ const parsedArgs = {
33
+ text: inputArgs[ 2 ],
34
+ };
35
+
36
+ // create defaults
37
+ Object.keys( inputOptions ).forEach( option => {
38
+ const name = option.replace( '--', '' );
39
+
40
+ parsedArgs[ name ] = inputOptions[ option ].default;
41
+ });
42
+
43
+ if( inputArgs[ 2 ] === '--help' || inputArgs[ 2 ] === '-h' ) {
44
+ parsedArgs.help = true;
45
+ }
46
+
47
+ if( inputArgs[ 2 ] === '--version' || inputArgs[ 2 ] === '-v' ) {
48
+ parsedArgs.version = true;
49
+ }
50
+
51
+ const args = inputArgs.splice( 3 ); // the first two are node specific, the third is our text
52
+
53
+ const options = AddShortcuts( inputOptions );
54
+
55
+ for( let index = 0; args.length > index; index ++ ) {
56
+ const option = options[ args[ index ] ];
57
+
58
+ if( option ) {
59
+ const name = option._name.replace( '--', '' );
60
+
61
+ if( option.options !== undefined ) {
62
+ index ++;
63
+ const value = args[ index ];
64
+
65
+ parsedArgs[ name ] = value;
66
+ }
67
+ else {
68
+ parsedArgs[ name ] = true;
69
+ }
70
+ }
71
+ else {
72
+ Debugging.report( `The cli argument ${ args[ index ] } was not found and ignored`, 2 );
73
+ }
74
+ }
75
+
76
+ return parsedArgs;
77
+ };
78
+
79
+
80
+ module.exports = exports = {
81
+ ParseArgs,
82
+ };
Binary file
package/src/Render.js ADDED
@@ -0,0 +1,257 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * Render
12
+ * Main method to get the ANSI output for a string
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const { AddLetterSpacing } = require('./AddLetterSpacing.js');
19
+ const { RenderConsole } = require('./RenderConsole.js');
20
+ const { Debugging, DEBUG } = require('./Debugging.js');
21
+ const { PaintGradient } = require('./Gradient.js');
22
+ const { CharLength } = require('./CharLength.js');
23
+ const { CheckInput } = require('./CheckInput.js');
24
+ const { CleanInput } = require('./CleanInput.js');
25
+ const { AlignText } = require('./AlignText.js');
26
+ const { AddLine } = require('./AddLine.js');
27
+ const { AddChar } = require('./AddChar.js');
28
+ const { Options } = require('./Options.js');
29
+ const { GetFont } = require('./GetFont.js');
30
+ const { CHARS } = require('./constants.js');
31
+ const { Color } = require('./Color.js');
32
+ const { Size } = require('./Size.js');
33
+ const { Log } = require('./Log.js');
34
+
35
+
36
+ /**
37
+ * Main method to get the ANSI output for a string
38
+ *
39
+ * @param {string} input - The string you want to write out
40
+ * @param {object} SETTINGS - Settings object
41
+ * @param {boolean} debug - A flag to enable debug mode
42
+ * @param {number} debuglevel - The debug level we want to show
43
+ * @param {object} size - The size of the terminal as an object, default: Size
44
+ * @param {number} size.width - The width of the terminal
45
+ * @param {number} size.height - The height of the terminal
46
+ *
47
+ * @typedef {(object|boolean)} ReturnObject
48
+ * @property {string} string - The pure string for output with all line breaks
49
+ * @property {array} array - Each line of output in an array
50
+ * @property {number} lines - The number of lines
51
+ * @property {object} options - All options used
52
+ *
53
+ * @return {ReturnObject} - CLI output of INPUT to be consoled out
54
+ */
55
+ const Render = ( input, SETTINGS = {}, debug = DEBUG.enabled, debuglevel = DEBUG.level, size = Size ) => {
56
+ Debugging.report(`Running render`, 1);
57
+
58
+ DEBUG.enabled = debug;
59
+ DEBUG.level = debuglevel;
60
+
61
+ const INPUT = CleanInput( input, CHARS );
62
+ Options.reset();
63
+ Options.set = SETTINGS;
64
+ const OPTIONS = Options.get;
65
+
66
+ let output = []; // for output where each line is an output line
67
+ let lines = 0; // for counting each line
68
+ let FONTFACE = {}; // scoping the fontface object higher for fonts with just one color
69
+
70
+ const _isGoodHuman = CheckInput( INPUT, OPTIONS.font, OPTIONS.colors, OPTIONS.background, OPTIONS.align, OPTIONS.gradient, OPTIONS.transitionGradient, OPTIONS.env );
71
+ if( !_isGoodHuman.pass ) {
72
+ Log.error( _isGoodHuman.message );
73
+
74
+ return false;
75
+ }
76
+
77
+ // the gradient option supersedes the color options
78
+ if( OPTIONS.gradient ) {
79
+ OPTIONS.colors = [];
80
+ }
81
+
82
+
83
+ // display an overview of options if debug flag is enabled
84
+ if( DEBUG.enabled ) {
85
+ let outOption = `OPTIONS:\n Text: ${ INPUT }`;
86
+
87
+ for( let key in OPTIONS ) {
88
+ outOption += `\n Options.${ key }: ${ OPTIONS[ key ] }`;
89
+ }
90
+
91
+ Debugging.report( outOption, 3 );
92
+ }
93
+
94
+
95
+ if( OPTIONS.env === 'browser' ) {
96
+ size = { ...size }; // we clone so we don't make changes to this object across multiple instances
97
+ size.width = OPTIONS.maxLength === 0 ? 999999999999 : OPTIONS.maxLength;
98
+ }
99
+
100
+
101
+ if( OPTIONS.font === 'console' ) { // console fontface is pretty easy to process
102
+ FONTFACE = {
103
+ colors: 1,
104
+ lines: 1,
105
+ };
106
+
107
+ const consoleOutput = RenderConsole( INPUT, OPTIONS, size );
108
+
109
+ output = consoleOutput.output;
110
+ lines = consoleOutput.lines;
111
+ }
112
+ else { // all other fontfaces need the font-file and some more work
113
+ FONTFACE = GetFont( OPTIONS.font );
114
+
115
+ if( !FONTFACE ) {
116
+ Log.error( `Font file for the font "${ OPTIONS.font }" could not be found.\nTry reinstalling this package.` );
117
+
118
+ return false;
119
+ }
120
+
121
+ // setting the letterspacing preference from font face if there is no user overwrite
122
+ if( SETTINGS.letterSpacing === undefined ) {
123
+ Debugging.report( `Looking up letter spacing from font face`, 1 );
124
+
125
+ let width = 0;
126
+
127
+ FONTFACE.letterspace.forEach( item => {
128
+ let char = item.replace( /(<([^>]+)>)/ig, '' ); // get character and strip color infos
129
+
130
+ if( width < char.length ) {
131
+ width = char.length;
132
+ }
133
+ });
134
+
135
+ Debugging.report(`Letter spacing set to font face default: "${ width }"`, 2);
136
+ OPTIONS.letterSpacing = width;
137
+ }
138
+
139
+ let lineLength = CharLength( FONTFACE.buffer, FONTFACE.lines, OPTIONS ); // count each output character per line and start with the buffer
140
+ let maxChars = 0; // count each character we print for maxLength option
141
+
142
+ output = AddLine( [], FONTFACE.lines, FONTFACE.buffer, OPTIONS.lineHeight ); // create first lines with buffer
143
+ lines ++;
144
+
145
+ output = AddLetterSpacing( output, FONTFACE.lines, FONTFACE.letterspace, FONTFACE.colors, OPTIONS.colors, OPTIONS.letterSpacing ); // add letter spacing to the beginning
146
+ lineLength += CharLength( FONTFACE.letterspace, FONTFACE.lines, OPTIONS ) * OPTIONS.letterSpacing; // count the space for the letter spacing
147
+
148
+ for( let i = 0; i < INPUT.length; i++ ) { // iterate through the message
149
+ let CHAR = INPUT.charAt( i ).toUpperCase(); // the current character we convert, only upper case is supported at this time
150
+ let lastLineLength = lineLength; // we need the lineLength for alignment before we look up if the next char fits
151
+
152
+ Debugging.report(`Character found in font: "${ CHAR }"`, 2);
153
+
154
+ if( CHAR !== `|` ) { // what will the line length be if we add the next char?
155
+ lineLength += CharLength( FONTFACE.chars[ CHAR ], FONTFACE.lines, OPTIONS ); // get the length of this character
156
+ lineLength += CharLength( FONTFACE.letterspace, FONTFACE.lines, OPTIONS ) * OPTIONS.letterSpacing; // new line, new line length
157
+ }
158
+
159
+ // jump to next line after OPTIONS.maxLength characters or when line break is found or the console windows would have ran out of space
160
+ if( maxChars >= OPTIONS.maxLength && OPTIONS.maxLength != 0 || CHAR === `|` || lineLength > size.width ) {
161
+ lines ++;
162
+
163
+ Debugging.report(
164
+ `NEWLINE: maxChars: ${ maxChars }, ` +
165
+ `OPTIONS.maxLength: ${ OPTIONS.maxLength }, ` +
166
+ `CHAR: ${ CHAR }, ` +
167
+ `lineLength: ${ lineLength }, ` +
168
+ `Size.width: ${ size.width } `, 2
169
+ );
170
+
171
+ if( OPTIONS.env === 'node' ) {
172
+ output = AlignText( output, lastLineLength, FONTFACE.lines, OPTIONS.align, size ); // calculate alignment based on lineLength
173
+ }
174
+
175
+ lineLength = CharLength( FONTFACE.buffer, FONTFACE.lines, OPTIONS ); // new line: new line length
176
+ lineLength += CharLength( FONTFACE.letterspace, FONTFACE.lines, OPTIONS ) * OPTIONS.letterSpacing; // each new line starts with letter spacing
177
+
178
+ if( CHAR !== `|` ) { // if this is a character and not a line break
179
+ lineLength += CharLength( FONTFACE.chars[ CHAR ], FONTFACE.lines, OPTIONS ); // get the length of this character
180
+ lineLength += CharLength( FONTFACE.letterspace, FONTFACE.lines, OPTIONS ) * OPTIONS.letterSpacing; // add letter spacing at the end
181
+ }
182
+
183
+ maxChars = 0; // new line, new maxLength goal
184
+
185
+ output = AddLine( output, FONTFACE.lines, FONTFACE.buffer, OPTIONS.lineHeight ); // adding new line
186
+ // add letter spacing to the beginning
187
+ output = AddLetterSpacing( output, FONTFACE.lines, FONTFACE.letterspace, FONTFACE.colors, OPTIONS.colors, OPTIONS.letterSpacing );
188
+ }
189
+
190
+ Debugging.report(`lineLength at: "${ lineLength }"`, 2);
191
+
192
+ if( CHAR !== `|` ) {
193
+ maxChars++; // counting all printed characters
194
+ output = AddChar( CHAR, output, FONTFACE.lines, FONTFACE.chars, FONTFACE.colors, OPTIONS.colors ); // add new character
195
+ output = AddLetterSpacing( output, FONTFACE.lines, FONTFACE.letterspace, FONTFACE.colors, OPTIONS.colors, OPTIONS.letterSpacing );
196
+ }
197
+ }
198
+
199
+ if( OPTIONS.env === 'node' ) {
200
+ output = AlignText( output, lineLength, FONTFACE.lines, OPTIONS.align, size ); // alignment last line
201
+ }
202
+ }
203
+
204
+ if( OPTIONS.gradient ) {
205
+ output = PaintGradient({
206
+ output,
207
+ gradient: OPTIONS.gradient,
208
+ lines,
209
+ lineHeight: OPTIONS.lineHeight,
210
+ fontLines: FONTFACE.lines,
211
+ independentGradient: OPTIONS.independentGradient,
212
+ transitionGradient: OPTIONS.transitionGradient,
213
+ });
214
+ }
215
+
216
+ if( OPTIONS.space ) { // add space
217
+ if( OPTIONS.align === 'top' ) {
218
+ output[ output.length - 1 ] = `${ output[ output.length - 1 ] }\n\n\n\n`;
219
+ }
220
+ else if( OPTIONS.align === 'bottom' ) {
221
+ output[ 0 ] = `\n\n\n\n${ output[ 0 ] }`;
222
+ }
223
+ else {
224
+ output[ 0 ] = `\n\n${ output[ 0 ] }`;
225
+ output[ output.length - 1 ] = `${ output[ output.length - 1 ] }\n\n`;
226
+ }
227
+ }
228
+
229
+ if( OPTIONS.background !== 'transparent' && OPTIONS.env === 'node' ) {
230
+ const { open: openNew, close: closeNew } = Color( OPTIONS.background, true );
231
+
232
+ output[ 0 ] = `${ openNew }\n${ output[ 0 ] }`;
233
+ output[ output.length - 1 ] = `${ output[ output.length - 1 ] }${ closeNew }`;
234
+ }
235
+
236
+ let write = output.join( OPTIONS.env === 'node' ? `\n` : '<br>' );
237
+
238
+ if( OPTIONS.env === 'browser' ) {
239
+ const { open: bgColor } = Color( OPTIONS.background, true );
240
+
241
+ write = `<div style="font-family:monospace;white-space:pre;text-align:${ OPTIONS.align };max-width:100%;overflow:scroll;background:${ bgColor ? bgColor : 'transparent' }">` +
242
+ `${ write }` +
243
+ `</div>`;
244
+ }
245
+
246
+ return {
247
+ string: write,
248
+ array: output,
249
+ lines: lines,
250
+ options: OPTIONS,
251
+ }
252
+ };
253
+
254
+
255
+ module.exports = exports = {
256
+ Render,
257
+ };
@@ -0,0 +1,102 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * RenderConsole
12
+ * Render our input with the console font
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const { AlignText } = require('./AlignText.js');
19
+ const { Colorize } = require('./Colorize.js');
20
+ const { Size } = require('./Size.js');
21
+
22
+
23
+ /**
24
+ * Render our input with the console font
25
+ *
26
+ * @param {string} INPUT - The string you want to write out
27
+ * @param {object} OPTIONS - All user options
28
+ * @param {object} size - The size of the terminal as an object, default: Size
29
+ * @param {number} size.width - The width of the terminal
30
+ * @param {number} size.height - The height of the terminal
31
+ *
32
+ * @typedef {object} ReturnObject
33
+ * @property {array} output - An array of each line of the output
34
+ * @property {number} lines - The count of line breaks
35
+ *
36
+ * @return {ReturnObject} - An object with the output and the line breaks
37
+ */
38
+ const RenderConsole = ( INPUT, OPTIONS, size = Size ) => {
39
+ const width = OPTIONS.maxLength < size.width && OPTIONS.maxLength !== 0
40
+ ? OPTIONS.maxLength
41
+ : size.width;
42
+ let lines = 0;
43
+ let output = [];
44
+ let i = 0;
45
+
46
+ let space = '';
47
+ if( OPTIONS.letterSpacing > 0 ) {
48
+ space = ' '.repeat( OPTIONS.letterSpacing );
49
+ }
50
+
51
+ // we have to add our letter spacing first
52
+ const outputLines = INPUT
53
+ .replace( /(?:\r\n|\r|\n)/g, '|' )
54
+ .split( '|' )
55
+ .map( line =>
56
+ line
57
+ .split('')
58
+ .join( space )
59
+ );
60
+
61
+ // now we check each line for it's length and split them if too long
62
+ while( i < outputLines.length ) {
63
+ let line = outputLines[ i ];
64
+
65
+ if( line.length > width ) {
66
+ outputLines[ i ] = line.slice( 0, width );
67
+ outputLines.splice( i + 1, 0, line.slice( width ) );
68
+ line = outputLines[ i ];
69
+ }
70
+
71
+ if( OPTIONS.colors[ 0 ] === 'candy' ) {
72
+ output.push( line
73
+ .split('')
74
+ .map( character => Colorize( character, 1, OPTIONS.colors ) )
75
+ .join('')
76
+ );
77
+ }
78
+ else {
79
+ output.push( Colorize( line, 1, OPTIONS.colors ) );
80
+ }
81
+
82
+ if( OPTIONS.env !== 'browser' ) {
83
+ output = AlignText( output, line.length, 1, OPTIONS.align, size );
84
+ }
85
+ if( i !== outputLines.length - 1 ) {
86
+ output = [ ...output, ...Array( OPTIONS.lineHeight ).fill('') ];
87
+ }
88
+
89
+ lines++;
90
+ i++;
91
+ }
92
+
93
+ return {
94
+ output,
95
+ lines,
96
+ };
97
+ };
98
+
99
+
100
+ module.exports = exports = {
101
+ RenderConsole,
102
+ };
package/src/Say.js ADDED
@@ -0,0 +1,50 @@
1
+ /***************************************************************************************************************************************************************
2
+ *
3
+ * cfonts
4
+ *
5
+ * Sexy fonts for the console. (CLI output)
6
+ *
7
+ * @license https://github.com/dominikwilkowski/cfonts/blob/released/LICENSE GNU GPLv2
8
+ * @author Dominik Wilkowski hi@dominik-wilkowski.com
9
+ * @repository https://github.com/dominikwilkowski/cfonts
10
+ *
11
+ * Say
12
+ * Print to console
13
+ *
14
+ **************************************************************************************************************************************************************/
15
+
16
+ 'use strict';
17
+
18
+ const { Debugging, DEBUG } = require('./Debugging.js');
19
+ const { Render } = require('./Render.js');
20
+ const { Size } = require('./Size.js');
21
+
22
+
23
+ /**
24
+ * Print to console
25
+ *
26
+ * @param {string} INPUT - The string you want to write out
27
+ * @param {object} SETTINGS - Settings object
28
+ * @param {boolean} debug - A flag to enable debug mode
29
+ * @param {number} debuglevel - The debug level we want to show
30
+ * @param {object} size - The size of the terminal as an object, default: Size
31
+ * @param {number} size.width - The width of the terminal
32
+ * @param {number} size.height - The height of the terminal
33
+ */
34
+ const Say = ( INPUT, SETTINGS = {}, debug = DEBUG.enabled, debuglevel = DEBUG.level, size = Size ) => {
35
+ Debugging.report(`Running say`, 1);
36
+
37
+ DEBUG.enabled = debug;
38
+ DEBUG.level = debuglevel;
39
+
40
+ const write = Render( INPUT, SETTINGS, debug, debuglevel, size );
41
+
42
+ if( write ) {
43
+ console.log( write.string ); // write out
44
+ }
45
+ };
46
+
47
+
48
+ module.exports = exports = {
49
+ Say,
50
+ };