switch cmd to use RemotePtrType, make Prompt react class to display fullremoteref and cwd

This commit is contained in:
sawka 2022-08-24 13:19:59 -07:00
parent 2028a98784
commit da710fc4c8
4 changed files with 88 additions and 35 deletions

View File

@ -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 (
<span className="term-bright-green">[{remoteStr}] {cwd} {isRoot ? "#" : "$"}</span>
);
}
}
@mobxReact.observer
class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width : number, interObs : IntersectionObserver, initVis : boolean, cmdRefNum : number}, {}> {
termLoaded : mobx.IObservableValue<boolean> = mobx.observable.box(false);
@ -240,11 +265,11 @@ class LineCmd extends React.Component<{sw : ScreenWindow, line : LineType, width
</div>
);
}
let promptStr = getRemoteStr(remote);
let remoteStr = getRemoteStr(cmd.remote);
let cwd = getCwdStr(remote, cmd.getRemoteState());
return (
<div className="metapart-mono cmdtext">
<span className="term-bright-green">[{promptStr} {cwd}]</span> {cmd.getSingleLineCmdText()}
<Prompt rptr={cmd.remote} rstate={cmd.getRemoteState()}/> {cmd.getSingleLineCmdText()}
</div>
);
}
@ -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<{}, {}> {
</div>
<div className="cmd-input-context">
<div className="has-text-white">
<span className="bold term-bright-green">[{promptStr} {cwdStr}]</span>
<Prompt rptr={rptr} rstate={remoteState}/>
</div>
</div>
<div className="cmd-input-field field has-addons">
<div className="control cmd-quick-context">
<div className="button is-static">{promptStr}</div>
<div className="button is-static">{remoteStr}</div>
</div>
<div className="control cmd-input-control is-expanded">
<textarea id="main-cmd-input" rows={displayLines} value={curLine} onKeyDown={this.onKeyDown} onChange={this.onChange} className="textarea"></textarea>

View File

@ -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<CmdDataType>;
@ -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<this.remoteInstances.length; i++) {
let rdata = this.remoteInstances[i];
if (rdata.windowid == windowId && rdata.remoteid == rptr.remoteid && rdata.remoteowneruserid == rptr.owneruserid && rdata.name == rptr.name) {
if (rdata.windowid == windowId && rdata.remoteid == rptr.remoteid && rdata.remoteownerid == rptr.ownerid && rdata.name == rptr.name) {
return rdata;
}
}
let remote = GlobalModel.getRemote(rptr.remoteid);
if (remote != null) {
return {riid: "", sessionid: this.sessionId, windowid: windowId,
remoteowneruserid: rptr.owneruserid, remoteid: rptr.remoteid, name: rptr.name,
remoteownerid: rptr.ownerid, remoteid: rptr.remoteid, name: rptr.name,
state: remote.defaultstate};
}
return null;
@ -1302,6 +1310,21 @@ class Model {
sendInputPacket(inputPacket : any) {
this.ws.pushMessage(inputPacket);
}
resolveUserIdToName(userid : string) : string {
return "@[unknown]"
}
resolveRemoteIdToRef(remoteId : string) {
let remote = this.getRemote(remoteId)
if (remote == null) {
return "[unknown]";
}
if (!isBlank(remote.remotealias)) {
return remote.remotealias;
}
return remote.remotecanonicalname;
}
}
class InputClass {
@ -1338,6 +1361,6 @@ if ((window as any).GlobalModal == null) {
GlobalModel = (window as any).GlobalModel;
GlobalInput = (window as any).GlobalInput;
export {Model, Session, Window, GlobalModel, GlobalInput, Cmd, Screen, ScreenWindow};
export {Model, Session, Window, GlobalModel, GlobalInput, Cmd, Screen, ScreenWindow, riToRPtr};

View File

@ -646,6 +646,10 @@ body .xterm .xterm-viewport {
color: #8ae234;
}
.term-bright-red {
color: #ef2929;
}
.monofont-thin {
font-family: 'JetBrains Mono', monospace;
font-weight: 200;

View File

@ -91,7 +91,7 @@ type RemoteInstanceType = {
name : string,
sessionid : string,
windowid : string,
remoteowneruserid : string,
remoteownerid : string,
remoteid : string,
state : RemoteStateType,
@ -99,7 +99,7 @@ type RemoteInstanceType = {
}
type RemotePtrType = {
owneruserid : string,
ownerid : string,
remoteid : string,
name : string,
};
@ -176,7 +176,7 @@ type CmdDonePacketType = {
type CmdDataType = {
sessionid : string,
cmdid : string,
remoteid : string,
remote : RemotePtrType,
cmdstr : string,
remotestate : RemoteStateType,
termopts : TermOptsType,