bitburner-src/src/Terminal/commands/nano.ts

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-09-16 01:50:44 +02:00
import { ITerminal } from "../ITerminal";
2021-09-17 08:58:02 +02:00
import { IRouter } from "../../ui/Router";
2021-09-16 01:50:44 +02:00
import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer";
2021-09-24 22:34:21 +02:00
import { isScriptFilename } from "../../Script/isScriptFilename";
2021-11-12 05:28:08 +01:00
import { CursorPositions } from "../../ScriptEditor/CursorPositions";
2021-09-16 01:50:44 +02:00
export function nano(
terminal: ITerminal,
2021-09-17 08:58:02 +02:00
router: IRouter,
2021-09-16 01:50:44 +02:00
player: IPlayer,
server: BaseServer,
args: (string | number)[],
): void {
if (args.length !== 1) {
terminal.error("Incorrect usage of nano command. Usage: nano [scriptname]");
return;
}
try {
const filename = args[0] + "";
2021-09-24 00:47:43 +02:00
if (isScriptFilename(filename)) {
2021-09-16 01:50:44 +02:00
const filepath = terminal.getFilepath(filename);
const script = terminal.getScript(player, filename);
if (script == null) {
let code = "";
if (filename.endsWith(".ns") || filename.endsWith(".js")) {
2021-11-12 05:28:08 +01:00
code = `/** @param {NS} ns **/
2021-10-10 05:07:18 +02:00
export async function main(ns) {
2021-11-12 05:28:08 +01:00
2021-09-16 01:50:44 +02:00
}`;
}
2021-11-12 05:28:08 +01:00
CursorPositions.saveCursor(filename, {
row: 3,
column: 5,
});
2021-09-17 08:58:02 +02:00
router.toScriptEditor(filepath, code);
2021-09-16 01:50:44 +02:00
} else {
2021-09-17 08:58:02 +02:00
router.toScriptEditor(filepath, script.code);
2021-09-16 01:50:44 +02:00
}
} else if (filename.endsWith(".txt")) {
const filepath = terminal.getFilepath(filename);
const txt = terminal.getTextFile(player, filename);
if (txt == null) {
2021-09-17 08:58:02 +02:00
router.toScriptEditor(filepath);
2021-09-16 01:50:44 +02:00
} else {
2021-09-17 08:58:02 +02:00
router.toScriptEditor(filepath, txt.text);
2021-09-16 01:50:44 +02:00
}
} else {
2021-11-27 00:04:33 +01:00
terminal.error("Invalid file. Only scripts (.script, .ns, .js), or text files (.txt) can be edited with nano");
2021-09-16 01:50:44 +02:00
return;
}
} catch (e) {
terminal.error(e + "");
}
}