Skip to content

Commit

Permalink
refactor(command): update hints formatting in help output (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Dec 29, 2020
1 parent f2d8c93 commit ed588e2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
50 changes: 32 additions & 18 deletions command/help/_help_generator.ts
@@ -1,7 +1,7 @@
import { Table } from "../../table/table.ts";
import { ArgumentsParser } from "../_arguments_parser.ts";
import type { Command } from "../command.ts";
import { blue, bold, dim, magenta, red, yellow } from "../deps.ts";
import { blue, bold, dim, italic, magenta, red, yellow } from "../deps.ts";
import type { IEnvVar, IExample, IOption } from "../types.ts";

/** Help text generator. */
Expand Down Expand Up @@ -174,14 +174,14 @@ export class HelpGenerator {
}

private generateExamples(): string {
const examples = this.cmd.getExamples(); ///Users/psychedelix/workspace/deno/deno-cliffy/command/test/option/action_test.ts
const examples = this.cmd.getExamples();
if (!examples.length) {
return "";
}
return this.label("Examples") +
Table.from(examples.map((example: IExample) => [
dim(bold(`${capitalize(example.name)}:`)),
`\n${example.description}`,
example.description,
]))
.padding(1)
.indent(this.indent * 2)
Expand All @@ -194,21 +194,17 @@ export class HelpGenerator {
const hints = [];

option.required && hints.push(yellow(`required`));
typeof option.default !== "undefined" &&
hints.push(
blue(bold(`Default: `)) +
blue(Deno.inspect(option.default, { depth: 1 })),
);
option.depends?.length &&
hints.push(
red(bold(`depends: `)) +
option.depends.map((depends) => red(depends)).join(", "),
);
option.conflicts?.length &&
hints.push(
red(bold(`conflicts: `)) +
option.conflicts.map((conflict) => red(conflict)).join(", "),
);
typeof option.default !== "undefined" && hints.push(
bold(`Default: `) + inspect(option.default),
);
option.depends?.length && hints.push(
yellow(bold(`Depends: `)) +
italic(option.depends.map(getFlag).join(", ")),
);
option.conflicts?.length && hints.push(
red(bold(`Conflicts: `)) +
italic(option.conflicts.map(getFlag).join(", ")),
);

if (hints.length) {
return `(${hints.join(", ")})`;
Expand All @@ -227,3 +223,21 @@ export class HelpGenerator {
function capitalize(string: string): string {
return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
}

function inspect(value: unknown): string {
return Deno.inspect(
value,
// deno < 1.4.3 doesn't support the colors property.
{ depth: 1, colors: true, trailingComma: false } as Deno.InspectOptions,
);
}

function getFlag(name: string) {
if (name.startsWith("-")) {
return name;
}
if (name.length > 1) {
return `--${name}`;
}
return `-${name}`;
}
56 changes: 28 additions & 28 deletions command/test/command/help_command_test.ts
Expand Up @@ -82,13 +82,13 @@ Deno.test({
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: test)
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (depends: test)
-c, --conflicts [val:string] - I conflict with test! (conflicts: test)
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: test)
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (Depends: --test)
-c, --conflicts [val:string] - I conflict with test! (Conflicts: --test)
Commands:
Expand Down Expand Up @@ -125,11 +125,11 @@ Deno.test({
Options:
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: test)
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (depends: test)
-c, --conflicts [val:string] - I conflict with test! (conflicts: test)
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: test)
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (Depends: --test)
-c, --conflicts [val:string] - I conflict with test! (Conflicts: --test)
Commands:
Expand Down Expand Up @@ -165,15 +165,15 @@ Deno.test({
Options:
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: "test")
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (depends: test)
-c, --conflicts [val:string] - I conflict with test! (conflicts: test)
-a, --all <val:string> - I have many hints! (required, Default: "test", depends: test, conflicts:
depends)
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: "test")
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (Depends: --test)
-c, --conflicts [val:string] - I conflict with test! (Conflicts: --test)
-a, --all <val:string> - I have many hints! (required, Default: "test", Depends: --test, Conflicts:
--depends)
Commands:
Expand Down Expand Up @@ -210,13 +210,13 @@ Deno.test({
Options:
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: "test")
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (depends: test)
-c, --conflicts [val:string] - I conflict with test! (conflicts: test)
-a, --all <val:string> - I have many hints! (required, Default: "test", depends: test, conflicts:
depends)
-t, --test [val:string] - test description
-D, --default [val:string] - I have a default value! (Default: "test")
-r, --required [val:string] - I am required! (required)
-d, --depends [val:string] - I depend on test! (Depends: --test)
-c, --conflicts [val:string] - I conflict with test! (Conflicts: --test)
-a, --all <val:string> - I have many hints! (required, Default: "test", Depends: --test, Conflicts:
--depends)
Commands:
Expand Down

0 comments on commit ed588e2

Please sign in to comment.