Skip to content

Commit

Permalink
feat: Build help text with dynamic columns, based on terminal width
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Oct 25, 2024
1 parent b4c23b2 commit 4e5f69b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions command/help/_help_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface OptionGroup {
export class HelpGenerator {
private indent = 2;
private options: Required<HelpOptions>;
private width: number;

/** Generate help text for given command. */
public static generate(cmd: Command, options?: HelpOptions): string {
Expand All @@ -48,6 +49,7 @@ export class HelpGenerator {
private cmd: Command,
options: HelpOptions = {},
) {
this.width = Math.min(Deno.consoleSize()?.columns, 150);
this.options = {
types: false,
hints: true,
Expand Down Expand Up @@ -123,7 +125,8 @@ export class HelpGenerator {
[dedent(this.cmd.getDescription())],
])
.indent(this.indent)
.maxColWidth(140)
.maxColWidth(this.width - this.indent)
.colRigidity(0)
.padding(1)
.toString() +
"\n";
Expand Down Expand Up @@ -188,6 +191,8 @@ export class HelpGenerator {
])
.padding([2, 2, 1, 2])
.indent(this.indent)
.maxTableWidth(this.width - this.indent)
.colRigidity([1, 1, 1, 0, 0])
.maxColWidth([60, 60, 1, 80, 60])
.toString() +
"\n";
Expand All @@ -203,6 +208,8 @@ export class HelpGenerator {
]),
])
.indent(this.indent)
.maxTableWidth(this.width - this.indent)
.colRigidity([1, 1, 1, 0])
.maxColWidth([60, 1, 80, 60])
.padding([2, 1, 2])
.toString() +
Expand Down Expand Up @@ -235,6 +242,8 @@ export class HelpGenerator {
]),
])
.indent(this.indent)
.maxTableWidth(this.width - this.indent)
.colRigidity([1, 1, 1, 0])
.maxColWidth([60, 60, 1, 80])
.padding([2, 2, 1, 2])
.toString() +
Expand All @@ -252,6 +261,8 @@ export class HelpGenerator {
command.getShortDescription(),
]),
])
.maxTableWidth(this.width - this.indent)
.colRigidity([1, 1, 0])
.maxColWidth([60, 1, 80])
.padding([2, 1, 2])
.indent(this.indent)
Expand Down Expand Up @@ -281,6 +292,8 @@ export class HelpGenerator {
])
.padding([2, 2, 1, 2])
.indent(this.indent)
.maxTableWidth(this.width - this.indent)
.colRigidity([1, 1, 1, 0, 0])
.maxColWidth([60, 60, 1, 80, 10])
.toString() +
"\n";
Expand All @@ -298,7 +311,7 @@ export class HelpGenerator {
]))
.padding(1)
.indent(this.indent)
.maxColWidth(150)
.maxTableWidth(this.width - this.indent)
.toString() +
"\n";
}
Expand Down

0 comments on commit 4e5f69b

Please sign in to comment.