mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-25 17:04:16 +00:00
feature(terminal) add optional --terminal support
This commit is contained in:
parent
9c80c0f3ac
commit
db2a58ca44
14 changed files with 266 additions and 19 deletions
129
client/modules/terminal.js
Normal file
129
client/modules/terminal.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
'use strict';
|
||||
|
||||
/* global CloudCmd, gritty */
|
||||
|
||||
const exec = require('execon');
|
||||
const load = require('../dom/load');
|
||||
const DOM = require('../dom');
|
||||
const Images = require('../dom/images');
|
||||
const {Dialog} = DOM;
|
||||
|
||||
const TITLE = 'Terminal';
|
||||
|
||||
CloudCmd.Terminal = TerminalProto;
|
||||
|
||||
const {Key} = CloudCmd;
|
||||
|
||||
let Element;
|
||||
let Loaded;
|
||||
let Terminal;
|
||||
|
||||
const {config} = CloudCmd;
|
||||
|
||||
function TerminalProto() {
|
||||
if (!config('terminal'))
|
||||
return;
|
||||
|
||||
Images.show.load('top');
|
||||
|
||||
exec.series([
|
||||
CloudCmd.View,
|
||||
loadAll,
|
||||
create,
|
||||
show,
|
||||
]);
|
||||
|
||||
Element = load({
|
||||
name: 'div',
|
||||
style: 'height: 99%',
|
||||
className : 'terminal',
|
||||
});
|
||||
|
||||
return module.exports;
|
||||
}
|
||||
|
||||
module.exports.show = show;
|
||||
|
||||
module.exports.hide = hide;
|
||||
|
||||
function hide () {
|
||||
CloudCmd.View.hide();
|
||||
}
|
||||
|
||||
function getPrefix() {
|
||||
return CloudCmd.PREFIX + '/gritty';
|
||||
}
|
||||
|
||||
function getEnv() {
|
||||
return {
|
||||
ACTIVE_DIR: DOM.getCurrentDirPath,
|
||||
PASSIVE_DIR: DOM.getNotCurrentDirPath,
|
||||
CURRENT_NAME: DOM.getCurrentName,
|
||||
CURRENT_PATH: DOM.getCurrentPath
|
||||
};
|
||||
}
|
||||
|
||||
function create(callback) {
|
||||
const options = {
|
||||
env: getEnv(),
|
||||
prefix: getPrefix(),
|
||||
socketPath: CloudCmd.PREFIX,
|
||||
};
|
||||
|
||||
const {socket, terminal} = gritty(Element, options);
|
||||
|
||||
terminal.focus();
|
||||
|
||||
Terminal = terminal;
|
||||
|
||||
terminal.on('key', (char, {keyCode, shiftKey}) => {
|
||||
if (shiftKey && keyCode === Key.ESC) {
|
||||
hide();
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('connect', exec.with(authCheck, socket));
|
||||
|
||||
exec(callback);
|
||||
}
|
||||
|
||||
function authCheck(spawn) {
|
||||
if (!config('auth'))
|
||||
return;
|
||||
|
||||
spawn.emit('auth', config('username'), config('password'));
|
||||
|
||||
spawn.on('reject', () => {
|
||||
Dialog.alert(TITLE, 'Wrong credentials!');
|
||||
});
|
||||
}
|
||||
|
||||
function show(callback) {
|
||||
if (!Loaded)
|
||||
return;
|
||||
|
||||
CloudCmd.View.show(Element, {
|
||||
afterShow: () => {
|
||||
if (Terminal) {
|
||||
Terminal.fit(); // lines corrupt without
|
||||
Terminal.focus();
|
||||
}
|
||||
|
||||
exec(callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadAll(callback) {
|
||||
const prefix = getPrefix();
|
||||
const url = prefix + '/gritty.js';
|
||||
|
||||
DOM.load.js(url, (error) => {
|
||||
if (error)
|
||||
return Dialog.alert(TITLE, error.message);
|
||||
|
||||
Loaded = true;
|
||||
exec(callback);
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue