-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple-term.vala
More file actions
332 lines (294 loc) · 12.3 KB
/
simple-term.vala
File metadata and controls
332 lines (294 loc) · 12.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/******************************************************************************
* Copyright (C) 2017 Michael Hofmann <mh21@mh21.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
******************************************************************************/
// TODO:
// - proper dnd behavior for multiple file names
// see /usr/include/pcre2.h
const uint32 PCRE2_MULTILINE = 0x00000400u;
class TerminalWindow : Gtk.Window
{
private Vte.Terminal terminal;
private Pid pid;
private string? font;
private string fg;
private string bg;
private string palette;
private const string link_expr = "(((file|http|ftp|https)://)|(www|ftp)[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*[^]'\\.}>\\) ,\\\"])?";
private int link_tag;
private const Gdk.ModifierType key_mask = Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK |
Gdk.ModifierType.SUPER_MASK | Gdk.ModifierType.HYPER_MASK | Gdk.ModifierType.META_MASK;
public TerminalWindow(Gtk.Application app, string[] command, string? title,
string? font, string fg, string bg, string palette, string? role,
string? working_directory)
{
Object(application: app);
this.title = title != null ? title : string.joinv(" ", command);
this.font = font;
terminal = new Vte.Terminal();
add(terminal);
terminal.child_exited.connect(() => { destroy(); });
terminal.decrease_font_size.connect(decrease_font_size_cb);
terminal.increase_font_size.connect(increase_font_size_cb);
terminal.window_title_changed.connect(window_title_changed_cb);
terminal.key_press_event.connect(key_press_event_cb);
terminal.button_press_event.connect(button_press_event_cb);
terminal.drag_data_received.connect(drag_data_received_cb);
terminal.set_audible_bell(false);
terminal.set_cursor_blink_mode(Vte.CursorBlinkMode.SYSTEM);
terminal.set_cursor_shape(Vte.CursorShape.BLOCK);
terminal.set_mouse_autohide(true);
terminal.set_scroll_on_output(false);
terminal.set_scroll_on_keystroke(true);
terminal.set_scrollback_lines(-1);
update_colors(fg, bg, palette);
if (font != null)
terminal.set_font(Pango.FontDescription.from_string(this.font));
if (role != null)
this.set_role(role);
try {
var regex = new Vte.Regex.for_match(link_expr, -1, PCRE2_MULTILINE);
link_tag = terminal.match_add_regex(regex, 0);
terminal.match_set_cursor_name(link_tag, "hand");
} catch (Error e) {
printerr("Failed to compile regex \"%s\": %s\n", link_expr, e.message);
// ignored
}
Gtk.drag_dest_set(terminal, Gtk.DestDefaults.ALL, {}, Gdk.DragAction.COPY);
Gtk.drag_dest_add_uri_targets(terminal); // prefer URIs to text
Gtk.drag_dest_add_text_targets(terminal);
try {
terminal.spawn_sync(Vte.PtyFlags.DEFAULT,
working_directory,
command,
null, /* environment */
GLib.SpawnFlags.SEARCH_PATH,
null, /* child setup */
out pid, /* child pid */
null /* cancellable */);
} catch (Error e) {
printerr("Error: %s\n", e.message);
Idle.add(() => { destroy(); return false; });
}
show_all();
}
private static Gdk.RGBA? get_color(string str)
{
var color = Gdk.RGBA();
if (!color.parse(str)) {
printerr("Failed to parse \"%s\" as color.\n", str);
return null;
}
return color;
}
private static string convert_uris(string[] uris)
{
for (var i = 0; i < uris.length; ++i) {
var path = File.new_for_uri(uris[i]).get_path();
if (path != null)
uris[i] = Shell.quote(path);
}
return string.joinv(" ", uris);
}
private void edit_contents()
{
try {
FileIOStream iostream;
var file = GLib.File.new_tmp("console-output-XXXXXX.txt", out iostream);
OutputStream ostream = iostream.output_stream;
terminal.write_contents_sync(ostream, Vte.WriteFlags.DEFAULT, null);
var editor = Environment.get_variable("EDITOR");
if (editor == null || editor[0] == '\0')
editor = "vi";
new TerminalWindow(this.get_application(), { editor, file.get_path() }, null,
this.font, this.fg, this.bg, this.palette, "scrollback-edit", null);
// after 10 seconds the editor should have opened the file, remove
// it from the filesystem again
Timeout.add_seconds(10, () => { file.delete_async.begin(); return false; });
} catch (Error e) {
printerr("Error: %s\n", e.message);
// ignored
}
}
public override bool delete_event(Gdk.EventAny event)
{
var pty = terminal.get_pty();
if (pty == null)
return false;
var fd = pty.get_fd();
if (fd == -1)
return false;
var fgpid = Posix.tcgetpgrp(fd);
if (fgpid == -1 || fgpid == pid)
return false;
Gtk.MessageDialog dialog = new Gtk.MessageDialog(this,
0, Gtk.MessageType.WARNING,
Gtk.ButtonsType.CANCEL, "Running processes, close nevertheless?");
dialog.add_button("_Close", Gtk.ResponseType.ACCEPT);
dialog.set_default_response(Gtk.ResponseType.ACCEPT);
var result = dialog.run();
dialog.destroy();
return result != Gtk.ResponseType.ACCEPT;
}
private void decrease_font_size_cb()
{
terminal.set_font_scale(terminal.get_font_scale() / 1.2);
}
private void increase_font_size_cb()
{
terminal.set_font_scale(terminal.get_font_scale() * 1.2);
}
private void window_title_changed_cb()
{
set_title(terminal.get_window_title());
}
private bool button_press_event_cb(Gdk.EventButton event)
{
if (((event.state & key_mask) == Gdk.ModifierType.CONTROL_MASK) && (event.button == 1)) {
int tag;
var match = terminal.match_check_event(event, out tag);
if (match != null && tag == link_tag) {
try {
Gtk.show_uri_on_window(this, match, Gtk.get_current_event_time());
} catch (Error e) {
printerr("Error: %s\n", e.message);
// ignored
}
}
return false;
}
return false;
}
private bool key_press_event_cb(Gdk.EventKey event)
{
if ((event.state & key_mask) == (Gdk.ModifierType.SHIFT_MASK | Gdk.ModifierType.CONTROL_MASK)) {
switch (event.keyval) {
case Gdk.Key.C:
terminal.copy_clipboard_format(Vte.Format.TEXT);
return true;
case Gdk.Key.V:
terminal.paste_clipboard();
return true;
case Gdk.Key.S:
edit_contents();
return true;
}
}
if ((event.state & key_mask & ~Gdk.ModifierType.SHIFT_MASK) == Gdk.ModifierType.CONTROL_MASK) {
switch (event.keyval) {
case Gdk.Key.plus:
increase_font_size_cb();
return true;
case Gdk.Key.minus:
decrease_font_size_cb();
return true;
}
}
return false;
}
private void drag_data_received_cb(Gtk.Widget widget, Gdk.DragContext context,
int x, int y, Gtk.SelectionData selection_data, uint target_type, uint time)
{
// this is the only way to get a usable target list
Gdk.Atom[] targets = { selection_data.get_target() };
if (Gtk.targets_include_text(targets)) {
terminal.feed_child(selection_data.get_text().data);
} else if (Gtk.targets_include_uri(targets)) {
terminal.feed_child(convert_uris(selection_data.get_uris()).data);
}
Gtk.drag_finish(context, true, false, time);
}
public void update_colors(string fg, string bg, string palette)
{
this.fg = fg;
this.bg = bg;
this.palette = palette;
Gdk.RGBA[] colors = null;
foreach (var color in palette.split(","))
colors += get_color(color);
terminal.set_colors(get_color(fg), get_color(bg), colors);
}
}
class Application: Gtk.Application
{
public Application()
{
Object(application_id: "de.mh21.simple-term",
flags: ApplicationFlags.HANDLES_COMMAND_LINE);
}
public override int command_line(GLib.ApplicationCommandLine command_line)
{
var argv = command_line.get_arguments();
string[]? command = null;
string title = null;
string font = null;
string fg = "black";
string bg = "#ffffdd";
string palette = "#000000,#aa0000,#00aa00,#aa5400,#0000aa,#aa00aa,#00aaaa,#aaaaaa,#545454,#ff5454,#54ff54,#ffff54,#5454ff,#ff54ff,#54ffff,#ffffff";
string? role = null;
bool update_colors = false;
string? working_directory = null;
for (int i = 1; i < argv.length; ++i) {
if (argv[i] == "-display" || argv[i] == "-name" || argv[i] == "-geometry" ||
argv[i] == "-tn") {
++i;
} else if (argv[i] == "-T" || argv[i] == "-title") {
title = argv[++i];
} else if (argv[i] == "-fn") {
font = argv[++i];
} else if (argv[i] == "-fg") {
fg = argv[++i];
} else if (argv[i] == "-bg") {
bg = argv[++i];
} else if (argv[i] == "-palette") {
palette = argv[++i];
} else if (argv[i] == "-e") {
command = argv[i + 1:argv.length];
i = argv.length - 1;
} else if (argv[i] == "-role") {
role = argv[++i];
} else if (argv[i] == "-update") {
update_colors = true;
} else if (argv[i] == "-w") {
working_directory = argv[++i];
}
}
if (update_colors) {
foreach (var window in this.get_windows()) {
if (window is TerminalWindow) {
((TerminalWindow) window).update_colors(fg, bg, palette);
}
}
return 0;
}
if (command.length == 0) {
string shell = Vte.get_user_shell();
if (shell == null || shell[0] == '\0')
shell = Environment.get_variable("SHELL");
if (shell == null || shell[0] == '\0')
shell = "/bin/sh";
command = { shell };
}
new TerminalWindow(this, command, title, font, fg, bg, palette, role, working_directory);
return 0;
}
public static int main(string[] argv)
{
Environment.set_prgname("simple-term");
Environment.set_application_name("Terminal");
return new Application().run(argv);
}
}