-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
171 lines (146 loc) · 5.81 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import * as PopupMenu from "resource:///org/gnome/shell/ui/popupMenu.js";
import * as Config from 'resource:///org/gnome/shell/misc/config.js';
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import St from "gi://St";
const ICON = "utilities-terminal-symbolic";
const BULLET = "pan-end-symbolic";
const ScrollableMenu = class ScrollableMenu extends PopupMenu.PopupMenuSection {
constructor() {
super();
const scrollView = new St.ScrollView();
this.innerMenu = new PopupMenu.PopupMenuSection();
const shellVersion = parseFloat(Config.PACKAGE_VERSION).toString().slice(0, 2);
if (shellVersion == 45) {
scrollView.add_actor(this.innerMenu.actor);
this.actor.add_actor(scrollView);
} else {
scrollView.add_child(this.innerMenu.actor);
this.actor.add_child(scrollView);
}
}
};
export default class LauncherExtension extends Extension {
constructor(metadata) {
super(metadata);
this._indicator = null;
this._menuId = null;
this._settings = null;
this._launcher = null;
this._menu = null;
this._path = null;
}
_appendLog(script, stdout, stderr) {
const logName = `${GLib.get_home_dir()}/.${this.metadata.name}.log`;
const logFile = Gio.File.new_for_path(logName);
const encoder = new TextEncoder();
const date = new Date();
const outputStream = logFile.append_to(Gio.FileCreateFlags.NONE, null);
outputStream.write(encoder.encode(`\n[${script}]: ${date}\n`), null);
outputStream.write(encoder.encode(`STDOUT:\n${stdout}`), null);
outputStream.write(encoder.encode(`STDERR:\n${stderr}`), null);
outputStream.close(null);
}
_fillMenu() {
this._menu.innerMenu.removeAll();
this._path = this._settings.get_string("path");
if (!this._path) {
return;
}
const shebangIcon = this._settings.get_boolean("shebang-icon");
const dafaultIcon = this._settings.get_string("default-icon");
const stripExt = this._settings.get_boolean("strip");
this._getScripts(this._path).forEach((script) => {
this._menu.innerMenu.addAction(
stripExt ? script.get_name().replace(/\.[^\.]+$/, "") : script.get_name(),
() => this._launchScript(script.get_name()),
shebangIcon ? script.get_icon() : dafaultIcon || BULLET
);
});
}
_getScripts(path) {
const directory = Gio.File.new_for_path(path);
if (!directory.query_exists(null)) {
return;
}
const enumerator = directory.enumerate_children(
"standard::name,standard::type,standard::icon",
Gio.FileQueryInfoFlags.NONE,
null
);
const scripts = [];
while (true) {
const fileInfo = enumerator.next_file(null);
if (!fileInfo) {
break;
}
const fileType = fileInfo.get_file_type();
if (fileType === Gio.FileType.REGULAR) {
scripts.push(fileInfo);
}
}
enumerator.close(null);
scripts.sort((a, b) => a.get_name().localeCompare(b.get_name()));
return scripts;
}
_launchScript(script) {
this._indicator.menu.toggle();
const command = [`${this._path}/${script}`];
try {
const proc = this._launcher.spawnv(command);
proc.communicate_utf8_async(null, null, (proc, res) => {
const [, stdout, stderr] = proc.communicate_utf8_finish(res);
const notify = this._settings.get_boolean("notify");
if (notify) {
if (stdout || stderr) {
Main.notify(this.metadata.name, `[${script}]: ${stdout || stderr}`);
} else {
Main.notify(this.metadata.name, `[${script}]: completed with exit code: ${proc.get_exit_status()}`);
}
}
const logging = this._settings.get_boolean("log");
if (logging) {
this._appendLog(script, stdout, stderr);
}
});
} catch (e) {
Main.notify(this.metadata.name, `[${script}]: ${e.toString().replace("GLib.SpawnError: ", "")}`);
}
}
_addIndicator() {
this._indicator = new PanelMenu.Button(0.5, this.metadata.name, false);
const icon = new St.Icon({
gicon: new Gio.ThemedIcon({ name: ICON }),
style_class: "popup-menu-icon",
});
this._indicator.add_child(icon);
this._menu = new ScrollableMenu();
this._indicator.menu.addMenuItem(this._menu);
this._indicator.menu.addAction("Settings", () => this.openPreferences(), "preferences-system-symbolic");
Main.panel.addToStatusArea(this.metadata.name, this._indicator);
this._menuId = this._indicator.menu.connect("open-state-changed", (open) => {
if (open) {
this._fillMenu();
}
});
}
enable() {
this._addIndicator();
this._settings = this.getSettings();
this._launcher = new Gio.SubprocessLauncher({
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
});
}
disable() {
this._indicator.menu.disconnect(this._menuId);
this._indicator.destroy();
this._indicator = null;
this._menuId = null;
this._menu = null;
this._settings = null;
this._launcher = null;
}
}