hook up cmd cmdstr and prompt

This commit is contained in:
sawka 2022-07-07 13:27:44 -07:00
parent 0053a63536
commit 827c33095b
3 changed files with 127 additions and 11 deletions

View File

@ -8,7 +8,7 @@ import {If, For, When, Otherwise, Choose} from "tsx-control-statements/component
import cn from "classnames"
import {TermWrap} from "./term";
import {getDefaultSession, getLineId, Session} from "./session";
import type {LineType} from "./session";
import type {LineType, CmdDataType, RemoteType} from "./session";
import localizedFormat from 'dayjs/plugin/localizedFormat';
dayjs.extend(localizedFormat)
@ -115,6 +115,54 @@ class LineCmd extends React.Component<{line : LineType, session : Session, chang
}
return cmdText;
}
replaceHomePath(path : string, homeDir : string) : string {
if (path == homeDir) {
return "~";
}
if (path.startsWith(homeDir + "/")) {
return "~" + path.substr(homeDir.length);
}
return path;
}
renderCmdText(cmd : CmdDataType, remote : RemoteType) : any {
if (cmd == null) {
return (
<div className="metapart-mono cmdtext">
<span className="term-bright-green">(cmd not found)</span>
</div>
);
}
let promptStr = "";
if (remote.remotevars.local) {
promptStr = sprintf("%s@%s", remote.remotevars.remoteuser, "local")
}
else if (remote.remotevars.remotehost) {
promptStr = sprintf("%s@%s", remote.remotevars.remoteuser, remote.remotevars.remotehost)
}
else {
let host = remote.remotevars.host || "unknown";
if (remote.remotevars.user) {
promptStr = sprintf("%s@%s", remote.remotevars.user, host)
}
else {
promptStr = host;
}
}
let cwd = "(unknown)";
if (cmd.remotestate && cmd.remotestate.cwd) {
cwd = cmd.remotestate.cwd;
}
if (remote.remotevars.home) {
cwd = this.replaceHomePath(cwd, remote.remotevars.home)
}
return (
<div className="metapart-mono cmdtext">
<span className="term-bright-green">[{promptStr} {cwd}]</span> {this.singleLineCmdText(cmd.cmdstr)}
</div>
);
}
render() {
let {session, line} = this.props;
@ -128,7 +176,12 @@ class LineCmd extends React.Component<{line : LineType, session : Session, chang
let termSize = termWrap.getSize();
let formattedTime = getLineDateStr(line.ts);
let cellHeightPx = 17;
let totalHeight = cellHeightPx * termWrap.usedRows.get();
let totalHeight = cellHeightPx * termWrap.usedRows;
let cmd : CmdDataType = session.getCmd(line.cmdid);
let remote : RemoteType = null;
if (cmd != null) {
remote = session.getRemote(cmd.remoteid);
}
return (
<div className="line line-cmd" id={"line-" + getLineId(line)}>
<div className={cn("avatar",{"num4": lineid.length == 4}, {"num5": lineid.length >= 5}, {"running": running})}>
@ -145,9 +198,7 @@ class LineCmd extends React.Component<{line : LineType, session : Session, chang
<If condition={termSize.rows > 0}>({termSize.rows}x{termSize.cols})</If>
{termWrap.ptyPos} bytes, v{renderVersion}
</div>
<div className="metapart-mono cmdtext">
<span className="term-bright-green">[mike@local ~]</span> {this.singleLineCmdText(line.cmdtext)}
</div>
{this.renderCmdText(cmd, remote)}
</div>
<div className={cn("terminal-wrapper", {"focus": termWrap.isFocused.get()})} style={{overflowY: "hidden"}}>
<div className="terminal" id={"term-" + getLineId(line)} data-cmdid={line.cmdid} style={{height: totalHeight}}></div>
@ -446,3 +497,4 @@ class Main extends React.Component<{}, {}> {
export {Main};

View File

@ -20,7 +20,6 @@ type LineType = {
linetype : string,
text : string,
cmdid : string,
cmdtext : string,
isnew : boolean,
};
@ -32,6 +31,7 @@ type RemoteType = {
remotetype : string,
remoteid : string,
remotename : string,
remotevars : Record<string, string>,
status : string,
defaultstate : RemoteStateType,
};
@ -86,7 +86,40 @@ type FeCmdPacketType = {
remotestate : CmdRemoteStateType,
}
type TermOptsType = {
rows : number,
cols : number,
flexrows : boolean,
};
type CmdStartPacketType = {
type : string,
respid : string,
ts : number,
ck : string,
pid : number,
mshellpid : number,
};
type CmdDonePacketType = {
type : string,
ts : number,
ck : string,
exitcode : number,
durationms : number,
};
type CmdDataType = {
sessionid : string,
cmdid : string,
remoteid : string,
cmdstr : string,
remotestate : RemoteStateType,
termopts : TermOptsType,
status : string,
startpk : CmdStartPacketType,
donepk : CmdDonePacketType,
runout : any[],
};
class Session {
@ -100,6 +133,7 @@ class Session {
loading : mobx.IObservableValue<boolean> = mobx.observable.box(false);
remotes : RemoteInstanceType[] = [];
globalRemotes : RemoteType[];
cmds : CmdDataType[];
constructor() {
}
@ -171,6 +205,30 @@ class Session {
});
}
getCmd(cmdId : string) : CmdDataType {
if (!cmdId) {
return null;
}
for (let i=0; i<this.cmds.length; i++) {
if (this.cmds[i].cmdid == cmdId) {
return this.cmds[i];
}
}
return null;
}
getRemote(remoteId : string) : RemoteType {
if (!remoteId) {
return null;
}
for (let i=0; i<this.globalRemotes.length; i++) {
if (this.globalRemotes[i].remoteid == remoteId) {
return this.globalRemotes[i];
}
}
return null;
}
setActiveWindow(windowid : string) {
this.activeWindowId.set(windowid);
this.loadWindowLines(windowid);
@ -191,7 +249,12 @@ class Session {
fetch(url, {method: "post", body: JSON.stringify(data)}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => {
mobx.action(() => {
if (data.data != null && data.data.line != null) {
this.addLine(data.data.line);
let line = data.data.line;
line.isnew = true;
this.addLine(line);
}
if (data.data != null && data.data.cmd != null) {
this.cmds.push(data.data.cmd);
}
})();
}).catch((err) => {
@ -325,6 +388,7 @@ function initSession() {
DefaultSession.windows[i].linesLoading = mobx.observable.box(false);
}
DefaultSession.remotes = sdata.remotes || [];
DefaultSession.cmds = sdata.cmds || [];
DefaultSession.setActiveWindow(sdata.windows[0].windowid);
sessionLoaded = true;
if (remotesLoaded && sessionLoaded) {
@ -343,4 +407,4 @@ function getDefaultSession() : Session {
(window as any).getDefaultSession = getDefaultSession;
export {Session, getDefaultSession, getLineId, initSession};
export type {LineType, WindowDataType};
export type {LineType, WindowDataType, CmdDataType, RemoteType};

View File

@ -33,14 +33,14 @@ class TermWrap {
atRowMax : boolean = false;
initialized : boolean = false;
changeSizeCallback : (TermWrap) => void = null;
usedRows : mobx.IObservableValue<number> = null;
usedRows : number;
constructor(sessionId : string, cmdId : string) {
this.termId = uuidv4();
this.sessionId = sessionId;
this.cmdId = cmdId;
this.terminal = new Terminal({rows: 25, cols: 80, theme: {foreground: "#d3d7cf"}});
this.usedRows = mobx.observable.box(2, {name: "usedRows"});
this.usedRows = 2;
}
destroy() {
@ -109,7 +109,7 @@ class TermWrap {
usedRows = i+1;
}
}
this.usedRows.set(usedRows);
this.usedRows = usedRows;
return;
}