-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
100 lines (83 loc) · 2.87 KB
/
extension.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
'use strict';
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const St = imports.gi.St;
const ExtensionUtils = imports.misc.extensionUtils;
const Util = imports.misc.util;
const Me = ExtensionUtils.getCurrentExtension();
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const Config = imports.misc.config;
const SHELL_MAJOR = parseInt(Config.PACKAGE_VERSION.split('.')[0]);
const SHELL_MINOR = parseInt(Config.PACKAGE_VERSION.split('.')[1]);
let SSHQuickConnect = class SSHQuickConnect extends PanelMenu.Button {
_init() {
super._init(0.0, `${Me.metadata.name} Indicator`, false);
this.createIcon();
// Get ~/.ssh/config as string
const hostString = GLib.spawn_command_line_sync("cat " + GLib.get_home_dir() + "/.ssh/config")[1].toString();
// Parse string into array of Hosts
this.hosts = hostString.split('\n').join('{{NEWLINE}}').split('\r').join('{{NEWLINE}}').split('{{NEWLINE}}')
.map(item => item.trim())
.filter(item => item.indexOf('Host ') === 0)
.map(item => item = item.split('Host ')[1]);
// Add listeners
this.hosts.forEach(item => {
this.menu.addAction(item, e => this.sshToItem(item));
});
}
/**
* Spawns a subprocess that opens the defualt terminal and runs `ssh ${item}`
* @param {String} item The Host to ssh into
*/
sshToItem(item) {
const command = ['x-terminal-emulator', '-e', 'ssh', item];
try {
let proc = Gio.Subprocess.new( command, Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE );
// The callback is a force exit as there is no need for process communication
proc.communicate_utf8_async(null, null, () => proc.force_exit());
} catch (e) {
logError(e);
}
}
/**
* Boilerplate to create an icon from an SVG
*/
createIcon() {
const iconUri = `file://${Me.path}/icons/icon.svg`;
const iconFile = Gio.File.new_for_uri(iconUri);
const gicon = new Gio.FileIcon({ file: iconFile });
const icon = new St.Icon({
gicon: gicon,
style_class: 'system-status-icon'
});
this.add_child(icon);
}
}
/**
* EXTENSION BOILERPLATE
*/
// Compatibility with gnome-shell >= 3.32
if (SHELL_MAJOR > 39 || SHELL_MINOR > 30) {
SSHQuickConnect = GObject.registerClass(
{ GTypeName: 'SSHQuickConnect' },
SSHQuickConnect
);
}
let indicator = null;
function init() {
// log(`initializing ${Me.metadata.name} version ${Me.metadata.version}`);
}
function enable() {
// log(`enabling ${Me.metadata.name} version ${Me.metadata.version}`);
indicator = new SSHQuickConnect();
Main.panel.addToStatusArea(`${Me.metadata.name} Indicator`, indicator);
}
function disable() {
// log(`disabling ${Me.metadata.name} version ${Me.metadata.version}`);
if (indicator !== null) {
indicator.destroy();
indicator = null;
}
}