From da710fc4c80e155d74121233dc7a55c8911f91c8 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 24 Aug 2022 13:19:59 -0700 Subject: [PATCH] switch cmd to use RemotePtrType, make Prompt react class to display fullremoteref and cwd --- src/main.tsx | 68 ++++++++++++++++++++++++++++++++++++---------------- src/model.ts | 45 +++++++++++++++++++++++++--------- src/sh2.less | 4 ++++ src/types.ts | 6 ++--- 4 files changed, 88 insertions(+), 35 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 6e35ec2e4..f85ac1f1d 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -10,7 +10,7 @@ import cn from "classnames" import {TermWrap} from "./term"; import type {SessionDataType, LineType, CmdDataType, RemoteType, RemoteStateType, RemoteInstanceType} from "./types"; import localizedFormat from 'dayjs/plugin/localizedFormat'; -import {GlobalModel, GlobalInput, Session, Cmd, Window, Screen, ScreenWindow} from "./model"; +import {GlobalModel, GlobalInput, Session, Cmd, Window, Screen, ScreenWindow, riToRPtr} from "./model"; dayjs.extend(localizedFormat) @@ -54,24 +54,27 @@ function getLineId(line : LineType) : string { return sprintf("%s-%s-%s", line.sessionid, line.windowid, line.lineid); } -function getRemoteStr(remote : RemoteType) : string { - if (remote == null) { - return "(no remote)"; +function makeFullRemoteRef(ownerName : string, remoteRef : string, name : string) : string { + if (isBlank(ownerName) && isBlank(name)) { + return remoteRef; } - if (remote.remotevars.local) { - return sprintf("%s@%s", remote.remotevars.user, "local"); + if (!isBlank(ownerName) && isBlank(name)) { + return ownerName + ":" + remoteRef; } - let hoststr = ""; - if (remote.remotevars.sudo) { - hoststr = sprintf("sudo@%s@%s", remote.remotevars.user, remote.remotevars.host) + if (isBlank(ownerName) && !isBlank(name)) { + return remoteRef + ":" + name; } - else { - hoststr = sprintf("%s@%s", remote.remotevars.user, remote.remotevars.host) + return ownerName + ":" + remoteRef + ":" + name; +} + +function getRemoteStr(rptr : RemotePtrType) : string { + if (rptr == null || isBlank(rptr.remoteid)) { + return "(invalid remote)"; } - if (remote.remotevars.alias) { - return sprintf("(%s) %s", remote.remotevars.alias, hoststr) - } - return hoststr; + let username = (isBlank(rptr.ownerid) ? null : GlobalModel.resolveUserIdToName(rptr.ownerid)); + let remoteRef = GlobalModel.resolveRemoteIdToRef(rptr.remoteid); + let fullRef = makeFullRemoteRef(username, remoteRef, rptr.name); + return fullRef; } function replaceHomePath(path : string, homeDir : string) : string { @@ -141,6 +144,28 @@ class LineText extends React.Component<{sw : ScreenWindow, line : LineType}, {}> } } +@mobxReact.observer +class Prompt extends React.Component<{rptr : RemotePtrType, rstate : RemoteStateType}, {}> { + render() { + let remote : RemoteType = null; + if (this.props.rptr && !isBlank(this.props.rptr.remoteid)) { + remote = GlobalModel.getRemote(this.props.rptr.remoteid); + } + let remoteStr = getRemoteStr(this.props.rptr); + let cwd = getCwdStr(remote, this.props.rstate); + let isRoot = false; + if (remote && remote.remotevars) { + if (remote.remotevars["sudo"] || remote.remotevars["bestuser"] == "root") { + isRoot = true; + } + } + let className = (isRoot ? "term-bright-red" : "term-bright-green"); + return ( + [{remoteStr}] {cwd} {isRoot ? "#" : "$"} + ); + } +} + @mobxReact.observer class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width : number, interObs : IntersectionObserver, initVis : boolean, cmdRefNum : number}, {}> { termLoaded : mobx.IObservableValue = mobx.observable.box(false); @@ -240,11 +265,11 @@ class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width ); } - let promptStr = getRemoteStr(remote); + let remoteStr = getRemoteStr(cmd.remote); let cwd = getCwdStr(remote, cmd.getRemoteState()); return (
- [{promptStr} {cwd}] {cmd.getSingleLineCmdText()} + {cmd.getSingleLineCmdText()}
); } @@ -464,8 +489,10 @@ class CmdInput extends React.Component<{}, {}> { } let win = GlobalModel.getActiveWindow(); let ri : RemoteInstanceType = null; + let rptr : RemotePtrType = null; if (win != null) { ri = win.getCurRemoteInstance(); + rptr = win.curRemote.get(); } console.log("cmd-input remote", ri); let remote : RemoteType = null; @@ -474,8 +501,7 @@ class CmdInput extends React.Component<{}, {}> { remote = GlobalModel.getRemote(ri.remoteid); remoteState = ri.state; } - console.log("lookup remote", remote); - let promptStr = getRemoteStr(remote); + let remoteStr = getRemoteStr(rptr); let cwdStr = getCwdStr(remote, remoteState); let infoMsg = GlobalModel.infoMsg.get(); let infoShow = GlobalModel.infoShow.get(); @@ -525,12 +551,12 @@ class CmdInput extends React.Component<{}, {}> {
- [{promptStr} {cwdStr}] +
-
{promptStr}
+
{remoteStr}
diff --git a/src/model.ts b/src/model.ts index e7e41f3ff..ca138691f 100644 --- a/src/model.ts +++ b/src/model.ts @@ -21,16 +21,23 @@ function remotePtrToString(rptr : RemotePtrType) : string { if (rptr == null || isBlank(rptr.remoteid)) { return null; } - if (isBlank(rptr.owneruserid) && isBlank(rptr.name)) { + if (isBlank(rptr.ownerid) && isBlank(rptr.name)) { return rptr.remoteid; } - if (!isBlank(rptr.owneruserid) && isBlank(rptr.name)) { - return sprintf("@%s:%s", rptr.owneruserid, rptr.remoteid) + if (!isBlank(rptr.ownerid) && isBlank(rptr.name)) { + return sprintf("@%s:%s", rptr.ownerid, rptr.remoteid) } - if (isBlank(rptr.owneruserid) && !isBlank(rptr.name)) { + if (isBlank(rptr.ownerid) && !isBlank(rptr.name)) { return sprintf("%s:%s", rptr.remoteid, rptr.name) } - return sprintf("@%s:%s:%s", rptr.owneruserid, rptr.remoteid, rptr.name) + return sprintf("@%s:%s:%s", rptr.ownerid, rptr.remoteid, rptr.name) +} + +function riToRPtr(ri : RemoteInstanceType) : RemotePtrType { + if (ri == null) { + return null; + } + return {ownerid: ri.ownerid, remoteid: ri.remoteid, name: ri.name}; } type KeyModsType = { @@ -69,6 +76,7 @@ function ces(s : string) { class Cmd { sessionId : string; + remote : RemotePtrType; remoteId : string; cmdId : string; data : OV; @@ -77,7 +85,7 @@ class Cmd { constructor(cmd : CmdDataType) { this.sessionId = cmd.sessionid; this.cmdId = cmd.cmdid; - this.remoteId = cmd.remoteid; + this.remote = cmd.remote; this.data = mobx.observable.box(cmd, {deep: false}); } @@ -135,10 +143,10 @@ class Cmd { } let data = this.data.get(); let inputPacket = { - type: "input", + type: "feinput", ck: this.sessionId + "/" + this.cmdId, inputdata: btoa(event.key), - remoteid: this.remoteId, + remote: this.remote, }; GlobalModel.sendInputPacket(inputPacket); } @@ -534,14 +542,14 @@ class Session { } for (let i=0; i