diff --git a/src/models/bookmarks.ts b/src/models/bookmarks.ts index 443ad9df5..17d788f81 100644 --- a/src/models/bookmarks.ts +++ b/src/models/bookmarks.ts @@ -8,11 +8,10 @@ import { genMergeSimpleData } from "../util/util"; import { BookmarkType } from "../types/types"; import { checkKeyPressed, adaptFromReactOrNativeKeyEvent } from "../util/keyutil"; import { OV, OArr } from "../types/types"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Model } from "./model"; class BookmarksModel { - globalCommandRunner: CommandRunner; globalModel: Model; bookmarks: OArr = mobx.observable.array([], { name: "Bookmarks", @@ -39,7 +38,6 @@ class BookmarksModel { constructor(globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); } showBookmarksView(bmArr: BookmarkType[], selectedBookmarkId: string): void { @@ -125,7 +123,7 @@ class BookmarksModel { this.tempDesc.set(""); this.tempCmd.set(""); })(); - this.globalCommandRunner.editBookmark(bm.bookmarkid, bm.description, bm.cmdstr); + GlobalCommandRunner.editBookmark(bm.bookmarkid, bm.description, bm.cmdstr); } handleDeleteBookmark(bookmarkId: string): void { @@ -134,7 +132,7 @@ class BookmarksModel { setTimeout(this.clearPendingDelete, 2000); return; } - this.globalCommandRunner.deleteBookmark(bookmarkId); + GlobalCommandRunner.deleteBookmark(bookmarkId); this.clearPendingDelete(); } diff --git a/src/models/commandrunner.ts b/src/models/commandrunner.ts new file mode 100644 index 000000000..2140a784e --- /dev/null +++ b/src/models/commandrunner.ts @@ -0,0 +1,431 @@ +// Copyright 2023, Command Line Inc. +// SPDX-License-Identifier: Apache-2.0 + +import * as mobx from "mobx"; +import { RendererContext, CommandRtnType, HistorySearchParams, LineStateType } from "../types/types"; +import { GlobalModel } from "./global"; + +class CommandRunner { + private constructor() {} + + static getInstance() { + if (!(window as any).GlobalCommandRunner) { + (window as any).GlobalCommandRunner = new CommandRunner(); + } + return (window as any).GlobalCommandRunner; + } + + loadHistory(show: boolean, htype: string) { + let kwargs = { nohist: "1" }; + if (!show) { + kwargs["noshow"] = "1"; + } + if (htype != null && htype != "screen") { + kwargs["type"] = htype; + } + GlobalModel.submitCommand("history", null, null, kwargs, true); + } + + resetShellState() { + GlobalModel.submitCommand("reset", null, null, null, true); + } + + historyPurgeLines(lines: string[]): Promise { + let prtn = GlobalModel.submitCommand("history", "purge", lines, { nohist: "1" }, false); + return prtn; + } + + switchSession(session: string) { + mobx.action(() => { + GlobalModel.activeMainView.set("session"); + })(); + GlobalModel.submitCommand("session", null, [session], { nohist: "1" }, false); + } + + switchScreen(screen: string, session?: string) { + mobx.action(() => { + GlobalModel.activeMainView.set("session"); + })(); + let kwargs = { nohist: "1" }; + if (session != null) { + kwargs["session"] = session; + } + GlobalModel.submitCommand("screen", null, [screen], kwargs, false); + } + + lineView(sessionId: string, screenId: string, lineNum?: number) { + let screen = GlobalModel.getScreenById(sessionId, screenId); + if (screen != null && lineNum != null) { + screen.setAnchorFields(lineNum, 0, "line:view"); + } + let lineNumStr = lineNum == null || lineNum == 0 ? "E" : String(lineNum); + GlobalModel.submitCommand("line", "view", [sessionId, screenId, lineNumStr], { nohist: "1" }, false); + } + + lineArchive(lineArg: string, archive: boolean): Promise { + let kwargs = { nohist: "1" }; + let archiveStr = archive ? "1" : "0"; + return GlobalModel.submitCommand("line", "archive", [lineArg, archiveStr], kwargs, false); + } + + lineDelete(lineArg: string, interactive: boolean): Promise { + return GlobalModel.submitCommand("line", "delete", [lineArg], { nohist: "1" }, interactive); + } + + lineRestart(lineArg: string, interactive: boolean): Promise { + return GlobalModel.submitCommand("line", "restart", [lineArg], { nohist: "1" }, interactive); + } + + lineSet(lineArg: string, opts: { renderer?: string }): Promise { + let kwargs = { nohist: "1" }; + if ("renderer" in opts) { + kwargs["renderer"] = opts.renderer ?? ""; + } + return GlobalModel.submitCommand("line", "set", [lineArg], kwargs, false); + } + + createNewSession() { + GlobalModel.submitCommand("session", "open", null, { nohist: "1" }, false); + } + + createNewScreen() { + GlobalModel.submitCommand("screen", "open", null, { nohist: "1" }, false); + } + + closeScreen(screen: string) { + GlobalModel.submitCommand("screen", "close", [screen], { nohist: "1" }, false); + } + + // include is lineIds to include, exclude is lineIds to exclude + // if include is given then it *only* does those ids. if exclude is given (or not), + // it does all running commands in the screen except for excluded. + resizeScreen(screenId: string, rows: number, cols: number, opts?: { include?: string[]; exclude?: string[] }) { + let kwargs: Record = { + nohist: "1", + screen: screenId, + cols: String(cols), + rows: String(rows), + }; + if (opts?.include != null && opts?.include.length > 0) { + kwargs.include = opts.include.join(","); + } + if (opts?.exclude != null && opts?.exclude.length > 0) { + kwargs.exclude = opts.exclude.join(","); + } + GlobalModel.submitCommand("screen", "resize", null, kwargs, false); + } + + screenArchive(screenId: string, shouldArchive: boolean): Promise { + return GlobalModel.submitCommand( + "screen", + "archive", + [screenId, shouldArchive ? "1" : "0"], + { nohist: "1" }, + false + ); + } + + screenDelete(screenId: string, interactive: boolean): Promise { + return GlobalModel.submitCommand("screen", "delete", [screenId], { nohist: "1" }, interactive); + } + + screenWebShare(screenId: string, shouldShare: boolean): Promise { + let kwargs: Record = { nohist: "1" }; + kwargs["screen"] = screenId; + return GlobalModel.submitCommand("screen", "webshare", [shouldShare ? "1" : "0"], kwargs, false); + } + + showRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "show", null, { nohist: "1", remote: remoteid }, true); + } + + showAllRemotes() { + GlobalModel.submitCommand("remote", "showall", null, { nohist: "1" }, true); + } + + connectRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "connect", null, { nohist: "1", remote: remoteid }, true); + } + + disconnectRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "disconnect", null, { nohist: "1", remote: remoteid }, true); + } + + installRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "install", null, { nohist: "1", remote: remoteid }, true); + } + + installCancelRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "installcancel", null, { nohist: "1", remote: remoteid }, true); + } + + createRemote(cname: string, kwargsArg: Record, interactive: boolean): Promise { + let kwargs = Object.assign({}, kwargsArg); + kwargs["nohist"] = "1"; + return GlobalModel.submitCommand("remote", "new", [cname], kwargs, interactive); + } + + openCreateRemote(): void { + GlobalModel.submitCommand("remote", "new", null, { nohist: "1", visual: "1" }, true); + } + + screenSetRemote(remoteArg: string, nohist: boolean, interactive: boolean): Promise { + let kwargs = {}; + if (nohist) { + kwargs["nohist"] = "1"; + } + return GlobalModel.submitCommand("connect", null, [remoteArg], kwargs, interactive); + } + + editRemote(remoteid: string, kwargsArg: Record): void { + let kwargs = Object.assign({}, kwargsArg); + kwargs["nohist"] = "1"; + kwargs["remote"] = remoteid; + GlobalModel.submitCommand("remote", "set", null, kwargs, true); + } + + openEditRemote(remoteid: string): void { + GlobalModel.submitCommand("remote", "set", null, { remote: remoteid, nohist: "1", visual: "1" }, true); + } + + archiveRemote(remoteid: string) { + GlobalModel.submitCommand("remote", "archive", null, { remote: remoteid, nohist: "1" }, true); + } + + importSshConfig() { + GlobalModel.submitCommand("remote", "parse", null, { nohist: "1", visual: "1" }, true); + } + + screenSelectLine(lineArg: string, focusVal?: string) { + let kwargs: Record = { + nohist: "1", + line: lineArg, + }; + if (focusVal != null) { + kwargs["focus"] = focusVal; + } + GlobalModel.submitCommand("screen", "set", null, kwargs, false); + } + + screenReorder(screenId: string, index: string) { + let kwargs: Record = { + nohist: "1", + screenId: screenId, + index: index, + }; + GlobalModel.submitCommand("screen", "reorder", null, kwargs, false); + } + + setTermUsedRows(termContext: RendererContext, height: number) { + let kwargs: Record = {}; + kwargs["screen"] = termContext.screenId; + kwargs["hohist"] = "1"; + let posargs = [String(termContext.lineNum), String(height)]; + GlobalModel.submitCommand("line", "setheight", posargs, kwargs, false); + } + + screenSetAnchor(sessionId: string, screenId: string, anchorVal: string): void { + let kwargs = { + nohist: "1", + anchor: anchorVal, + session: sessionId, + screen: screenId, + }; + GlobalModel.submitCommand("screen", "set", null, kwargs, false); + } + + screenSetFocus(focusVal: string): void { + GlobalModel.submitCommand("screen", "set", null, { focus: focusVal, nohist: "1" }, false); + } + + screenSetSettings( + screenId: string, + settings: { tabcolor?: string; tabicon?: string; name?: string; sharename?: string }, + interactive: boolean + ): Promise { + let kwargs: { [key: string]: any } = Object.assign({}, settings); + kwargs["nohist"] = "1"; + kwargs["screen"] = screenId; + return GlobalModel.submitCommand("screen", "set", null, kwargs, interactive); + } + + sessionArchive(sessionId: string, shouldArchive: boolean): Promise { + return GlobalModel.submitCommand( + "session", + "archive", + [sessionId, shouldArchive ? "1" : "0"], + { nohist: "1" }, + false + ); + } + + sessionDelete(sessionId: string): Promise { + return GlobalModel.submitCommand("session", "delete", [sessionId], { nohist: "1" }, false); + } + + sessionSetSettings(sessionId: string, settings: { name?: string }, interactive: boolean): Promise { + let kwargs = Object.assign({}, settings); + kwargs["nohist"] = "1"; + kwargs["session"] = sessionId; + return GlobalModel.submitCommand("session", "set", null, kwargs, interactive); + } + + lineStar(lineId: string, starVal: number) { + GlobalModel.submitCommand("line", "star", [lineId, String(starVal)], { nohist: "1" }, true); + } + + lineBookmark(lineId: string) { + GlobalModel.submitCommand("line", "bookmark", [lineId], { nohist: "1" }, true); + } + + linePin(lineId: string, val: boolean) { + GlobalModel.submitCommand("line", "pin", [lineId, val ? "1" : "0"], { nohist: "1" }, true); + } + + bookmarksView() { + GlobalModel.submitCommand("bookmarks", "show", null, { nohist: "1" }, true); + } + + connectionsView() { + GlobalModel.connectionViewModel.showConnectionsView(); + } + + clientSettingsView() { + GlobalModel.clientSettingsViewModel.showClientSettingsView(); + } + + historyView(params: HistorySearchParams) { + let kwargs = { nohist: "1" }; + kwargs["offset"] = String(params.offset); + kwargs["rawoffset"] = String(params.rawOffset); + if (params.searchText != null) { + kwargs["text"] = params.searchText; + } + if (params.searchSessionId != null) { + kwargs["searchsession"] = params.searchSessionId; + } + if (params.searchRemoteId != null) { + kwargs["searchremote"] = params.searchRemoteId; + } + if (params.fromTs != null) { + kwargs["fromts"] = String(params.fromTs); + } + if (params.noMeta) { + kwargs["meta"] = "0"; + } + if (params.filterCmds) { + kwargs["filter"] = "1"; + } + GlobalModel.submitCommand("history", "viewall", null, kwargs, true); + } + + telemetryOff(interactive: boolean): Promise { + return GlobalModel.submitCommand("telemetry", "off", null, { nohist: "1" }, interactive); + } + + telemetryOn(interactive: boolean): Promise { + return GlobalModel.submitCommand("telemetry", "on", null, { nohist: "1" }, interactive); + } + + releaseCheckAutoOff(interactive: boolean): Promise { + return GlobalModel.submitCommand("releasecheck", "autooff", null, { nohist: "1" }, interactive); + } + + releaseCheckAutoOn(interactive: boolean): Promise { + return GlobalModel.submitCommand("releasecheck", "autoon", null, { nohist: "1" }, interactive); + } + + setTermFontSize(fsize: number, interactive: boolean): Promise { + let kwargs = { + nohist: "1", + termfontsize: String(fsize), + }; + return GlobalModel.submitCommand("client", "set", null, kwargs, interactive); + } + + setClientOpenAISettings(opts: { model?: string; apitoken?: string; maxtokens?: string }): Promise { + let kwargs = { + nohist: "1", + }; + if (opts.model != null) { + kwargs["openaimodel"] = opts.model; + } + if (opts.apitoken != null) { + kwargs["openaiapitoken"] = opts.apitoken; + } + if (opts.maxtokens != null) { + kwargs["openaimaxtokens"] = opts.maxtokens; + } + return GlobalModel.submitCommand("client", "set", null, kwargs, false); + } + + clientAcceptTos(): void { + GlobalModel.submitCommand("client", "accepttos", null, { nohist: "1" }, true); + } + + clientSetConfirmFlag(flag: string, value: boolean): Promise { + let kwargs = { nohist: "1" }; + let valueStr = value ? "1" : "0"; + return GlobalModel.submitCommand("client", "setconfirmflag", [flag, valueStr], kwargs, false); + } + + clientSetSidebar(width: number, collapsed: boolean): Promise { + let kwargs = { nohist: "1", width: `${width}`, collapsed: collapsed ? "1" : "0" }; + return GlobalModel.submitCommand("client", "setsidebar", null, kwargs, false); + } + + editBookmark(bookmarkId: string, desc: string, cmdstr: string) { + let kwargs = { + nohist: "1", + desc: desc, + cmdstr: cmdstr, + }; + GlobalModel.submitCommand("bookmark", "set", [bookmarkId], kwargs, true); + } + + deleteBookmark(bookmarkId: string): void { + GlobalModel.submitCommand("bookmark", "delete", [bookmarkId], { nohist: "1" }, true); + } + + openSharedSession(): void { + GlobalModel.submitCommand("session", "openshared", null, { nohist: "1" }, true); + } + + setLineState( + screenId: string, + lineId: string, + state: LineStateType, + interactive: boolean + ): Promise { + let stateStr = JSON.stringify(state); + return GlobalModel.submitCommand( + "line", + "set", + [lineId], + { screen: screenId, nohist: "1", state: stateStr }, + interactive + ); + } + + screenSidebarAddLine(lineId: string) { + GlobalModel.submitCommand("sidebar", "add", null, { nohist: "1", line: lineId }, false); + } + + screenSidebarRemove() { + GlobalModel.submitCommand("sidebar", "remove", null, { nohist: "1" }, false); + } + + screenSidebarClose(): void { + GlobalModel.submitCommand("sidebar", "close", null, { nohist: "1" }, false); + } + + screenSidebarOpen(width?: string): void { + let kwargs: Record = { nohist: "1" }; + if (width != null) { + kwargs.width = width; + } + GlobalModel.submitCommand("sidebar", "open", null, kwargs, false); + } +} + +export { CommandRunner }; diff --git a/src/models/forwardlinecontainer.ts b/src/models/forwardlinecontainer.ts index 02260163c..b84c039d6 100644 --- a/src/models/forwardlinecontainer.ts +++ b/src/models/forwardlinecontainer.ts @@ -6,12 +6,11 @@ import * as types from "../types/types"; import { windowWidthToCols, windowHeightToRows } from "../util/textmeasure"; import { MagicLayout } from "../app/magiclayout"; import { Model } from "./model"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Cmd } from "./cmd"; import { Screen } from "./screen"; class ForwardLineContainer { - globalCommandRunner: CommandRunner; globalModel: Model; winSize: types.WindowSize; screen: Screen; @@ -20,7 +19,6 @@ class ForwardLineContainer { constructor(screen: Screen, winSize: types.WindowSize, containerType: types.LineContainerStrs, lineId: string) { this.globalModel = Model.getInstance(); - this.globalCommandRunner = CommandRunner.getInstance(); this.screen = screen; this.winSize = winSize; this.containerType = containerType; @@ -35,7 +33,7 @@ class ForwardLineContainer { let cols = windowWidthToCols(winSize.width, fontSize); let rows = windowHeightToRows(winSize.height, fontSize); termWrap.resizeCols(cols); - this.globalCommandRunner.resizeScreen(this.screen.screenId, rows, cols, { include: [this.lineId] }); + GlobalCommandRunner.resizeScreen(this.screen.screenId, rows, cols, { include: [this.lineId] }); } } diff --git a/src/models/global.ts b/src/models/global.ts new file mode 100644 index 000000000..91c263260 --- /dev/null +++ b/src/models/global.ts @@ -0,0 +1,6 @@ +import { Model } from "./model"; +import { CommandRunner } from "./commandrunner"; + +const GlobalModel = Model.getInstance(); +const GlobalCommandRunner = CommandRunner.getInstance(); +export { GlobalModel, GlobalCommandRunner }; diff --git a/src/models/historyview.ts b/src/models/historyview.ts index ef8f2d505..d3a5aa5bf 100644 --- a/src/models/historyview.ts +++ b/src/models/historyview.ts @@ -17,7 +17,7 @@ import dayjs from "dayjs"; import * as appconst from "../app/appconst"; import { checkKeyPressed, adaptFromReactOrNativeKeyEvent } from "../util/keyutil"; import { OV, OArr, OMap } from "../types/types"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Model } from "./model"; import { Cmd } from "./cmd"; import { SpecialLineContainer } from "./speciallinecontainer"; @@ -25,7 +25,6 @@ import { SpecialLineContainer } from "./speciallinecontainer"; const HistoryPageSize = 50; class HistoryViewModel { - globalCommandRunner: CommandRunner; globalModel: Model; items: OArr = mobx.observable.array([], { name: "HistoryItems", @@ -70,7 +69,6 @@ class HistoryViewModel { constructor(globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); } closeView(): void { @@ -161,14 +159,14 @@ class HistoryViewModel { _deleteSelected(): void { let lineIds = Array.from(this.selectedItems.keys()); - let prtn = this.globalCommandRunner.historyPurgeLines(lineIds); + let prtn = GlobalCommandRunner.historyPurgeLines(lineIds); prtn.then((result: CommandRtnType) => { if (!result.success) { this.globalModel.showAlert({ message: "Error removing history lines." }); } }); let params = this._getSearchParams(); - this.globalCommandRunner.historyView(params); + GlobalCommandRunner.historyView(params); } @boundMethod @@ -207,7 +205,7 @@ class HistoryViewModel { reSearch(): void { this.setActiveItem(null); - this.globalCommandRunner.historyView(this._getSearchParams()); + GlobalCommandRunner.historyView(this._getSearchParams()); } resetAllFilters(): void { @@ -220,7 +218,7 @@ class HistoryViewModel { this.searchShowMeta.set(true); this.searchFilterCmds.set(true); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } setFromDate(fromDate: string): void { @@ -230,7 +228,7 @@ class HistoryViewModel { mobx.action(() => { this.searchFromDate.set(fromDate); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } setSearchFilterCmds(filter: boolean): void { @@ -240,7 +238,7 @@ class HistoryViewModel { mobx.action(() => { this.searchFilterCmds.set(filter); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } setSearchShowMeta(show: boolean): void { @@ -250,7 +248,7 @@ class HistoryViewModel { mobx.action(() => { this.searchShowMeta.set(show); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } setSearchSessionId(sessionId: string): void { @@ -260,7 +258,7 @@ class HistoryViewModel { mobx.action(() => { this.searchSessionId.set(sessionId); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } setSearchRemoteId(remoteId: string): void { @@ -270,7 +268,7 @@ class HistoryViewModel { mobx.action(() => { this.searchRemoteId.set(remoteId); })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } goPrev(): void { @@ -280,14 +278,14 @@ class HistoryViewModel { offset = 0; } let params = this._getSearchParams(offset, 0); - this.globalCommandRunner.historyView(params); + GlobalCommandRunner.historyView(params); } goNext(): void { let offset = this.offset.get(); offset += HistoryPageSize; let params = this._getSearchParams(offset, this.nextRawOffset ?? 0); - this.globalCommandRunner.historyView(params); + GlobalCommandRunner.historyView(params); } submitSearch(): void { @@ -298,7 +296,7 @@ class HistoryViewModel { this.historyItemLines = []; this.historyItemCmds = []; })(); - this.globalCommandRunner.historyView(this._getSearchParams(0, 0)); + GlobalCommandRunner.historyView(this._getSearchParams(0, 0)); } handleDocKeyDown(e: any): void { diff --git a/src/models/index.ts b/src/models/index.ts index 39ce398b7..d4d05197d 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,4 @@ +export * from "./global"; export * from "./model"; export { BookmarksModel } from "./bookmarks"; export { ClientSettingsViewModel } from "./clientsettingsview"; diff --git a/src/models/input.ts b/src/models/input.ts index aa9a3ed38..db35b20dc 100644 --- a/src/models/input.ts +++ b/src/models/input.ts @@ -18,7 +18,7 @@ import { StrWithPos } from "../types/types"; import * as appconst from "../app/appconst"; import { OV } from "../types/types"; import { Model } from "./model"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; function getDefaultHistoryQueryOpts(): HistoryQueryOpts { return { @@ -34,7 +34,6 @@ function getDefaultHistoryQueryOpts(): HistoryQueryOpts { } class InputModel { - globalCommandRunner: CommandRunner; globalModel: Model; historyShow: OV = mobx.observable.box(false); infoShow: OV = mobx.observable.box(false); @@ -85,7 +84,6 @@ class InputModel { constructor(globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); this.filteredHistoryItems = mobx.computed(() => { return this._getFilteredHistoryItems(); }); @@ -153,7 +151,7 @@ class InputModel { let screen = this.globalModel.getActiveScreen(); if (screen != null) { if (screen.focusType.get() != "input") { - this.globalCommandRunner.screenSetFocus("input"); + GlobalCommandRunner.screenSetFocus("input"); } } } @@ -251,7 +249,7 @@ class InputModel { mobx.action(() => { this.historyLoading.set(true); })(); - this.globalCommandRunner.loadHistory(show, htype); + GlobalCommandRunner.loadHistory(show, htype); } openHistory(): void { diff --git a/src/models/model.ts b/src/models/model.ts index f76b3ea53..8b281617b 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -60,6 +60,7 @@ import { ModalsModel } from "./modals"; import { MainSidebarModel } from "./mainsidebar"; import { Screen } from "./screen"; import { Cmd } from "./cmd"; +import { GlobalCommandRunner } from "./global"; type KeyModsType = { meta?: boolean; @@ -1416,429 +1417,4 @@ class Model { } } -class CommandRunner { - private constructor() {} - - static getInstance() { - if (!(window as any).GlobalCommandRunner) { - (window as any).GlobalCommandRunner = new CommandRunner(); - } - return (window as any).GlobalCommandRunner; - } - - loadHistory(show: boolean, htype: string) { - let kwargs = { nohist: "1" }; - if (!show) { - kwargs["noshow"] = "1"; - } - if (htype != null && htype != "screen") { - kwargs["type"] = htype; - } - GlobalModel.submitCommand("history", null, null, kwargs, true); - } - - resetShellState() { - GlobalModel.submitCommand("reset", null, null, null, true); - } - - historyPurgeLines(lines: string[]): Promise { - let prtn = GlobalModel.submitCommand("history", "purge", lines, { nohist: "1" }, false); - return prtn; - } - - switchSession(session: string) { - mobx.action(() => { - GlobalModel.activeMainView.set("session"); - })(); - GlobalModel.submitCommand("session", null, [session], { nohist: "1" }, false); - } - - switchScreen(screen: string, session?: string) { - mobx.action(() => { - GlobalModel.activeMainView.set("session"); - })(); - let kwargs = { nohist: "1" }; - if (session != null) { - kwargs["session"] = session; - } - GlobalModel.submitCommand("screen", null, [screen], kwargs, false); - } - - lineView(sessionId: string, screenId: string, lineNum?: number) { - let screen = GlobalModel.getScreenById(sessionId, screenId); - if (screen != null && lineNum != null) { - screen.setAnchorFields(lineNum, 0, "line:view"); - } - let lineNumStr = lineNum == null || lineNum == 0 ? "E" : String(lineNum); - GlobalModel.submitCommand("line", "view", [sessionId, screenId, lineNumStr], { nohist: "1" }, false); - } - - lineArchive(lineArg: string, archive: boolean): Promise { - let kwargs = { nohist: "1" }; - let archiveStr = archive ? "1" : "0"; - return GlobalModel.submitCommand("line", "archive", [lineArg, archiveStr], kwargs, false); - } - - lineDelete(lineArg: string, interactive: boolean): Promise { - return GlobalModel.submitCommand("line", "delete", [lineArg], { nohist: "1" }, interactive); - } - - lineRestart(lineArg: string, interactive: boolean): Promise { - return GlobalModel.submitCommand("line", "restart", [lineArg], { nohist: "1" }, interactive); - } - - lineSet(lineArg: string, opts: { renderer?: string }): Promise { - let kwargs = { nohist: "1" }; - if ("renderer" in opts) { - kwargs["renderer"] = opts.renderer ?? ""; - } - return GlobalModel.submitCommand("line", "set", [lineArg], kwargs, false); - } - - createNewSession() { - GlobalModel.submitCommand("session", "open", null, { nohist: "1" }, false); - } - - createNewScreen() { - GlobalModel.submitCommand("screen", "open", null, { nohist: "1" }, false); - } - - closeScreen(screen: string) { - GlobalModel.submitCommand("screen", "close", [screen], { nohist: "1" }, false); - } - - // include is lineIds to include, exclude is lineIds to exclude - // if include is given then it *only* does those ids. if exclude is given (or not), - // it does all running commands in the screen except for excluded. - resizeScreen(screenId: string, rows: number, cols: number, opts?: { include?: string[]; exclude?: string[] }) { - let kwargs: Record = { - nohist: "1", - screen: screenId, - cols: String(cols), - rows: String(rows), - }; - if (opts?.include != null && opts?.include.length > 0) { - kwargs.include = opts.include.join(","); - } - if (opts?.exclude != null && opts?.exclude.length > 0) { - kwargs.exclude = opts.exclude.join(","); - } - GlobalModel.submitCommand("screen", "resize", null, kwargs, false); - } - - screenArchive(screenId: string, shouldArchive: boolean): Promise { - return GlobalModel.submitCommand( - "screen", - "archive", - [screenId, shouldArchive ? "1" : "0"], - { nohist: "1" }, - false - ); - } - - screenDelete(screenId: string, interactive: boolean): Promise { - return GlobalModel.submitCommand("screen", "delete", [screenId], { nohist: "1" }, interactive); - } - - screenWebShare(screenId: string, shouldShare: boolean): Promise { - let kwargs: Record = { nohist: "1" }; - kwargs["screen"] = screenId; - return GlobalModel.submitCommand("screen", "webshare", [shouldShare ? "1" : "0"], kwargs, false); - } - - showRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "show", null, { nohist: "1", remote: remoteid }, true); - } - - showAllRemotes() { - GlobalModel.submitCommand("remote", "showall", null, { nohist: "1" }, true); - } - - connectRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "connect", null, { nohist: "1", remote: remoteid }, true); - } - - disconnectRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "disconnect", null, { nohist: "1", remote: remoteid }, true); - } - - installRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "install", null, { nohist: "1", remote: remoteid }, true); - } - - installCancelRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "installcancel", null, { nohist: "1", remote: remoteid }, true); - } - - createRemote(cname: string, kwargsArg: Record, interactive: boolean): Promise { - let kwargs = Object.assign({}, kwargsArg); - kwargs["nohist"] = "1"; - return GlobalModel.submitCommand("remote", "new", [cname], kwargs, interactive); - } - - openCreateRemote(): void { - GlobalModel.submitCommand("remote", "new", null, { nohist: "1", visual: "1" }, true); - } - - screenSetRemote(remoteArg: string, nohist: boolean, interactive: boolean): Promise { - let kwargs = {}; - if (nohist) { - kwargs["nohist"] = "1"; - } - return GlobalModel.submitCommand("connect", null, [remoteArg], kwargs, interactive); - } - - editRemote(remoteid: string, kwargsArg: Record): void { - let kwargs = Object.assign({}, kwargsArg); - kwargs["nohist"] = "1"; - kwargs["remote"] = remoteid; - GlobalModel.submitCommand("remote", "set", null, kwargs, true); - } - - openEditRemote(remoteid: string): void { - GlobalModel.submitCommand("remote", "set", null, { remote: remoteid, nohist: "1", visual: "1" }, true); - } - - archiveRemote(remoteid: string) { - GlobalModel.submitCommand("remote", "archive", null, { remote: remoteid, nohist: "1" }, true); - } - - importSshConfig() { - GlobalModel.submitCommand("remote", "parse", null, { nohist: "1", visual: "1" }, true); - } - - screenSelectLine(lineArg: string, focusVal?: string) { - let kwargs: Record = { - nohist: "1", - line: lineArg, - }; - if (focusVal != null) { - kwargs["focus"] = focusVal; - } - GlobalModel.submitCommand("screen", "set", null, kwargs, false); - } - - screenReorder(screenId: string, index: string) { - let kwargs: Record = { - nohist: "1", - screenId: screenId, - index: index, - }; - GlobalModel.submitCommand("screen", "reorder", null, kwargs, false); - } - - setTermUsedRows(termContext: RendererContext, height: number) { - let kwargs: Record = {}; - kwargs["screen"] = termContext.screenId; - kwargs["hohist"] = "1"; - let posargs = [String(termContext.lineNum), String(height)]; - GlobalModel.submitCommand("line", "setheight", posargs, kwargs, false); - } - - screenSetAnchor(sessionId: string, screenId: string, anchorVal: string): void { - let kwargs = { - nohist: "1", - anchor: anchorVal, - session: sessionId, - screen: screenId, - }; - GlobalModel.submitCommand("screen", "set", null, kwargs, false); - } - - screenSetFocus(focusVal: string): void { - GlobalModel.submitCommand("screen", "set", null, { focus: focusVal, nohist: "1" }, false); - } - - screenSetSettings( - screenId: string, - settings: { tabcolor?: string; tabicon?: string; name?: string; sharename?: string }, - interactive: boolean - ): Promise { - let kwargs: { [key: string]: any } = Object.assign({}, settings); - kwargs["nohist"] = "1"; - kwargs["screen"] = screenId; - return GlobalModel.submitCommand("screen", "set", null, kwargs, interactive); - } - - sessionArchive(sessionId: string, shouldArchive: boolean): Promise { - return GlobalModel.submitCommand( - "session", - "archive", - [sessionId, shouldArchive ? "1" : "0"], - { nohist: "1" }, - false - ); - } - - sessionDelete(sessionId: string): Promise { - return GlobalModel.submitCommand("session", "delete", [sessionId], { nohist: "1" }, false); - } - - sessionSetSettings(sessionId: string, settings: { name?: string }, interactive: boolean): Promise { - let kwargs = Object.assign({}, settings); - kwargs["nohist"] = "1"; - kwargs["session"] = sessionId; - return GlobalModel.submitCommand("session", "set", null, kwargs, interactive); - } - - lineStar(lineId: string, starVal: number) { - GlobalModel.submitCommand("line", "star", [lineId, String(starVal)], { nohist: "1" }, true); - } - - lineBookmark(lineId: string) { - GlobalModel.submitCommand("line", "bookmark", [lineId], { nohist: "1" }, true); - } - - linePin(lineId: string, val: boolean) { - GlobalModel.submitCommand("line", "pin", [lineId, val ? "1" : "0"], { nohist: "1" }, true); - } - - bookmarksView() { - GlobalModel.submitCommand("bookmarks", "show", null, { nohist: "1" }, true); - } - - connectionsView() { - GlobalModel.connectionViewModel.showConnectionsView(); - } - - clientSettingsView() { - GlobalModel.clientSettingsViewModel.showClientSettingsView(); - } - - historyView(params: HistorySearchParams) { - let kwargs = { nohist: "1" }; - kwargs["offset"] = String(params.offset); - kwargs["rawoffset"] = String(params.rawOffset); - if (params.searchText != null) { - kwargs["text"] = params.searchText; - } - if (params.searchSessionId != null) { - kwargs["searchsession"] = params.searchSessionId; - } - if (params.searchRemoteId != null) { - kwargs["searchremote"] = params.searchRemoteId; - } - if (params.fromTs != null) { - kwargs["fromts"] = String(params.fromTs); - } - if (params.noMeta) { - kwargs["meta"] = "0"; - } - if (params.filterCmds) { - kwargs["filter"] = "1"; - } - GlobalModel.submitCommand("history", "viewall", null, kwargs, true); - } - - telemetryOff(interactive: boolean): Promise { - return GlobalModel.submitCommand("telemetry", "off", null, { nohist: "1" }, interactive); - } - - telemetryOn(interactive: boolean): Promise { - return GlobalModel.submitCommand("telemetry", "on", null, { nohist: "1" }, interactive); - } - - releaseCheckAutoOff(interactive: boolean): Promise { - return GlobalModel.submitCommand("releasecheck", "autooff", null, { nohist: "1" }, interactive); - } - - releaseCheckAutoOn(interactive: boolean): Promise { - return GlobalModel.submitCommand("releasecheck", "autoon", null, { nohist: "1" }, interactive); - } - - setTermFontSize(fsize: number, interactive: boolean): Promise { - let kwargs = { - nohist: "1", - termfontsize: String(fsize), - }; - return GlobalModel.submitCommand("client", "set", null, kwargs, interactive); - } - - setClientOpenAISettings(opts: { model?: string; apitoken?: string; maxtokens?: string }): Promise { - let kwargs = { - nohist: "1", - }; - if (opts.model != null) { - kwargs["openaimodel"] = opts.model; - } - if (opts.apitoken != null) { - kwargs["openaiapitoken"] = opts.apitoken; - } - if (opts.maxtokens != null) { - kwargs["openaimaxtokens"] = opts.maxtokens; - } - return GlobalModel.submitCommand("client", "set", null, kwargs, false); - } - - clientAcceptTos(): void { - GlobalModel.submitCommand("client", "accepttos", null, { nohist: "1" }, true); - } - - clientSetConfirmFlag(flag: string, value: boolean): Promise { - let kwargs = { nohist: "1" }; - let valueStr = value ? "1" : "0"; - return GlobalModel.submitCommand("client", "setconfirmflag", [flag, valueStr], kwargs, false); - } - - clientSetSidebar(width: number, collapsed: boolean): Promise { - let kwargs = { nohist: "1", width: `${width}`, collapsed: collapsed ? "1" : "0" }; - return GlobalModel.submitCommand("client", "setsidebar", null, kwargs, false); - } - - editBookmark(bookmarkId: string, desc: string, cmdstr: string) { - let kwargs = { - nohist: "1", - desc: desc, - cmdstr: cmdstr, - }; - GlobalModel.submitCommand("bookmark", "set", [bookmarkId], kwargs, true); - } - - deleteBookmark(bookmarkId: string): void { - GlobalModel.submitCommand("bookmark", "delete", [bookmarkId], { nohist: "1" }, true); - } - - openSharedSession(): void { - GlobalModel.submitCommand("session", "openshared", null, { nohist: "1" }, true); - } - - setLineState( - screenId: string, - lineId: string, - state: LineStateType, - interactive: boolean - ): Promise { - let stateStr = JSON.stringify(state); - return GlobalModel.submitCommand( - "line", - "set", - [lineId], - { screen: screenId, nohist: "1", state: stateStr }, - interactive - ); - } - - screenSidebarAddLine(lineId: string) { - GlobalModel.submitCommand("sidebar", "add", null, { nohist: "1", line: lineId }, false); - } - - screenSidebarRemove() { - GlobalModel.submitCommand("sidebar", "remove", null, { nohist: "1" }, false); - } - - screenSidebarClose(): void { - GlobalModel.submitCommand("sidebar", "close", null, { nohist: "1" }, false); - } - - screenSidebarOpen(width?: string): void { - let kwargs: Record = { nohist: "1" }; - if (width != null) { - kwargs.width = width; - } - GlobalModel.submitCommand("sidebar", "open", null, kwargs, false); - } -} - -const GlobalModel = Model.getInstance(); -const GlobalCommandRunner = CommandRunner.getInstance(); -export { Model, CommandRunner, GlobalModel, GlobalCommandRunner }; +export { Model }; diff --git a/src/models/plugins.ts b/src/models/plugins.ts index adeb766c8..4767dd7bc 100644 --- a/src/models/plugins.ts +++ b/src/models/plugins.ts @@ -5,17 +5,15 @@ import * as mobx from "mobx"; import { PluginModel } from "../plugins/plugins"; import { RendererPluginType } from "../types/types"; import { OV } from "../types/types"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Model } from "./model"; class PluginsModel { - globalCommandRunner: CommandRunner = null; globalModel: Model = null; selectedPlugin: OV = mobx.observable.box(null, { name: "selectedPlugin" }); constructor(globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); } showPluginsView(): void { diff --git a/src/models/remotes.ts b/src/models/remotes.ts index e36e81a05..f1e28f789 100644 --- a/src/models/remotes.ts +++ b/src/models/remotes.ts @@ -8,7 +8,7 @@ import { TermWrap } from "../plugins/terminal/term"; import { RemoteInputPacketType, RemoteEditType } from "../types/types"; import * as appconst from "../app/appconst"; import { OV } from "../types/types"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Model } from "./model"; import { getTermPtyData } from "../util/modelutil"; @@ -16,7 +16,6 @@ const RemotePtyRows = 8; // also in main.tsx const RemotePtyCols = 80; class RemotesModel { - globalCommandRunner: CommandRunner; globalModel: Model; selectedRemoteId: OV = mobx.observable.box(null, { name: "RemotesModel-selectedRemoteId", @@ -38,7 +37,6 @@ class RemotesModel { constructor(globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); } get recentConnAdded(): boolean { @@ -97,7 +95,7 @@ class RemotesModel { startEditAuth(): void { let remoteId = this.selectedRemoteId.get(); if (remoteId != null) { - this.globalCommandRunner.openEditRemote(remoteId); + GlobalCommandRunner.openEditRemote(remoteId); } } diff --git a/src/models/screen.ts b/src/models/screen.ts index 72638c0c9..1dfda4418 100644 --- a/src/models/screen.ts +++ b/src/models/screen.ts @@ -29,13 +29,12 @@ import * as appconst from "../app/appconst"; import { checkKeyPressed, adaptFromReactOrNativeKeyEvent } from "../util/keyutil"; import { OV } from "../types/types"; import { Model } from "./model"; -import { CommandRunner } from "./model"; +import { GlobalCommandRunner } from "./global"; import { Cmd } from "./cmd"; import { ScreenLines } from "./screenlines"; import { getTermPtyData } from "../util/modelutil"; class Screen { - globalCommandRunner: CommandRunner; globalModel: Model; sessionId: string; screenId: string; @@ -64,7 +63,6 @@ class Screen { constructor(sdata: ScreenDataType, globalModel: Model) { this.globalModel = globalModel; - this.globalCommandRunner = CommandRunner.getInstance(); this.sessionId = sdata.sessionid; this.screenId = sdata.screenid; this.name = mobx.observable.box(sdata.name, { name: "screen-name" }); @@ -285,7 +283,7 @@ class Screen { setAnchor(anchorLine: number, anchorOffset: number): void { let setVal = anchorLine == null || anchorLine == 0 ? "0" : sprintf("%d:%d", anchorLine, anchorOffset); - this.globalCommandRunner.screenSetAnchor(this.sessionId, this.screenId, setVal); + GlobalCommandRunner.screenSetAnchor(this.sessionId, this.screenId, setVal); } getAnchor(): { anchorLine: number; anchorOffset: number } { @@ -470,7 +468,7 @@ class Screen { exclude.push(lineid); } } - this.globalCommandRunner.resizeScreen(this.screenId, rows, cols, { exclude }); + GlobalCommandRunner.resizeScreen(this.screenId, rows, cols, { exclude }); } getTermWrap(lineId: string): TermWrap { @@ -488,9 +486,9 @@ class Screen { setLineFocus(lineNum: number, focus: boolean): void { mobx.action(() => this.termLineNumFocus.set(focus ? lineNum : 0))(); if (focus && this.selectedLine.get() != lineNum) { - this.globalCommandRunner.screenSelectLine(String(lineNum), "cmd"); + GlobalCommandRunner.screenSelectLine(String(lineNum), "cmd"); } else if (focus && this.focusType.get() == "input") { - this.globalCommandRunner.screenSetFocus("cmd"); + GlobalCommandRunner.screenSetFocus("cmd"); } } diff --git a/src/util/modelutil.ts b/src/util/modelutil.ts index ebf820bbe..5dc594435 100644 --- a/src/util/modelutil.ts +++ b/src/util/modelutil.ts @@ -1,5 +1,5 @@ import { sprintf } from "sprintf-js"; -import { GlobalModel } from "../models/model"; +import { GlobalModel } from "../models"; import { RemotePtrType, FeCmdPacketType, PtyDataType, TermContextUnion } from "../types/types"; import { isBlank } from "./util";