-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
186 lines (163 loc) · 5.42 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const _domain = 'git-monitor';
const GETTEXT_DOMAIN = _domain;
const { GObject, St, Gio, GLib, Pango, Clutter } = imports.gi;
const Gettext = imports.gettext.domain(GETTEXT_DOMAIN);
const _ = Gettext.gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Me = ExtensionUtils.getCurrentExtension();
const ByteArray = imports.byteArray;
const Util = imports.misc.util;
function lg(s) { log("===" + _domain + "===>" + s); }
let gitDirs = [];
const configname = _domain + ".json"; //没有界面时,还不能改成schmes方式。
const configfile = GLib.get_user_config_dir() + "/" + configname;
const configorig = Me.path + "/" + configname;
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init() {
super._init(0.0, _domain);
this.stock_icon = Gio.icon_new_for_string(Me.path + "/org.gnome.gitg-symbolic.svg");
this.add_child(new St.Icon({
gicon : this.stock_icon,
style_class : 'system-status-icon'
}));
this.connect("button-press-event", (actor, event) => {
if (event.get_button() == 2) { // refresh
this.menu._getMenuItems().forEach((j) => { j.destroy(); });
this.refresh();
}
if (event.get_button() == 3) { // open configfile
Gio.app_info_launch_default_for_uri(`file://${configfile}`, global.create_app_launch_context(0, -1));
}
});
if (!GLib.file_test(configfile, GLib.FileTest.IS_REGULAR)) {
const [ok, content] = GLib.file_get_contents(configorig);
if (ok) {
GLib.file_set_contents(configfile, content);
}
}
this.refresh();
}
refresh() { // re-read json file, check all dirs, refresh menu.
try {
if (GLib.file_test(configfile, GLib.FileTest.IS_REGULAR)) {
const [ok, content] = GLib.file_get_contents(configfile);
if (ok) {
const obj = JSON.parse(ByteArray.toString(content));
if (obj.dirs) {
gitDirs = [];
for (let i of obj.dirs) {
i = i.replace(/^~/, GLib.get_home_dir());
gitDirs.push(i);
}
}
}
}
} catch (e) { throw e; }
for (let i of gitDirs) {
const r = this.lsDir(i);
if (!r) continue;
for (let j of r) {
this.async_cmd_git_st(i, j);
}
}
}
async_cmd_git_st(root, path) {
if (GLib.chdir(root) != 0) return null; // need improve
if (GLib.chdir(path) != 0) return null; // need improve
try {
let proc = Gio.Subprocess.new(
[ 'git', 'status' ],
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
let [, stdout, stderr] = proc.communicate_utf8_finish(res);
if (proc.get_successful()) {
const l = stdout.split("\n").filter(item => item.match(/[::](?=\ )/));
if (l.length > 0) {
this.add_menu(root, path, true);
for (let i of l) {
this.add_menu(root + "/" + path, i, false);
}
}
} else {
log("err: " + stderr);
}
} catch (e) { logError(e); }
});
} catch (e) { logError(e); }
};
add_menu(path, text, isDir) {
let item;
if (isDir) {
item = new PopupMenu.PopupImageMenuItem(text, this.stock_icon);
item.label.clutter_text.set_line_alignment(Pango.Alignment.RIGHT);
const pango = text.bold().italics().fontcolor("#F29F9C").replace(/font/g, "span");
item.label.clutter_text.set_markup(pango);
item.connect('activate', (actor, event) => {
if (event.get_button() == 3) {
Gio.app_info_launch_default_for_uri(`file://${path}/${text}`, global.create_app_launch_context(0, -1));
//~ Util.spawn(['gnome-terminal', `--working-directory='${path}/${text}' -- bash -c 'git status; bash'`]); //no work correctly.
return Clutter.EVENT_STOP;
}
//~ GLib.spawn_command_line_async(`gnome-terminal --working-directory='${path}/${text}' -- bash -c 'git status; bash'`);
Util.spawn(['git', 'difftool', '-d', path+"/"+text]);
});
} else {
item = new PopupMenu.PopupMenuItem(text);
item.connect('activate', (actor, event) => {
let f = text;
f = f.replace(/^.*[::]\ */, '').trim();
if (event.get_button() == 3) {
Gio.app_info_launch_default_for_uri(`file://${path}/${f}`, global.create_app_launch_context(0, -1));
return Clutter.EVENT_STOP;
}
if (GLib.chdir(path) != 0) return;
Util.spawn(['git', 'difftool', f]);
});
}
this.menu.addMenuItem(item);
};
lsDir(path) { // return an array of git dirs in path.
if (!this.isDir(path)) return null;
const dir = Gio.File.new_for_path(path);
let fileEnum;
let r = [];
try {
fileEnum = dir.enumerate_children('standard::name', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
} catch (e) { fileEnum = null; }
if (fileEnum != null) {
let info;
while (info = fileEnum.next_file(null)) {
const f = info.get_name();
if (this.isDir(path + "/" + f + "/.git")) {
r.push(f);
}
}
}
return r;
}
isDir(path) {
return GLib.file_test(path, GLib.FileTest.IS_DIR);
}
});
class Extension {
constructor(uuid) {
this._uuid = uuid;
ExtensionUtils.initTranslations(GETTEXT_DOMAIN);
}
enable() {
this._indicator = new Indicator();
Main.panel.addToStatusArea(this._uuid, this._indicator);
}
disable() {
this._indicator.destroy();
this._indicator = null;
}
}
function init(meta) {
return new Extension(meta.uuid);
}