Skip to content

Commit

Permalink
Merge pull request #80 from FriedlandAaron/feat/raw-mode-flag
Browse files Browse the repository at this point in the history
Add support for raw mode
  • Loading branch information
dominikwilkowski authored Jun 13, 2024
2 parents e433942 + cc85654 commit e00f0fa
Show file tree
Hide file tree
Showing 14 changed files with 1,133 additions and 205 deletions.
13 changes: 9 additions & 4 deletions nodejs/src/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ const Options = {
background: 'transparent',
letterSpacing: 1,
lineHeight: 1,
space: true,
spaceless: false,
maxLength: 0,
gradient: false,
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
};

this.store = { ...defaults }; // cloning
Expand All @@ -64,7 +65,7 @@ const Options = {
* @param {string} options.backgroundColor - Alias for background
* @param {number} options.letterSpacing - Space between letters, Default: set by selected font face
* @param {number} options.lineHeight - Space between lines, Default: 1
* @param {boolean} options.space - Output space before and after output, Default: true
* @param {boolean} options.spaceless - Don't output space before and after output, Default: false
* @param {number} options.maxLength - Maximum amount of characters per line, Default width of console window
* @param {(string|array|boolean)} options.gradient - Gradient color pair, Default: false
* @param {boolean} options.independentGradient - A switch to calculate gradient per line or not
Expand All @@ -73,6 +74,7 @@ const Options = {
* @param {object} options.allowedColors - All allowed font colors
* @param {object} options.allowedBG - All allowed background colors
* @param {object} options.allowedFont - All allowed fontfaces
* @param {boolean} options.rawMode - A switch for raw mode in terminals
*/
set set({
font = '',
Expand All @@ -82,7 +84,7 @@ const Options = {
backgroundColor,
letterSpacing,
lineHeight,
space,
spaceless,
maxLength,
gradient,
independentGradient,
Expand All @@ -91,6 +93,7 @@ const Options = {
allowedColors = COLORS,
allowedBG = BGCOLORS,
allowedFont = FONTFACES,
rawMode,
}) {
this.store.font = font !== '' ? allowedFont[font.toLowerCase()] || font : this.store.font;

Expand All @@ -117,7 +120,7 @@ const Options = {
? 0
: this.store.lineHeight;

this.store.space = typeof space === 'boolean' ? space : this.store.space;
this.store.spaceless = typeof spaceless === 'boolean' ? spaceless : this.store.spaceless;

this.store.maxLength = maxLength !== undefined ? maxLength : this.store.maxLength;

Expand All @@ -137,6 +140,8 @@ const Options = {
transitionGradient !== undefined ? transitionGradient : this.store.transitionGradient;

this.store.env = env !== undefined ? env : this.store.env;

this.store.rawMode = rawMode !== undefined ? rawMode : this.store.rawMode;
},
};

Expand Down
20 changes: 13 additions & 7 deletions nodejs/src/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ const Render = (input, SETTINGS = {}, debug = DEBUG.enabled, debuglevel = DEBUG.
let lineLength = CharLength(FONTFACE.buffer, FONTFACE.lines, OPTIONS); // count each output character per line and start with the buffer
let maxChars = 0; // count each character we print for maxLength option

// handle raw mode
let line_break = '\n';
if (OPTIONS.env === 'node' && OPTIONS.rawMode === true) {
line_break = '\r\n';
}

output = AddLine([], FONTFACE.lines, FONTFACE.buffer, OPTIONS.lineHeight); // create first lines with buffer
lines++;

Expand Down Expand Up @@ -210,26 +216,26 @@ const Render = (input, SETTINGS = {}, debug = DEBUG.enabled, debuglevel = DEBUG.
});
}

if (OPTIONS.space) {
if (!OPTIONS.spaceless) {
// add space
if (OPTIONS.align === 'top') {
output[output.length - 1] = `${output[output.length - 1]}\n\n\n\n`;
output[output.length - 1] = `${output[output.length - 1]}${line_break}${line_break}${line_break}${line_break}`;
} else if (OPTIONS.align === 'bottom') {
output[0] = `\n\n\n\n${output[0]}`;
output[0] = `${line_break}${line_break}${line_break}${line_break}${output[0]}`;
} else {
output[0] = `\n\n${output[0]}`;
output[output.length - 1] = `${output[output.length - 1]}\n\n`;
output[0] = `${line_break}${line_break}${output[0]}`;
output[output.length - 1] = `${output[output.length - 1]}${line_break}${line_break}`;
}
}

if (OPTIONS.background !== 'transparent' && OPTIONS.env === 'node') {
const { open: openNew, close: closeNew } = Color(OPTIONS.background, true);

output[0] = `${openNew}\n${output[0]}`;
output[0] = `${openNew}${line_break}${output[0]}`;
output[output.length - 1] = `${output[output.length - 1]}${closeNew}`;
}

let write = output.join(OPTIONS.env === 'node' ? `\n` : '<br>\n');
let write = output.join(OPTIONS.env === 'node' ? `${line_break}` : `<br>${line_break}`);

if (OPTIONS.env === 'browser') {
const { open: bgColor } = Color(OPTIONS.background, true);
Expand Down
7 changes: 7 additions & 0 deletions nodejs/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ const CLIOPTIONS = {
options: true,
default: 1,
},
'--raw-mode': {
description: 'Use to enable proper newline rendering in raw mode in the terminal by adding \\r to line breaks',
example: '--raw-mode',
short: '-r',
fallback_shortcut: false,
default: false,
},
};

const PACKAGE = require('../package.json');
Expand Down
12 changes: 7 additions & 5 deletions nodejs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ const Cli = (inputOptions = CLIOPTIONS, inputArgs = process.argv) => {
` background: "${args.background}",\n` +
` letterSpacing: ${args['letter-spacing']},\n` +
` lineHeight: ${args['line-height']},\n` +
` space: ${!args.spaceless},\n` +
` spaceless: ${args.spaceless},\n` +
` maxLength: ${args['max-length']},\n` +
` gradient: ${args.gradient},\n` +
` independentGradient: ${args['independent-gradient']},\n` +
` transitionGradient: ${args['transition-gradient']},\n` +
` env: ${args.env},\n` +
` }, ${args.debug}, ${args.debugLevel} );`,
` rawMode: ${args['raw-mode']},\n` +
` }, ${args.debug}, ${args['debug-level']} );`,
3,
args.debug,
args.debugLevel
args['debug-level']
);

if (args.help) {
Expand Down Expand Up @@ -83,15 +84,16 @@ const Cli = (inputOptions = CLIOPTIONS, inputArgs = process.argv) => {
background: args.background,
letterSpacing: args['letter-spacing'],
lineHeight: args['line-height'],
space: !args.spaceless,
spaceless: args.spaceless,
maxLength: args['max-length'],
gradient: args.gradient,
independentGradient: args['independent-gradient'],
transitionGradient: args['transition-gradient'],
env: args.env,
rawMode: args['raw-mode'],
},
args.debug,
args.debugLevel
args['debug-level']
);
};

Expand Down
1 change: 1 addition & 0 deletions nodejs/test/unit/Cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ beforeEach(() => {
gradient: false,
independentGradient: false,
transitionGradient: false,
rawMode: false,
};

Options.set = DEFAULTS;
Expand Down
Loading

0 comments on commit e00f0fa

Please sign in to comment.