Still completely broken terminal...
This commit is contained in:
parent
54339c1344
commit
1ae374069c
1 changed files with 60 additions and 85 deletions
|
@ -1,10 +1,8 @@
|
||||||
import React, { createRef } from 'react';
|
import React, { createRef, useEffect } from 'react';
|
||||||
import { Terminal } from 'xterm';
|
import { Terminal } from 'xterm';
|
||||||
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
|
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
import { connect } from 'react-redux';
|
import { ServerContext } from '@/state/server';
|
||||||
import { Websocket } from '@/plugins/Websocket';
|
|
||||||
import { ServerStore } from '@/state/server';
|
|
||||||
|
|
||||||
const theme = {
|
const theme = {
|
||||||
background: 'transparent',
|
background: 'transparent',
|
||||||
|
@ -27,14 +25,7 @@ const theme = {
|
||||||
brightWhite: '#ffffff',
|
brightWhite: '#ffffff',
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Props {
|
const terminal = new Terminal({
|
||||||
connected: boolean;
|
|
||||||
instance: Websocket | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Console extends React.PureComponent<Readonly<Props>> {
|
|
||||||
ref = createRef<HTMLDivElement>();
|
|
||||||
terminal = new Terminal({
|
|
||||||
disableStdin: true,
|
disableStdin: true,
|
||||||
cursorStyle: 'underline',
|
cursorStyle: 'underline',
|
||||||
allowTransparency: true,
|
allowTransparency: true,
|
||||||
|
@ -42,56 +33,48 @@ class Console extends React.PureComponent<Readonly<Props>> {
|
||||||
fontFamily: 'Menlo, Monaco, Consolas, monospace',
|
fontFamily: 'Menlo, Monaco, Consolas, monospace',
|
||||||
rows: 30,
|
rows: 30,
|
||||||
theme: theme,
|
theme: theme,
|
||||||
});
|
});
|
||||||
|
|
||||||
componentDidMount () {
|
export default () => {
|
||||||
if (this.ref.current) {
|
const ref = createRef<HTMLDivElement>();
|
||||||
this.terminal.open(this.ref.current);
|
const connected = ServerContext.useStoreState(state => state.socket.connected);
|
||||||
this.terminal.clear();
|
const instance = ServerContext.useStoreState(state => state.socket.instance);
|
||||||
|
|
||||||
|
const handleConsoleOutput = (line: string) => terminal.writeln(
|
||||||
|
line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m',
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (ref.current) {
|
||||||
|
terminal.open(ref.current);
|
||||||
|
|
||||||
// @see https://github.com/xtermjs/xterm.js/issues/2265
|
// @see https://github.com/xtermjs/xterm.js/issues/2265
|
||||||
// @see https://github.com/xtermjs/xterm.js/issues/2230
|
// @see https://github.com/xtermjs/xterm.js/issues/2230
|
||||||
TerminalFit.fit(this.terminal);
|
TerminalFit.fit(terminal);
|
||||||
}
|
}
|
||||||
|
}, [ ref.current ]);
|
||||||
|
|
||||||
if (this.props.connected && this.props.instance) {
|
useEffect(() => {
|
||||||
this.listenForEvents();
|
if (connected && instance) {
|
||||||
}
|
terminal.clear();
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate (prevProps: Readonly<Readonly<Props>>) {
|
instance
|
||||||
if (!prevProps.connected && this.props.connected) {
|
.addListener('stats', data => console.log(JSON.parse(data)))
|
||||||
this.listenForEvents();
|
.addListener('console output', handleConsoleOutput);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount () {
|
|
||||||
if (this.props.instance) {
|
|
||||||
this.props.instance.removeListener('server log', this.handleServerLog);
|
|
||||||
this.props.instance.removeListener('server log', this.handleConsoleOutput);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
listenForEvents () {
|
|
||||||
const instance = this.props.instance!;
|
|
||||||
|
|
||||||
instance.addListener('server log', this.handleServerLog);
|
|
||||||
instance.addListener('console output', this.handleConsoleOutput);
|
|
||||||
instance.send('send logs');
|
instance.send('send logs');
|
||||||
}
|
}
|
||||||
|
|
||||||
handleServerLog = (lines: string[]) => lines.forEach(data => {
|
return () => {
|
||||||
return data.split(/\n/g).forEach(line => this.terminal.writeln(line + '\u001b[0m'));
|
instance && instance
|
||||||
});
|
.removeListener('console output', handleConsoleOutput)
|
||||||
|
.removeAllListeners('stats');
|
||||||
|
};
|
||||||
|
}, [ connected, instance ]);
|
||||||
|
|
||||||
handleConsoleOutput = (line: string) => this.terminal.writeln(
|
|
||||||
line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m'
|
|
||||||
);
|
|
||||||
|
|
||||||
render () {
|
|
||||||
return (
|
return (
|
||||||
<div className={'text-xs font-mono relative'}>
|
<div className={'text-xs font-mono relative'}>
|
||||||
<SpinnerOverlay visible={!this.props.connected} size={'large'}/>
|
<SpinnerOverlay visible={!connected} size={'large'}/>
|
||||||
<div
|
<div
|
||||||
className={'rounded-t p-2 bg-black overflow-scroll w-full'}
|
className={'rounded-t p-2 bg-black overflow-scroll w-full'}
|
||||||
style={{
|
style={{
|
||||||
|
@ -99,7 +82,7 @@ class Console extends React.PureComponent<Readonly<Props>> {
|
||||||
maxHeight: '64rem',
|
maxHeight: '64rem',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div id={'terminal'} ref={this.ref}/>
|
<div id={'terminal'} ref={ref}/>
|
||||||
</div>
|
</div>
|
||||||
<div className={'rounded-b bg-neutral-900 text-neutral-100 flex'}>
|
<div className={'rounded-b bg-neutral-900 text-neutral-100 flex'}>
|
||||||
<div className={'flex-no-shrink p-2 font-bold'}>$</div>
|
<div className={'flex-no-shrink p-2 font-bold'}>$</div>
|
||||||
|
@ -109,12 +92,4 @@ class Console extends React.PureComponent<Readonly<Props>> {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default connect(
|
|
||||||
(state: ServerStore) => ({
|
|
||||||
connected: state.socket.connected,
|
|
||||||
instance: state.socket.instance,
|
|
||||||
}),
|
|
||||||
)(Console);
|
|
||||||
|
|
Loading…
Reference in a new issue