Skip to content

Commit

Permalink
deno fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
marcushultman committed Jul 3, 2021
1 parent 5515dc7 commit d82001f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
13 changes: 10 additions & 3 deletions command/completions/_fish_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()})'`;
}
}
Expand Down
49 changes: 29 additions & 20 deletions command/completions/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,42 @@ 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<string>]> {
extends Command<
{ token: string },
[action: string, commandNames?: Array<string>]
> {
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("<action:string> [command...:string]")
.action(async ({ token = '' }, action: string, commandNames?: Array<string>) => {
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<string>,
) => {
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<string | number> =
await completion?.complete(token, completeCommand, parent) ?? [];
const completion: ICompletion | undefined = completeCommand
.getCompletion(action);
const result: Array<string | number> =
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();
}
}
6 changes: 5 additions & 1 deletion command/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ export type ICompleteHandler<
PG extends Record<string, any> | void = any,
// deno-lint-ignore no-explicit-any
P extends Command | undefined = any,
> = (token: string, cmd: Command<O, A, G, PG, P>, parent?: Command) => CompleteHandlerResult;
> = (
token: string,
cmd: Command<O, A, G, PG, P>,
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<
Expand Down

0 comments on commit d82001f

Please sign in to comment.