Skip to content

Commit

Permalink
Capture help tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkGriffiths committed Nov 3, 2021
1 parent 4f46bc6 commit 8c53778
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 144 deletions.
71 changes: 36 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import meta from '@thebespokepixel/meta';
import { createSelector } from '@thebespokepixel/n-selector';
import _ from 'lodash';
import { simple, palette } from 'trucolor';
import { Buffer } from 'node:buffer';
import { statSync, readFileSync } from 'node:fs';
import ansiRegex from 'ansi-regex';

Expand All @@ -20,7 +21,7 @@ const newlineRegex$1 = /\n/g;
class Tokeniser {
/**
* Create a new tokeniser
* @param {Regexp} tokenisingRegex - The regex that forms the word boundaries.
* @param {RegExp} tokenisingRegex - The regex that forms the word boundaries.
*/
constructor(tokenisingRegex) {
this.tokenisingRegex = tokenisingRegex || (function () {
Expand Down Expand Up @@ -48,8 +49,8 @@ class Tokeniser {
}
/**
* Reconstruct the line, flushing any remaining tokens
* @param {String} source - Line to process
* @return {String} - Process line
* @param {string} source - Line to process
* @return {string} - Process line
*/
restore(source) {
return source
Expand All @@ -60,7 +61,7 @@ class Tokeniser {
/**
* Creates a tokeniser.
* @private
* @param {<type>} tokenisingRegex The tokenising regular expression
* @param {RegExp} tokenisingRegex The tokenising regular expression
* @see {@link Tokeniser}
* @return {Tokeniser} { A tokeniser instance. }
*/
Expand Down Expand Up @@ -103,7 +104,7 @@ class LineFitter {
/**
* Add a token to the line.
* @param {string} token The word token to add.
* @returns {Boolean} Causes newline.
* @returns {boolean} Causes newline.
*/
add(token) {
if (newlineRegex.test(token)) {
Expand Down Expand Up @@ -161,9 +162,9 @@ class LineFitter {
/**
* Creates a line fitter - a new line of wrapped text..
* @private
* @param {String} margin The left margin, made up of spaces
* @param {Number} width The width the line can take up
* @param {Number} tabWidth Desired TAB width
* @param {string} margin The left margin, made up of spaces
* @param {number} width The width the line can take up
* @param {number} tabWidth Desired TAB width
* @return {LineFitter} The line fitter.
*/
function createLineFitter(margin, width, tabWidth) {
Expand All @@ -178,9 +179,9 @@ class WrapTool {
/**
* Create a new line wrapping tool.
* @param {options} $0 - The supplied options
* @param {Number} $0.left - The left margins
* @param {Number} $0.width - The width of the view, in chars
* @param {Regex} $0.tokenRegex - An optional regex passed to the Tokeniser
* @param {number} $0.left - The left margins
* @param {number} $0.width - The width of the view, in chars
* @param {RegExp} $0.tokenRegex - An optional regex passed to the Tokeniser
*/
constructor({
left,
Expand All @@ -195,8 +196,8 @@ class WrapTool {
}
/**
* Apply instance settings to source text.
* @param {String} text - The text that require wrapping to the view.
* @return {String} - Text with wrapping applied.
* @param {string} text - The text that require wrapping to the view.
* @return {string} - Text with wrapping applied.
*/
wrap(text) {
this.lines = [];
Expand All @@ -221,7 +222,7 @@ class WrapTool {
/**
* Creates a wrap tool.
* @private
* @param {Object} options The options
* @param {object} options The options
* @return {WrapTool} The wrap tool.
*/
function createWrapTool(options) {
Expand All @@ -231,11 +232,11 @@ function createWrapTool(options) {
const clr = _.merge(
simple({format: 'sgr'}),
palette({format: 'sgr'},
{
title: 'bold #9994D1',
bright: 'bold rgb(255,255,255)',
dark: '#333',
}),
{
title: 'bold #9994D1',
bright: 'bold rgb(255,255,255)',
dark: '#333',
}),
);
const colorReplacer = new TemplateTag(
replaceSubstitutionTransformer(
Expand All @@ -257,9 +258,9 @@ class Image {
* @param {string} $0.file - The filename of the image.
* @param {string} $0.name - The name of the image
* [will be taken from image if missing]
* @param {String} $0.width - Can be X(chars), Xpx(pixels),
* @param {string} $0.width - Can be X(chars), Xpx(pixels),
* X%(% width of window) or 'auto'
* @param {String} $0.height - Can be Y(chars), Ypx(pixels),
* @param {string} $0.height - Can be Y(chars), Ypx(pixels),
* Y%(% width of window) or 'auto'
*/
constructor({
Expand Down Expand Up @@ -291,29 +292,29 @@ class Image {
}
/**
* Load and render the image into the CLI
* @param {Object} options - The options to set
* @param {object} options - The options to set
* @property {number} align - The line count needed to realign the cursor.
* @property {Boolean} stretch - Do we stretch the image to match the width
* @property {boolean} stretch - Do we stretch the image to match the width
* and height.
* @property {Boolean} nobreak - Do we clear the image with a newline?
* @property {boolean} nobreak - Do we clear the image with a newline?
* @return {string} The string to insert into the output buffer, with base64
* encoded image.
*/
render(options) {
const {align, stretch = false, nobreak} = options;
const {align, stretch = false, nobreak, spacing = ''} = options;
const content = Buffer.from(readFileSync(this.filePath));
const aspect = stretch ? 'preserveAspectRatio=0;' : '';
const linebreak = nobreak ? '' : '\n';
const newline = align > 1 ? `\u001BH\u001B[${align}A` : linebreak;
return `${prefix}${aspect}size=${content.length}${this.config}:${
content.toString('base64')
}${suffix}${newline}`
}${suffix}${newline}${spacing}`
}
}
/**
* Creates an image.
* @private
* @param {String} source The source
* @param {string} source The source
* @return {Image} A configured (but not yet loaded) image.
*/
function createImage(source) {
Expand All @@ -325,7 +326,7 @@ function createImage(source) {
* @private
* @param {string} buffer_ Input plain text.
* @param {string} delimiter_ Field delimiter.
* @param {Number} width_ Panel width.
* @param {number} width_ Panel width.
* @return {object} The columnify configuration.
*/
function panel(buffer_, delimiter_, width_) {
Expand Down Expand Up @@ -428,7 +429,7 @@ function truwrap({
if (outStream.isTTY) {
return outStream.columns || outStream.getWindowSize()[0]
}
return Infinity
return 120
})();
const viewWidth = (function () {
if (ttyWidth - left - right > 1) {
Expand Down Expand Up @@ -468,15 +469,15 @@ function truwrap({
/**
* Fetch the width in characters of the wrapping view.
* @function
* @return {Number} wrapping width
* @return {number} wrapping width
*/
getWidth: unimplemented,
/**
* Create a multicolumn panel within this view
* @function
* @param {panelObject} content - Object for columnify
* @param {Object} configuration - Configuration for columnify
* @return {String} - The rendered panel.
* @param {object} configuration - Configuration for columnify
* @return {string} - The rendered panel.
*/
panel(content, configuration) {
if (outStream._isStdio) {
Expand All @@ -487,7 +488,7 @@ function truwrap({
/**
* Generate linebreaks within this view
* @function
* @param {Number} newlines - number of new lines to add.
* @param {number} newlines - number of new lines to add.
* @return {api} has side effect of writing to stream.
*/
break(newlines = 1) {
Expand All @@ -506,7 +507,7 @@ function truwrap({
/**
* Write text via the wrapping logic
* @function
* @param {String} text - The raw, unwrapped test to wrap.
* @param {string} text - The raw, unwrapped test to wrap.
* @return {api} has side effect of writing to stream.
*/
write(text) {
Expand All @@ -516,7 +517,7 @@ function truwrap({
};
switch (true) {
case !ttyActive:
console.info(colorReplacer`${'yellow|Non-TTY'}: width: Infinity`);
console.info(colorReplacer`${'yellow|Non-TTY'}: width: 120`);
/**
* @name noTTY
* @private
Expand Down
5 changes: 3 additions & 2 deletions src/cli/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const images = (function () {
/**
* Render help when asked for.
* @param {yargs} yargsInstance - yargs object defined in cli
* @return {undefined} Writes help to stdout.
* @return {null} Writes help to stdout.
*/
export default async function help(yargsInstance) {
const header = () => stripIndent(colorReplacer)`
${`title| ${metadata.name}`}
${`title|${metadata.name}`}
${images.space}${metadata.description}
${images.space}${`grey|${metadata.version(3)}`}
`
Expand Down Expand Up @@ -75,6 +75,7 @@ export default async function help(yargsInstance) {
container.write(images.cc.render({
nobreak: false,
align: 2,
spacing: ' ',
}))
container.write(header()).break()
container.write(`${clr.dark}${'—'.repeat(windowWidth)}${clr.dark.out}`).break()
Expand Down
65 changes: 31 additions & 34 deletions src/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* ────────────╮
│ truwrap CLI │
╰─────────────┴─────────────────────────────────────────────────────────────── */
/* eslint unicorn/no-process-exit:0,quotes:0 */

import {format} from 'util'

Expand Down Expand Up @@ -156,43 +155,41 @@ if (!(process.env.USER === 'root' && process.env.SUDO_USER !== process.env.USER)
if (argv.help) {
(async () => {
await help(yargsInstance)
process.exit(0)
})()
}

const viewSettings = {
left: argv.left,
right: argv.right,
mode: argv.mode,
tabWidth: argv.tab,
outStream
}

if (argv.regex) {
viewSettings.tokenRegex = new RegExp(argv.regex, 'g')
}
} else {
const viewSettings = {
left: argv.left,
right: argv.right,
mode: argv.mode,
tabWidth: argv.tab,
outStream
}

if (argv.width) {
viewSettings.width = argv.width
}
if (argv.regex) {
viewSettings.tokenRegex = new RegExp(argv.regex, 'g')
}

const renderer = truwrap(viewSettings)
if (argv.width) {
viewSettings.width = argv.width
}

if (argv.stamp) {
renderer.write(format(...argv._))
process.exit(0)
}
const renderer = truwrap(viewSettings)

getStdin().then(input => {
if (argv.panel) {
const panel = parsePanel(input, argv.delimiter, renderer.getWidth())
renderer.panel(panel.content, {
maxLineWidth: renderer.getWidth(),
showHeaders: false,
truncate: argv.truncate,
config: panel.configuration
})
if (argv.stamp) {
renderer.write(format(...argv._))
} else {
renderer.write(input)
getStdin().then(input => {
if (argv.panel) {
const panel = parsePanel(input, argv.delimiter, renderer.getWidth())
renderer.panel(panel.content, {
maxLineWidth: renderer.getWidth(),
showHeaders: false,
truncate: argv.truncate,
config: panel.configuration
})
} else {
renderer.write(input)
}
})
}
})
}
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function truwrap({
return outStream.columns || outStream.getWindowSize()[0]
}

return Infinity
return 120
})()

const viewWidth = (function () {
Expand Down Expand Up @@ -116,16 +116,16 @@ export function truwrap({
/**
* Fetch the width in characters of the wrapping view.
* @function
* @return {Number} wrapping width
* @return {number} wrapping width
*/
getWidth: unimplemented,

/**
* Create a multicolumn panel within this view
* @function
* @param {panelObject} content - Object for columnify
* @param {Object} configuration - Configuration for columnify
* @return {String} - The rendered panel.
* @param {object} configuration - Configuration for columnify
* @return {string} - The rendered panel.
*/
panel(content, configuration) {
if (outStream._isStdio) {
Expand All @@ -138,7 +138,7 @@ export function truwrap({
/**
* Generate linebreaks within this view
* @function
* @param {Number} newlines - number of new lines to add.
* @param {number} newlines - number of new lines to add.
* @return {api} has side effect of writing to stream.
*/
break(newlines = 1) {
Expand All @@ -159,7 +159,7 @@ export function truwrap({
/**
* Write text via the wrapping logic
* @function
* @param {String} text - The raw, unwrapped test to wrap.
* @param {string} text - The raw, unwrapped test to wrap.
* @return {api} has side effect of writing to stream.
*/
write(text) {
Expand All @@ -170,7 +170,7 @@ export function truwrap({

switch (true) {
case !ttyActive:
console.info(colorReplacer`${'yellow|Non-TTY'}: width: Infinity`)
console.info(colorReplacer`${'yellow|Non-TTY'}: width: 120`)

/**
* @name noTTY
Expand Down
Loading

0 comments on commit 8c53778

Please sign in to comment.