firest commit

This commit is contained in:
wwweww
2026-02-21 22:48:40 +08:00
commit 55e8053e07
1034 changed files with 99049 additions and 0 deletions
+154
View File
@@ -0,0 +1,154 @@
const Section = require('../section')
const t = require('typical')
const Table = require('table-layout')
const chalkFormat = require('../chalk-format')
class ContentSection extends Section {
constructor (section) {
super()
this.header(section.header)
if (section.content) {
/* add content without indentation or wrapping */
if (section.raw) {
const arrayify = require('array-back')
const content = arrayify(section.content).map(line => chalkFormat(line))
this.add(content)
} else {
this.add(getContentLines(section.content))
}
this.add()
}
}
}
function getContentLines (content) {
const defaultPadding = { left: ' ', right: ' ' }
if (content) {
/* string content */
if (t.isString(content)) {
const table = new Table({ column: chalkFormat(content) }, {
padding: defaultPadding,
maxWidth: 80
})
return table.renderLines()
/* array of strings */
} else if (Array.isArray(content) && content.every(t.isString)) {
const rows = content.map(string => ({ column: chalkFormat(string) }))
const table = new Table(rows, {
padding: defaultPadding,
maxWidth: 80
})
return table.renderLines()
/* array of objects (use table-layout) */
} else if (Array.isArray(content) && content.every(t.isPlainObject)) {
const table = new Table(content.map(row => ansiFormatRow(row)), {
padding: defaultPadding
})
return table.renderLines()
/* { options: object, data: object[] } */
} else if (t.isPlainObject(content)) {
if (!content.options || !content.data) {
throw new Error('must have an "options" or "data" property\n' + JSON.stringify(content))
}
const options = Object.assign(
{ padding: defaultPadding },
content.options
)
/* convert nowrap to noWrap to avoid breaking compatibility */
if (options.columns) {
options.columns = options.columns.map(column => {
if (column.nowrap) {
column.noWrap = column.nowrap
delete column.nowrap
}
return column
})
}
const table = new Table(
content.data.map(row => ansiFormatRow(row)),
options
)
return table.renderLines()
} else {
const message = `invalid input - 'content' must be a string, array of strings, or array of plain objects:\n\n${JSON.stringify(content)}`
throw new Error(message)
}
}
}
function ansiFormatRow (row) {
for (const key in row) {
row[key] = chalkFormat(row[key])
}
return row
}
module.exports = ContentSection
/**
* A Content section comprises a header and one or more lines of content.
* @typedef module:command-line-usage~content
* @property header {string} - The section header, always bold and underlined.
* @property content {string|string[]|object[]} - Overloaded property, accepting data in one of four formats:
*
* 1. A single string (one line of text)
* 2. An array of strings (multiple lines of text)
* 3. An array of objects (recordset-style data). In this case, the data will be rendered in table format. The property names of each object are not important, so long as they are consistent throughout the array.
* 4. An object with two properties - `data` and `options`. In this case, the data and options will be passed directly to the underlying [table layout](https://github.com/75lb/table-layout) module for rendering.
*
* @property raw {boolean} - Set to true to avoid indentation and wrapping. Useful for banners.
* @example
* Simple string of content. For ansi formatting, use [chalk template literal syntax](https://github.com/chalk/chalk#tagged-template-literal).
* ```js
* {
* header: 'A typical app',
* content: 'Generates something {rgb(255,200,0).italic very {underline.bgRed important}}.'
* }
* ```
*
* An array of strings is interpreted as lines, to be joined by the system newline character.
* ```js
* {
* header: 'A typical app',
* content: [
* 'First line.',
* 'Second line.'
* ]
* }
* ```
*
* An array of recordset-style objects are rendered in table layout.
* ```js
* {
* header: 'A typical app',
* content: [
* { colA: 'First row, first column.', colB: 'First row, second column.'},
* { colA: 'Second row, first column.', colB: 'Second row, second column.'}
* ]
* }
* ```
*
* An object with `data` and `options` properties will be passed directly to the underlying [table layout](https://github.com/75lb/table-layout) module for rendering.
* ```js
* {
* header: 'A typical app',
* content: {
* data: [
* { colA: 'First row, first column.', colB: 'First row, second column.'},
* { colA: 'Second row, first column.', colB: 'Second row, second column.'}
* ],
* options: {
* maxWidth: 60
* }
* }
* }
* ```
*/
+128
View File
@@ -0,0 +1,128 @@
const Section = require('../section')
const Table = require('table-layout')
const chalk = require('../chalk-format')
const t = require('typical')
const arrayify = require('array-back')
class OptionList extends Section {
constructor (data) {
super()
let definitions = arrayify(data.optionList)
const hide = arrayify(data.hide)
const groups = arrayify(data.group)
/* filter out hidden definitions */
if (hide.length) {
definitions = definitions.filter(definition => {
return hide.indexOf(definition.name) === -1
})
}
if (data.header) this.header(data.header)
if (groups.length) {
definitions = definitions.filter(def => {
const noGroupMatch = groups.indexOf('_none') > -1 && !t.isDefined(def.group)
const groupMatch = intersect(arrayify(def.group), groups)
if (noGroupMatch || groupMatch) return def
})
}
const rows = definitions.map(def => {
return {
option: getOptionNames(def, data.reverseNameOrder),
description: chalk(def.description)
}
})
const tableOptions = data.tableOptions || {
padding: { left: ' ', right: ' ' },
columns: [
{ name: 'option', noWrap: true },
{ name: 'description', maxWidth: 80 }
]
}
const table = new Table(rows, tableOptions)
this.add(table.renderLines())
this.add()
}
}
function getOptionNames (definition, reverseNameOrder) {
let type = definition.type ? definition.type.name.toLowerCase() : 'string'
const multiple = (definition.multiple || definition.lazyMultiple) ? '[]' : ''
if (type) {
type = type === 'boolean' ? '' : `{underline ${type}${multiple}}`
}
type = chalk(definition.typeLabel || type)
let result = ''
if (definition.alias) {
if (definition.name) {
if (reverseNameOrder) {
result = chalk(`{bold --${definition.name}}, {bold -${definition.alias}} ${type}`)
} else {
result = chalk(`{bold -${definition.alias}}, {bold --${definition.name}} ${type}`)
}
} else {
if (reverseNameOrder) {
result = chalk(`{bold -${definition.alias}} ${type}`)
} else {
result = chalk(`{bold -${definition.alias}} ${type}`)
}
}
} else {
result = chalk(`{bold --${definition.name}} ${type}`)
}
return result
}
function intersect (arr1, arr2) {
return arr1.some(function (item1) {
return arr2.some(function (item2) {
return item1 === item2
})
})
}
module.exports = OptionList
/**
* An OptionList section adds a table displaying the supplied option definitions.
* @typedef module:command-line-usage~optionList
* @property {string} [header] - The section header, always bold and underlined.
* @property optionList {OptionDefinition[]} - An array of [option definition](https://github.com/75lb/command-line-args/blob/master/doc/option-definition.md) objects. In addition to the regular definition properties, command-line-usage will look for:
*
* - `description` - a string describing the option.
* - `typeLabel` - a string to replace the default type string (e.g. `<string>`). It's often more useful to set a more descriptive type label, like `<ms>`, `<files>`, `<command>` etc.
* @property {string|string[]} [group] - If specified, only options from this particular group will be printed. [Example](https://github.com/75lb/command-line-usage/blob/master/example/groups.js).
* @property {string|string[]} [hide] - The names of one of more option definitions to hide from the option list. [Example](https://github.com/75lb/command-line-usage/blob/master/example/hide.js).
* @property {boolean} [reverseNameOrder] - If true, the option alias will be displayed after the name, i.e. `--verbose, -v` instead of `-v, --verbose`).
* @property {object} [tableOptions] - An options object suitable for passing into [table-layout](https://github.com/75lb/table-layout#table-). See [here for an example](https://github.com/75lb/command-line-usage/blob/master/example/option-list-options.js).
*
* @example
* {
* header: 'Options',
* optionList: [
* {
* name: 'help',
* alias: 'h',
* description: 'Display this usage guide.'
* },
* {
* name: 'src',
* description: 'The input files to process',
* multiple: true,
* defaultOption: true,
* typeLabel: '{underline file} ...'
* },
* {
* name: 'timeout',
* description: 'Timeout value in ms.',
* alias: 't',
* typeLabel: '{underline ms}'
* }
* ]
* }
*/