import { BuiltinCommand } from './builtin_command'; import { ansi } from '../ansi'; import { BooleanArgument } from '../argument'; import { CommandArguments } from '../arguments'; import type { IRunContext, ITabCompleteContext } from '../context'; import { ExitCode } from '../exit_code'; import type { ITabCompleteResult } from '../tab_complete'; class ClearArguments extends CommandArguments { description = 'Clear the terminal screen if ANSI escapes are supported.'; help = new BooleanArgument('h', 'help', 'display this help and exit'); } export class ClearCommand extends BuiltinCommand { get name(): string { return 'clear'; } async tabComplete(context: ITabCompleteContext): Promise { return await new ClearArguments().tabComplete(context); } protected async _run(context: IRunContext): Promise { const { stdout } = context; const args = new ClearArguments().parse(context.args); if (args.help.isSet) { args.writeHelp(stdout); return ExitCode.SUCCESS; } if (stdout.isTerminal()) { stdout.write(ansi.eraseScreen + ansi.eraseSavedLines + ansi.cursorHome); } return ExitCode.SUCCESS; } }