-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
116 lines (87 loc) · 2.48 KB
/
index.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
'use strict';
module.exports = (pluginContext) => {
const shell = pluginContext.shell;
const logger = pluginContext.logger;
const exec = require('child_process').exec;
const registry_key = "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions";
var stored_sessions = [];
function loadPuttySessions() {
logger.log('loading Putty sessions...')
exec("reg query " + registry_key, (error, stdout, stderr) => {
if (error) {
logger.log("putty reg error: " + stderr);
return;
}
stored_sessions = [];
stdout.split("\r\n").forEach((item) => {
if (item.trim().length === 0) return;
let session_name = decodeURI(item.slice(registry_key.length + 1));
if (session_name == "Default Settings") return;
stored_sessions.push(session_name);
});
stored_sessions.sort();
logger.log("putty sessions: " + stored_sessions);
});
}
function startup() {
loadPuttySessions();
}
function search(query, res) {
let found_matches = 0;
let has_query = false;
let filter_call = (item) => { return true; };
// search query
if (!(!query || query.trim().length === 0)) {
query = query.trim();
has_query = true;
filter_call = (item) => {
let found = item.toLowerCase().startsWith(query.toLowerCase()) ? true : false;
if (found) found_matches++;
return found;
}
}
stored_sessions.forEach((item) => {
if (filter_call(item)) {
res.add({
id: 'session',
title: item,
payload: item,
desc: 'open PuTTY session \"' + item + '\"',
icon: '#fa fa-chevron-right'
});
}
});
// add "putty call"
res.add({
id: 'q',
title: has_query ? 'putty.exe ' + query : query,
payload: query,
desc: 'Run PuTTY command',
icon: '#fa fa-search'
});
// other commands
res.add({
id: 'reload',
title: 'reload',
desc: 'reload putty sessions',
icon: '#fa fa-refresh'
});
}
function execute(id, payload) {
logger.log("id: " + id, "payload: " + payload);
switch (id) {
case 'q':
if (payload.trim().length > 0) {
exec('START putty ' + payload);
}
break;
case 'session':
exec('START putty -load \"' + payload + '\"');
break;
case 'reload':
loadPuttySessions();
break;
}
}
return { startup, search, execute };
};