From 65c3b7d8e15bfa6f8f8830f533445e8878873374 Mon Sep 17 00:00:00 2001 From: Marcus Hultman Date: Sat, 3 Jul 2021 11:04:29 -0400 Subject: [PATCH] deno fmt --- .../_fish_completions_generator.ts | 13 +++-- command/completions/complete.ts | 50 +++++++++++-------- command/types.ts | 6 ++- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/command/completions/_fish_completions_generator.ts b/command/completions/_fish_completions_generator.ts index 1329e11f2..c396d7f3a 100644 --- a/command/completions/_fish_completions_generator.ts +++ b/command/completions/_fish_completions_generator.ts @@ -144,9 +144,16 @@ ${this.generateCompletions(this.cmd).trim()} return `'(${this.cmd.getName()} completions complete -t (${token}) ${cmd.trim()})'`; } - private getOptionCompletionCommand(shortOption: string | undefined, longOption: string | undefined, cmd: string): string { - const pipeReplace = (prefix: string, opt?: string) => opt ? `| string replace -- ${prefix}${opt} ""` : ''; - const token = `commandline -tc ${pipeReplace('-', shortOption)} ${pipeReplace('--', longOption)}`; + private getOptionCompletionCommand( + shortOption: string | undefined, + longOption: string | undefined, + cmd: string, + ): string { + const pipeReplace = (prefix: string, opt?: string) => + opt ? `| string replace -- ${prefix}${opt} ""` : ""; + const token = `commandline -tc ${pipeReplace("-", shortOption)} ${ + pipeReplace("--", longOption) + }`; return `'(${this.cmd.getName()} completions complete -t (${token}) ${cmd.trim()})'`; } } diff --git a/command/completions/complete.ts b/command/completions/complete.ts index b995ffed7..b231b3860 100644 --- a/command/completions/complete.ts +++ b/command/completions/complete.ts @@ -3,34 +3,42 @@ import { UnknownCompletionCommand } from "../_errors.ts"; import type { ICompletion } from "../types.ts"; /** Execute auto completion method of command and action. */ -export class CompleteCommand - extends Command<{ token: string }, [action: string, commandNames?: Array]> { +export class CompleteCommand extends Command< + { token: string }, + [action: string, commandNames?: Array] +> { public constructor(cmd?: Command) { super(); this.description("Get completions for given action from given command.") .option("-t, --token [t:string]", "the current cmd token.") .arguments(" [command...:string]") - .action(async ({ token = '' }, action: string, commandNames?: Array) => { - let parent: Command | undefined; - const completeCommand: Command = commandNames - ?.reduce((cmd: Command, name: string): Command => { - parent = cmd; - const childCmd: Command | undefined = cmd.getCommand(name, false); - if (!childCmd) { - throw new UnknownCompletionCommand(name, cmd.getCommands()); - } - return childCmd; - }, cmd || this.getMainCommand()) ?? (cmd || this.getMainCommand()); + .action( + async ( + { token = "" }, + action: string, + commandNames?: Array, + ) => { + let parent: Command | undefined; + const completeCommand: Command = commandNames + ?.reduce((cmd: Command, name: string): Command => { + parent = cmd; + const childCmd: Command | undefined = cmd.getCommand(name, false); + if (!childCmd) { + throw new UnknownCompletionCommand(name, cmd.getCommands()); + } + return childCmd; + }, cmd || this.getMainCommand()) ?? (cmd || this.getMainCommand()); - const completion: ICompletion | undefined = completeCommand - .getCompletion(action); - const result: Array = - await completion?.complete(token, completeCommand, parent) ?? []; + const completion: ICompletion | undefined = completeCommand + .getCompletion(action); + const result: Array = + await completion?.complete(token, completeCommand, parent) ?? []; - if (result?.length) { - Deno.stdout.writeSync(new TextEncoder().encode(result.join("\n"))); - } - }) + if (result?.length) { + Deno.stdout.writeSync(new TextEncoder().encode(result.join("\n"))); + } + }, + ) .reset(); } } diff --git a/command/types.ts b/command/types.ts index c0a8f30ef..d46de1ded 100644 --- a/command/types.ts +++ b/command/types.ts @@ -208,7 +208,11 @@ export type ICompleteHandler< PG extends Record | void = any, // deno-lint-ignore no-explicit-any P extends Command | undefined = any, -> = (token: string, cmd: Command, parent?: Command) => CompleteHandlerResult; +> = ( + token: string, + cmd: Command, + parent?: Command, +) => CompleteHandlerResult; /** Help callback method to print the help. Invoked by the `--help` option and `help` command and the `.getHelp()` and `.showHelp()` method's. */ export type IHelpHandler<