Skip to content

Commit 989f689

Browse files
committed
usage text fixes
1 parent 0050002 commit 989f689

3 files changed

Lines changed: 67 additions & 16 deletions

File tree

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "pwa-node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": [
12+
"<node_internals>/**"
13+
],
14+
"program": "${workspaceFolder}/build/index.js",
15+
"outFiles": [
16+
"${workspaceFolder}/**/*.js"
17+
],
18+
"args": [
19+
"--help"
20+
]
21+
}
22+
]
23+
}

src/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
112112
console.log(color(titleColor, this._help.header))
113113
}
114114

115-
console.log()
115+
if (this._commands.length) {
116+
console.log("")
117+
console.log(color(titleColor, chalk.bold`Commands:`))
118+
this._printCommands()
119+
}
116120

117-
console.log(color(titleColor, chalk.bold`Commands:`))
118-
this._printCommands()
121+
console.log()
119122

120123
console.log(color(titleColor, chalk.bold`Options:`))
121124
this._printOptions()
@@ -225,6 +228,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
225228
indent: nameFullSize + INDENT_LEN,
226229
colorCount: COLOR_COUNT,
227230
firstLineIndent: INDENT_LEN,
231+
printWidth: this._help.printWidth,
228232
})) {
229233
lines.push(line)
230234
}
@@ -237,15 +241,19 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
237241
for (const line of this._getWrappedLines(
238242
this._commands.map((c) => ({ name: this._fullCmdName(c), description: c.description }))
239243
)) {
240-
console.log(line)
244+
if (line.trim().length) {
245+
console.log(line)
246+
}
241247
}
242248
}
243249

244250
private _printOptions() {
245251
for (const line of this._getWrappedLines(
246252
this._options.map((c) => ({ name: this._fullOptName(c), description: c.description }))
247253
)) {
248-
console.log(line)
254+
if (line.trim().length) {
255+
console.log(line)
256+
}
249257
}
250258
}
251259

@@ -259,6 +267,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
259267

260268
massarg()
261269
.help({
270+
// printWidth: 0,
262271
binName: "my-cmd",
263272
useGlobalColumns: true,
264273
header: "This is the app description",
@@ -287,7 +296,8 @@ massarg()
287296
.command({
288297
name: "my-custom-command",
289298
description:
290-
"This is another command that does something. It's a different one just to see more available. This description is just to fill the blanks. Don't kill the messenger.",
299+
"This is another command that does something. It's a different one just to see more available. This " +
300+
"description is just to fill the blanks. Don't kill the messenger.",
291301
aliases: ["cc", "c"],
292302
run: console.log.bind(undefined, "do"),
293303
})

src/utils.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import chalk from "chalk"
2-
import chunk from "lodash/chunk"
2+
// import chunk from "lodash/chunk"
33
import repeat from "lodash/repeat"
44
import merge from "lodash/merge"
55

66
export function color(color: keyof typeof chalk, ...text: any[]): string {
7-
return (chalk[color] as typeof chalk.dim)(...text)
7+
return chalk.reset((chalk[color] as typeof chalk.dim)(...text))
88
}
99

1010
export interface WrapOptions {
@@ -30,29 +30,47 @@ export function wrap(text: string, options?: WrapOptions): string[] {
3030

3131
function indent(i: number, l: string): string {
3232
const INDENT_FIX = -2
33-
return repeat(" ", INDENT_FIX + (i === 0 ? firstIndentSize : indentSize)) + l
33+
return repeat(" ", i === 0 ? firstIndentSize : indentSize) + l
3434
}
3535

36-
if (maxLineLength <= 0) {
37-
console.debug("Returning early")
38-
return text.split("\n")
36+
if (!_opts.printWidth || maxLineLength <= 0) {
37+
return text.split("\n").map((l, i) => indent(i, l))
3938
}
4039

41-
let lines = chunk(text, maxLineLength).map((l, i) => indent(i, l.join("")))
40+
let lines = chunk(text, maxLineLength).map((l, i) => indent(i, l))
4241

4342
lines = [
4443
lines[0],
4544
...chunk(
4645
lines
4746
.slice(1)
4847
.map((l) => l.trim())
49-
.join("")
48+
.join(" ")
5049
.trim(),
5150
maxLineLength - indentSize - COLOR_CODE_LEN
52-
).map((l, i) => indent(i + 1, l.join(""))),
53-
]
51+
).map((l, i) => indent(i + 1, l)),
52+
].filter((l) => l.trim().length)
5453

5554
return lines
5655
}
5756

5857
export const COLOR_CODE_LEN = chalk.yellow` `.length - 1
58+
59+
function chunk(text: string, len: number): string[] {
60+
const arr = text.split(" ")
61+
const result = []
62+
let subStr = arr[0]
63+
for (let i = 1; i < arr.length; i++) {
64+
let word = arr[i]
65+
if (subStr.length + word.length + 1 <= len) {
66+
subStr = subStr + " " + word
67+
} else {
68+
result.push(subStr)
69+
subStr = word
70+
}
71+
}
72+
if (subStr.length) {
73+
result.push(subStr)
74+
}
75+
return result
76+
}

0 commit comments

Comments
 (0)