-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c629f8e
commit 139936c
Showing
7 changed files
with
158 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
'use strict'; | ||
|
||
/* global CloudCmd, gritty */ | ||
|
||
const {promisify} = require('es6-promisify'); | ||
const tryToCatch = require('try-to-catch/legacy'); | ||
|
||
require('../../css/terminal.css'); | ||
|
||
const exec = require('execon'); | ||
const load = require('load.js'); | ||
const DOM = require('../dom'); | ||
const Images = require('../dom/images'); | ||
|
||
const loadParallel = promisify(load.parallel); | ||
|
||
const {Dialog} = DOM; | ||
const { | ||
Key, | ||
config, | ||
} = CloudCmd; | ||
|
||
CloudCmd.TerminalRun = exports; | ||
|
||
let Loaded; | ||
let Terminal; | ||
let Socket; | ||
|
||
const loadAll = async () => { | ||
const {prefix} = CloudCmd; | ||
|
||
const prefixGritty = getPrefix(); | ||
const js = `${prefixGritty}/gritty.js`; | ||
const css = `${prefix}/dist/terminal.css`; | ||
|
||
const [e] = await tryToCatch(loadParallel, [js, css]); | ||
|
||
if (e) { | ||
const src = e.target.src.replace(window.location.href, ''); | ||
return Dialog.alert(`file ${src} could not be loaded`); | ||
} | ||
|
||
Loaded = true; | ||
}; | ||
|
||
module.exports.init = async () => { | ||
if (!config('terminal')) | ||
return; | ||
|
||
Images.show.load('top'); | ||
|
||
await CloudCmd.View(); | ||
await loadAll(); | ||
}; | ||
|
||
module.exports.show = show; | ||
module.exports.hide = hide; | ||
|
||
function hide () { | ||
CloudCmd.View.hide(); | ||
} | ||
|
||
function getPrefix() { | ||
return CloudCmd.prefix + '/gritty'; | ||
} | ||
|
||
function getPrefixSocket() { | ||
return CloudCmd.prefixSocket + '/gritty'; | ||
} | ||
|
||
function getEnv() { | ||
return { | ||
ACTIVE_DIR: DOM.getCurrentDirPath, | ||
PASSIVE_DIR: DOM.getNotCurrentDirPath, | ||
CURRENT_NAME: DOM.getCurrentName, | ||
CURRENT_PATH: DOM.getCurrentPath, | ||
}; | ||
} | ||
|
||
function create({command}) { | ||
const options = { | ||
env: getEnv(), | ||
prefix: getPrefixSocket(), | ||
socketPath: CloudCmd.prefix, | ||
fontFamily: 'Droid Sans Mono', | ||
command, | ||
autoRestart: false, | ||
}; | ||
|
||
let commandExit = false; | ||
|
||
const {socket, terminal} = gritty(document.body, options); | ||
|
||
Socket = socket; | ||
Terminal = terminal; | ||
|
||
Terminal.on('key', (char, {keyCode, shiftKey}) => { | ||
if (commandExit) | ||
hide(); | ||
|
||
if (shiftKey && keyCode === Key.ESC) { | ||
hide(); | ||
} | ||
}); | ||
|
||
Socket.on('exit', () => { | ||
terminal.write('\nPress any key to close Terminal...'); | ||
commandExit = true; | ||
}); | ||
|
||
Socket.on('connect', exec.with(authCheck, socket)); | ||
} | ||
|
||
function authCheck(spawn) { | ||
spawn.emit('auth', config('username'), config('password')); | ||
|
||
spawn.on('reject', () => { | ||
Dialog.alert('Wrong credentials!'); | ||
}); | ||
} | ||
|
||
async function show(options = {}) { | ||
if (!Loaded) | ||
return; | ||
|
||
if (!config('terminal')) | ||
return; | ||
|
||
const {command} = options; | ||
await create({command}); | ||
|
||
CloudCmd.View.show(Terminal.element, { | ||
afterShow: () => { | ||
Terminal.focus(); | ||
}, | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"operation", | ||
"konsole", | ||
"terminal", | ||
"terminal-run", | ||
"cloud", | ||
"user-menu" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters