2018-07-21 05:34:30 +00:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<div class="text-xs font-mono">
|
2018-07-21 22:20:37 +00:00
|
|
|
<div class="rounded-t p-2 bg-black overflow-scroll w-full" style="min-height: 16rem;max-height:64rem;">
|
2018-08-07 05:21:13 +00:00
|
|
|
<div class="mb-2 text-grey-light" ref="terminal" v-if="connected"></div>
|
|
|
|
<div v-else>
|
2018-07-22 05:37:41 +00:00
|
|
|
<div class="spinner spinner-xl mt-24"></div>
|
|
|
|
</div>
|
2018-07-21 07:04:56 +00:00
|
|
|
</div>
|
|
|
|
<div class="rounded-b bg-grey-darkest text-white flex">
|
|
|
|
<div class="flex-no-shrink p-2">
|
|
|
|
<span class="font-bold">$</span>
|
|
|
|
</div>
|
|
|
|
<div class="w-full">
|
2018-07-21 22:20:37 +00:00
|
|
|
<input type="text" aria-label="Send console command" class="bg-transparent text-white p-2 pl-0 w-full" placeholder="enter command and press enter to send"
|
|
|
|
ref="command"
|
|
|
|
v-model="command"
|
|
|
|
v-on:keyup.enter="sendCommand"
|
2018-07-21 23:22:41 +00:00
|
|
|
v-on:keydown="handleArrowKey"
|
2018-07-21 22:20:37 +00:00
|
|
|
>
|
2018-07-21 07:04:56 +00:00
|
|
|
</div>
|
2018-07-21 05:34:30 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-07-21 22:20:37 +00:00
|
|
|
import { Terminal } from 'xterm';
|
|
|
|
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
|
2018-08-02 06:37:14 +00:00
|
|
|
import {mapState} from 'vuex';
|
2018-07-21 22:20:37 +00:00
|
|
|
|
|
|
|
Terminal.applyAddon(TerminalFit);
|
2018-07-21 06:45:07 +00:00
|
|
|
|
2018-07-21 05:34:30 +00:00
|
|
|
export default {
|
|
|
|
name: 'console-page',
|
2018-08-02 06:37:14 +00:00
|
|
|
computed: {
|
|
|
|
...mapState('socket', ['connected']),
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
/**
|
|
|
|
* Watch the connected variable and when it becomes true request the server logs.
|
|
|
|
*
|
|
|
|
* @param {Boolean} state
|
|
|
|
*/
|
|
|
|
connected: function (state) {
|
|
|
|
if (state) {
|
2018-08-07 05:21:13 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.mountTerminal();
|
|
|
|
});
|
2018-08-07 04:33:43 +00:00
|
|
|
} else {
|
2018-08-07 05:21:13 +00:00
|
|
|
this.terminal.clear();
|
2018-08-02 06:37:14 +00:00
|
|
|
}
|
2018-08-07 04:33:43 +00:00
|
|
|
},
|
2018-08-02 06:37:14 +00:00
|
|
|
},
|
2018-07-21 05:34:30 +00:00
|
|
|
|
2018-07-22 05:37:41 +00:00
|
|
|
/**
|
2018-08-02 06:37:14 +00:00
|
|
|
* Listen for specific socket.io emits from the server.
|
2018-07-22 05:37:41 +00:00
|
|
|
*/
|
2018-08-02 06:37:14 +00:00
|
|
|
sockets: {
|
|
|
|
'server log': function (data) {
|
|
|
|
data.split(/\n/g).forEach(line => {
|
2018-08-04 03:48:27 +00:00
|
|
|
this.terminal.writeln(line + '\u001b[0m');
|
2018-08-02 06:37:14 +00:00
|
|
|
});
|
|
|
|
},
|
2018-07-22 05:37:41 +00:00
|
|
|
|
2018-08-02 06:37:14 +00:00
|
|
|
'console': function (data) {
|
|
|
|
data.line.split(/\n/g).forEach(line => {
|
2018-08-04 03:48:27 +00:00
|
|
|
this.terminal.writeln(line + '\u001b[0m');
|
2018-08-02 06:37:14 +00:00
|
|
|
});
|
2018-08-07 04:33:43 +00:00
|
|
|
},
|
2018-08-02 06:37:14 +00:00
|
|
|
},
|
2018-07-21 22:20:37 +00:00
|
|
|
|
2018-08-02 06:37:14 +00:00
|
|
|
/**
|
|
|
|
* Mount the component and setup all of the terminal actions. Also fetches the initial
|
|
|
|
* logs from the server to populate into the terminal if the socket is connected. If the
|
|
|
|
* socket is not connected this will occur automatically when it connects.
|
|
|
|
*/
|
|
|
|
mounted: function () {
|
|
|
|
if (this.connected) {
|
2018-08-07 05:21:13 +00:00
|
|
|
this.mountTerminal();
|
2018-08-02 06:37:14 +00:00
|
|
|
}
|
2018-07-21 06:45:07 +00:00
|
|
|
},
|
2018-07-21 22:20:37 +00:00
|
|
|
|
|
|
|
data: function () {
|
|
|
|
return {
|
2018-08-07 05:21:13 +00:00
|
|
|
terminal: this._terminalInstance(),
|
2018-07-21 22:20:37 +00:00
|
|
|
command: '',
|
2018-07-21 23:22:41 +00:00
|
|
|
commandHistory: [],
|
|
|
|
commandHistoryIndex: -1,
|
2018-07-21 22:20:37 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
2018-08-07 05:21:13 +00:00
|
|
|
/**
|
|
|
|
* Mount the terminal and grab the most recent server logs.
|
|
|
|
*/
|
|
|
|
mountTerminal: function () {
|
|
|
|
// Get a new instance of the terminal setup.
|
|
|
|
this.terminal = this._terminalInstance();
|
|
|
|
|
|
|
|
this.terminal.open(this.$refs.terminal);
|
|
|
|
this.terminal.fit();
|
|
|
|
this.terminal.clear();
|
|
|
|
|
|
|
|
this.$socket.emit('send server log');
|
|
|
|
},
|
|
|
|
|
2018-07-22 05:37:41 +00:00
|
|
|
/**
|
|
|
|
* Send a command to the server using the configured websocket.
|
|
|
|
*/
|
2018-07-21 22:20:37 +00:00
|
|
|
sendCommand: function () {
|
2018-07-21 23:23:15 +00:00
|
|
|
this.commandHistoryIndex = -1;
|
2018-07-21 23:22:41 +00:00
|
|
|
this.commandHistory.unshift(this.command);
|
2018-08-02 06:37:14 +00:00
|
|
|
this.$socket.emit('send command', this.command);
|
2018-07-21 22:20:37 +00:00
|
|
|
this.command = '';
|
2018-07-21 23:22:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a user pressing up/down arrows when in the command field to scroll through thier
|
|
|
|
* command history for this server.
|
|
|
|
*
|
|
|
|
* @param {KeyboardEvent} e
|
|
|
|
*/
|
|
|
|
handleArrowKey: function (e) {
|
|
|
|
if (['ArrowUp', 'ArrowDown'].indexOf(e.key) < 0 || e.key === 'ArrowDown' && this.commandHistoryIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (e.key === 'ArrowUp' && (this.commandHistoryIndex + 1 > (this.commandHistory.length - 1))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.commandHistoryIndex += (e.key === 'ArrowUp') ? 1 : -1;
|
|
|
|
this.command = this.commandHistoryIndex < 0 ? '' : this.commandHistory[this.commandHistoryIndex];
|
2018-08-07 05:21:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new instance of the terminal to be used.
|
|
|
|
*
|
|
|
|
* @return {module:xterm.Terminal}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_terminalInstance() {
|
|
|
|
return new Terminal({
|
|
|
|
disableStdin: true,
|
|
|
|
cursorStyle: 'underline',
|
|
|
|
allowTransparency: true,
|
|
|
|
fontSize: 12,
|
|
|
|
fontFamily: 'Menlo, Monaco, Consolas, monospace',
|
|
|
|
rows: 30,
|
|
|
|
theme: {
|
|
|
|
background: 'transparent',
|
|
|
|
cursor: 'transparent',
|
|
|
|
black: '#000000',
|
|
|
|
red: '#E54B4B',
|
|
|
|
green: '#9ECE58',
|
|
|
|
yellow: '#FAED70',
|
|
|
|
blue: '#396FE2',
|
|
|
|
magenta: '#BB80B3',
|
|
|
|
cyan: '#2DDAFD',
|
|
|
|
white: '#d0d0d0',
|
|
|
|
brightBlack: 'rgba(255, 255, 255, 0.2)',
|
|
|
|
brightRed: '#FF5370',
|
|
|
|
brightGreen: '#C3E88D',
|
|
|
|
brightYellow: '#FFCB6B',
|
|
|
|
brightBlue: '#82AAFF',
|
|
|
|
brightMagenta: '#C792EA',
|
|
|
|
brightCyan: '#89DDFF',
|
|
|
|
brightWhite: '#ffffff',
|
|
|
|
},
|
|
|
|
});
|
2018-07-21 22:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 05:34:30 +00:00
|
|
|
};
|
|
|
|
</script>
|
2018-07-21 22:20:37 +00:00
|
|
|
|
|
|
|
<style lang="postcss">
|
|
|
|
@import "~xterm/src/xterm.css";
|
|
|
|
</style>
|