From 7313d551f11a8c6fbc26211ef2803640bb2bbbd7 Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Wed, 18 Jul 2018 14:34:18 -0400 Subject: [PATCH] [refactor] Pulling out `post` to terminal to its own file. --- src/Alias.js | 2 +- src/DarkWeb.js | 2 +- src/NetscriptFunctions.js | 2 +- src/Script.js | 2 +- src/Terminal.js | 28 +++------------------------ src/ui/postToTerminal.ts | 40 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 src/ui/postToTerminal.ts diff --git a/src/Alias.js b/src/Alias.js index 618a956c2..dee7da66b 100644 --- a/src/Alias.js +++ b/src/Alias.js @@ -1,4 +1,4 @@ -import {post} from "./Terminal"; +import {post} from "./ui/postToTerminal"; let Aliases = {}; let GlobalAliases = {}; diff --git a/src/DarkWeb.js b/src/DarkWeb.js index 9730f25e0..08108d09a 100644 --- a/src/DarkWeb.js +++ b/src/DarkWeb.js @@ -1,7 +1,7 @@ import {Programs} from "./CreateProgram"; import {Player} from "./Player"; import {SpecialServerIps} from "./SpecialServerIps"; -import {post} from "./Terminal"; +import {post} from "./ui/postToTerminal"; import {isValidIPAddress} from "../utils/helpers/isValidIPAddress"; import {formatNumber} from "../utils/StringHelperFunctions"; diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 76b7f81d7..ec469df62 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -35,7 +35,7 @@ import {StockMarket, StockSymbols, SymbolToStockMap, initStockSymbols, updateStockTicker, updateStockPlayerPosition, Stock, shortStock, sellShort, OrderTypes, PositionTypes, placeOrder, cancelOrder} from "./StockMarket"; -import {post} from "./Terminal"; +import {post} from "./ui/postToTerminal"; import {TextFile, getTextFile, createTextFile} from "./TextFile"; import {unknownBladeburnerActionErrorMessage, diff --git a/src/Script.js b/src/Script.js index 93f03a373..d16247275 100644 --- a/src/Script.js +++ b/src/Script.js @@ -29,7 +29,7 @@ import {addWorkerScript, import {Player} from "./Player"; import {AllServers, processSingleServerGrowth} from "./Server"; import {Settings} from "./Settings"; -import {post} from "./Terminal"; +import {post} from "./ui/postToTerminal"; import {TextFile} from "./TextFile"; import {parse, Node} from "../utils/acorn"; diff --git a/src/Terminal.js b/src/Terminal.js index a7f709fd0..cf023b0e4 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -44,34 +44,12 @@ import {logBoxCreate} from "../utils/LogBox"; import {yesNoBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox"; +import {post, hackProgressBarPost, + hackProgressPost} from "./ui/postToTerminal"; import * as JSZip from 'jszip'; import * as FileSaver from 'file-saver'; -/* Write text to terminal */ -//If replace is true then spaces are replaced with " " -function post(input) { - $("#terminal-input").before('' + input + ''); - updateTerminalScroll(); -} - -//Same thing as post but the td cells have ids so they can be animated for the hack progress bar -function hackProgressBarPost(input) { - $("#terminal-input").before('' + input + ''); - updateTerminalScroll(); -} - -function hackProgressPost(input) { - $("#terminal-input").before('' + input + ''); - updateTerminalScroll(); -} - -//Scroll to the bottom of the terminal's 'text area' -function updateTerminalScroll() { - var element = document.getElementById("terminal-container"); - element.scrollTop = element.scrollHeight; -} - function postNetburnerText() { post("Bitburner v" + CONSTANTS.Version); } @@ -2057,4 +2035,4 @@ let Terminal = { } }; -export {postNetburnerText, post, Terminal}; +export {postNetburnerText, Terminal}; diff --git a/src/ui/postToTerminal.ts b/src/ui/postToTerminal.ts new file mode 100644 index 000000000..8fb3383bc --- /dev/null +++ b/src/ui/postToTerminal.ts @@ -0,0 +1,40 @@ +import { getElementById } from "../../utils/uiHelpers/getElementById"; + +/** + * Adds some output to the terminal. + * @param input Text or HTML to output to the terminal + */ +export function post(input: string) { + postContent(input); +} + +/** + * Adds some output to the terminal with an identifier of "hack-progress-bar" + * @param input Text or HTML to output to the terminal + */ +export function hackProgressBarPost(input: string) { + postContent(input, "hack-progress-bar"); +} + +/** + * Adds some output to the terminal with an identifier of "hack-progress" + * @param input Text or HTML to output to the terminal + */ +export function hackProgressPost(input: string) { + postContent(input, "hack-progress"); +} + +function postContent(input: string, id?: string) { + // tslint:disable-next-line:max-line-length + const style: string = `color: var(--my-font-color); background-color:var(--my-background-color);${id === undefined ? " white-space:pre-wrap;" : ""}`; + // tslint:disable-next-line:max-line-length + const content: string = `${input}`; + const inputElement: HTMLElement = getElementById("terminal-input"); + inputElement.insertAdjacentHTML("beforebegin", content); + scrollTerminalToBottom(); +} + +function scrollTerminalToBottom() { + const container: HTMLElement = getElementById("terminal-container"); + container.scrollTop = container.scrollHeight; +}