From 1ed47b4bc94eeda3702cc981ec0a4a3f2a89f715 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Sun, 4 Aug 2024 13:52:29 +0700 Subject: [PATCH] BUGFIX: Tab completion uses wrong command list (#1537) --- src/Terminal/Terminal.ts | 95 ++++++++++--------- src/Terminal/commands/clear.ts | 5 + src/Terminal/getTabCompletionPossibilities.ts | 47 +-------- 3 files changed, 55 insertions(+), 92 deletions(-) create mode 100644 src/Terminal/commands/clear.ts diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 9a11352d9..3fce2c7cc 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -76,6 +76,7 @@ import { wget } from "./commands/wget"; import { hash } from "../hash/hash"; import { apr1 } from "./commands/apr1"; import { changelog } from "./commands/changelog"; +import { clear } from "./commands/clear"; import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; import { Engine } from "../engine"; import { Directory, resolveDirectory, root } from "../Paths/Directory"; @@ -84,6 +85,52 @@ import { hasTextExtension } from "../Paths/TextFilePath"; import { ContractFilePath } from "../Paths/ContractFilePath"; import { ServerConstants } from "../Server/data/Constants"; +export const TerminalCommands: Record void> = { + "scan-analyze": scananalyze, + alias: alias, + analyze: analyze, + backdoor: backdoor, + buy: buy, + cat: cat, + cd: cd, + changelog: changelog, + check: check, + clear: clear, + cls: clear, + connect: connect, + cp: cp, + download: download, + expr: expr, + free: free, + grep: grep, + grow: grow, + hack: hack, + help: help, + history: history, + home: home, + hostname: hostname, + kill: kill, + killall: killall, + ls: ls, + lscpu: lscpu, + mem: mem, + mv: mv, + nano: nano, + ps: ps, + rm: rm, + run: run, + scan: scan, + scp: scp, + sudov: sudov, + tail: tail, + apr1: apr1, + top: top, + unalias: unalias, + vim: vim, + weaken: weaken, + wget: wget, +}; + export class Terminal { // Flags to determine whether the player is currently running a hack or an analyze action: TTimer | null = null; @@ -744,53 +791,7 @@ export class Terminal { // Aside from the run-by-path command, we don't need the first entry once we've stored it in commandName. commandArray.shift(); - const commands: Record void> = { - "scan-analyze": scananalyze, - alias: alias, - analyze: analyze, - backdoor: backdoor, - buy: buy, - cat: cat, - cd: cd, - changelog: changelog, - check: check, - clear: () => this.clear(), - cls: () => this.clear(), - connect: connect, - cp: cp, - download: download, - expr: expr, - free: free, - grep: grep, - grow: grow, - hack: hack, - help: help, - history: history, - home: home, - hostname: hostname, - kill: kill, - killall: killall, - ls: ls, - lscpu: lscpu, - mem: mem, - mv: mv, - nano: nano, - ps: ps, - rm: rm, - run: run, - scan: scan, - scp: scp, - sudov: sudov, - tail: tail, - apr1: apr1, - top: top, - unalias: unalias, - vim: vim, - weaken: weaken, - wget: wget, - }; - - const f = commands[commandName.toLowerCase()]; + const f = TerminalCommands[commandName.toLowerCase()]; if (!f) return this.error(`Command ${commandName} not found`); f(commandArray, currentServer); diff --git a/src/Terminal/commands/clear.ts b/src/Terminal/commands/clear.ts new file mode 100644 index 000000000..77a4511a3 --- /dev/null +++ b/src/Terminal/commands/clear.ts @@ -0,0 +1,5 @@ +import { Terminal } from "../../Terminal"; + +export function clear(): void { + Terminal.clear(); +} diff --git a/src/Terminal/getTabCompletionPossibilities.ts b/src/Terminal/getTabCompletionPossibilities.ts index 393c65a50..8cf8e44a1 100644 --- a/src/Terminal/getTabCompletionPossibilities.ts +++ b/src/Terminal/getTabCompletionPossibilities.ts @@ -11,50 +11,7 @@ import libarg from "arg"; import { getAllDirectories, resolveDirectory, root } from "../Paths/Directory"; import { isLegacyScript, resolveScriptFilePath } from "../Paths/ScriptFilePath"; import { enums } from "../NetscriptFunctions"; - -// TODO: this shouldn't be hardcoded in two places with no typechecks to verify equivalence -// An array of all Terminal commands -const gameCommands = [ - "alias", - "analyze", - "backdoor", - "cat", - "cd", - "changelog", - "check", - "clear", - "cls", - "connect", - "cp", - "download", - "expr", - "free", - "grow", - "hack", - "help", - "home", - "hostname", - "ifconfig", - "kill", - "killall", - "ls", - "lscpu", - "mem", - "mv", - "nano", - "ps", - "rm", - "run", - "scan-analyze", - "scan", - "scp", - "sudov", - "tail", - "theme", - "top", - "vim", - "weaken", -]; +import { TerminalCommands } from "./Terminal"; /** Suggest all completion possibilities for the last argument in the last command being typed * @param terminalText The current full text entered in the terminal @@ -123,7 +80,7 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi const addAliases = () => addGeneric({ iterable: Aliases.keys() }); const addGlobalAliases = () => addGeneric({ iterable: GlobalAliases.keys() }); - const addCommands = () => addGeneric({ iterable: gameCommands }); + const addCommands = () => addGeneric({ iterable: Object.keys(TerminalCommands) }); const addDarkwebItems = () => addGeneric({ iterable: Object.values(DarkWebItems).map((item) => item.program) }); const addServerNames = () => addGeneric({