-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.js
141 lines (119 loc) · 3.25 KB
/
term.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var hterm = require('hterm-umdjs').hterm
var lib = require('hterm-umdjs').lib
var spawn = require('child_pty').spawn
var cmd = require("child_process").execSync
var WebSocket = require('ws');
var path = require('path');
const eNotify = require('node-notifier');
const { app, BrowserWindow, shell, Menu } = require('electron').remote;
var hostname = cmd("hostname").toString()
var defaultShell = require('default-shell')
var ws = new WebSocket('ws://localhost:3000');
hterm.defaultStorage = new lib.Storage.Memory();
var t = new hterm.Terminal()
var pty = spawn(defaultShell, [])
function isWebpage(data) {
if ( typeof data === 'string' && data.match(/https?/) ) {
return true
}
return false
}
function isImage(data) {
if ( typeof data === 'string' && data.indexOf(".png") != -1 || data.indexOf(".gif") != -1 ) {
return true
}
return false
}
function shouldOpenInNewWindow(data) {
if (data && (data.match(/o /) || data.match(/open /))) {
return true
}
return false
}
function openImageInline(data) {
img = `<br /><img src='${data}'> </img>`
t.io.println(data)
t.getRowNode(t.getCursorRow()).innerHTML += img
}
function popOpenLink(link) {
let c = new BrowserWindow({modal: true, show: false})
c.loadURL(link)
c.once('ready-to-show', () => { c.show() })
}
function inputHandler(data) {
let command = t.getRowText(t.getCursorRow()).split("\(master\)")[1]
if (command) {
command = command.slice(3, command.length)
if ( command.length > 0) {
msg["message"] = command
let l_msg = JSON.stringify(msg)
console.log(`62 command ${l_msg}`)
ws.send(l_msg)
}
}
if (!command)
command = data
console.log(command)
if (shouldOpenInNewWindow(command)) {
console.log(`pop open ${command}`)
url = command.split(" ")[1]
popOpenLink(url)
} else if (isImage(command)) {
console.log(`inline ${command}`)
openImageInline(command)
} else {
pty.stdin.write(data);
}
}
pty.stdout.on('data', (data) => {
let local_str = data.toString('utf8')
t.io.print(local_str)
});
msg = {user: hostname, room: "", message: ""}
function notify(title, body) {
eNotify.notify({
title: title,
text: body,
icon: path.join(__dirname, 'images/sudo.jpg')
})
}
t.onTerminalReady = function() {
ws.on('open', function open() {
ws.send(JSON.stringify(msg));
notify("Sudo", "You are connected to hubot")
});
ws.on('message', function(data, flags) {
try {
console.log(data)
data = JSON.parse(data)
if (isWebpage(data[0])) {
popOpenLink(data[0])
} else {
//t.clear()
//t.deleteLines(1)
if ( typeof data == 'string' ) {
pty.stdin.write(ele);
} else {
for (let ele of data) {
if (typeof ele == 'object') {
var pj = JSON.stringify(ele, null, 2);
notify("Hubot says", pj)
t.io.println("")
} else {
notify("Hubot says", ele)
t.io.println("")
}
}
}
}
} catch(e) {
console.log(e)
pty.stdin.write(data);
}
});
var io = t.io.push();
io.onVTKeystroke = inputHandler;
io.sendString = inputHandler;
}
t.decorate(document.getElementById('terminal'));
t.installKeyboard()