Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for raw mode #80

Merged
5 changes: 5 additions & 0 deletions nodejs/src/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Options = {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
};

this.store = { ...defaults }; // cloning
Expand Down Expand Up @@ -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 @@ -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 Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions nodejs/src/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ const Render = (input, SETTINGS = {}, debug = DEBUG.enabled, debuglevel = DEBUG.
`</div>`;
}

// handle raw mode
if (OPTIONS.env === 'node') {
if (OPTIONS.rawMode === true) {
const re = /\n/g;
const newline = '\r\n';
write = write.replace(re, newline);
output = output.map((x) => {
return x.replace(re, newline);
});
}
}
dominikwilkowski marked this conversation as resolved.
Show resolved Hide resolved

return {
string: write,
array: output,
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
8 changes: 5 additions & 3 deletions nodejs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ const Cli = (inputOptions = CLIOPTIONS, inputArgs = process.argv) => {
` 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']} );`,
FriedlandAaron marked this conversation as resolved.
Show resolved Hide resolved
3,
args.debug,
args.debugLevel
args['debug-level']
);

if (args.help) {
Expand Down Expand Up @@ -89,9 +90,10 @@ const Cli = (inputOptions = CLIOPTIONS, inputArgs = process.argv) => {
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
40 changes: 40 additions & 0 deletions nodejs/test/unit/Options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test(`Options - Should return default options`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
};

Options.reset();
Expand Down Expand Up @@ -55,6 +56,7 @@ test(`Options - Should be able to handle casing`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand Down Expand Up @@ -84,6 +86,7 @@ test(`Options - Should be able to handle background and backgroundColor`, () =>
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -106,6 +109,7 @@ test(`Options - Should merge font option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -128,6 +132,7 @@ test(`Options - Should merge align option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -150,6 +155,7 @@ test(`Options - Should merge colors option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -172,6 +178,7 @@ test(`Options - Should merge background option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -194,6 +201,7 @@ test(`Options - Should merge backgroundColor option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -216,6 +224,7 @@ test(`Options - Should merge letterSpacing option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -238,6 +247,7 @@ test(`Options - Should merge lineHeight option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -260,6 +270,7 @@ test(`Options - Should merge space option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -282,6 +293,7 @@ test(`Options - Should merge maxLength option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -304,6 +316,7 @@ test(`Options - Should merge string-gradient option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -326,6 +339,7 @@ test(`Options - Should merge array-gradient option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -348,6 +362,7 @@ test(`Options - Should merge independentGradient option with defaults`, () => {
independentGradient: true,
transitionGradient: false,
env: 'node',
rawMode: false,
});
});

Expand All @@ -370,6 +385,7 @@ test(`Options - Should merge independentGradient option with defaults`, () => {
independentGradient: false,
transitionGradient: true,
env: 'node',
rawMode: false,
});
});

Expand All @@ -392,5 +408,29 @@ test(`Options - Should merge env option with defaults`, () => {
independentGradient: false,
transitionGradient: false,
env: 'browser',
rawMode: false,
});
});

test(`Options - Should merge raw-mode option with defaults`, () => {
Options.reset();
Options.set = {
rawMode: true,
};

expect(Options.get).toEqual({
font: 'block',
align: 'left',
colors: [],
background: 'transparent',
letterSpacing: 1,
lineHeight: 1,
space: true,
maxLength: 0,
gradient: false,
independentGradient: false,
transitionGradient: false,
env: 'node',
rawMode: true,
});
});
1 change: 1 addition & 0 deletions nodejs/test/unit/ParseArgs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ test(`ParseArgs - Return defaults without arguments`, () => {
text: undefined,
version: false,
env: 'node',
'raw-mode': false,
};

process.argv = ['node', 'script']; // we have to remove process.argv so args passed to our testing lib don't break our test
Expand Down
Loading