forked from t-8ch/taterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaterm.vala
253 lines (215 loc) · 5.91 KB
/
taterm.vala
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
/* Copyright (c) 2012, 2013, 2014 Thomas Weißschuh
*
* 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, see <http://www.gnu.org/licenses/>.
*/
// modules: vte-2.90
using GLib;
using Gtk;
using Vte;
using Pango;
const string FONT = "11";
const double FONT_SCALE_STEP = 0.1;
const string[] COLORS = {
"#080808080808",
"#c08c535350ea",
"#7d169a004814",
"#ba208abd3333",
"#400c62957aad",
"#ba2055bc86b9",
"#4db49a009460",
"#cbcbc7c7bbbb",
"#7373710a6c38",
"#e9825c2859c0",
"#8c58be243333",
"#e315a53e3333",
"#5285a6d9e315",
"#e315602c9ed1",
"#5824c2f5ba20",
"#f4c0f0bce719"
};
const string FG_COLOR = "#ffffff";
const string BG_COLOR = "#000000";
static Pango.FontDescription font;
static Gdk.RGBA[] palette;
static Gdk.RGBA fg_color;
static Gdk.RGBA bg_color;
public static GLib.Regex uri_regex;
/*
Credits: http://snipplr.com/view/6889/regular-expressions-for-uri-validationparsing/
*/
const string hex_encode = "%[0-9A-F]{2}";
const string common_chars = "\\\\a-z0-9-._~!$&'()*+,;=";
const string regex_string =
"([a-z0-9][a-z0-9+.-]+):" + // scheme
"(//)?" + // it has an authority
"(([:"+common_chars+"]|"+hex_encode+")*@)?" + // userinfo
"(["+common_chars+"]|"+hex_encode+"){3,}" + // host
"(:\\d{1,5})?" + // port
"(/([:@/"+common_chars+")]|"+hex_encode+")*)?" + // path
// v be flexible with shell escaping here
"(\\\\?\\?(["+common_chars+":/?@]|"+hex_encode+")*)?" + // query string
"(\\\\?\\#(["+common_chars+"+:/?@]|"+hex_encode+")*)?" + // fragment
"(?=[\\s)}>\"\',;])" // look ahead
;
public static int main(string[] args)
{
try {
var regex_flags = RegexCompileFlags.CASELESS | RegexCompileFlags.OPTIMIZE;
uri_regex = new GLib.Regex(regex_string, regex_flags);
} catch (GLib.RegexError err) {
GLib.assert_not_reached();
}
font = Pango.FontDescription.from_string(FONT);
for (int i = 0; i < COLORS.length; i++) {
Gdk.RGBA color = Gdk.RGBA();
color.parse(COLORS[i]);
palette += color;
}
fg_color.parse(FG_COLOR);
bg_color.parse(BG_COLOR);
return new Taterm().run();
}
class Taterm : Gtk.Application
{
string pwd = GLib.Environment.get_home_dir();
public Taterm()
{
Object(application_id: "de.t-8ch.taterm");
activate.connect(() => {
var new_win = new Window(pwd);
add_window(new_win);
new_win.focus_out_event.connect(() => {
pwd = new_win.pwd;
return Gdk.EVENT_PROPAGATE;
});
});
}
class Window : Gtk.Window
{
Vte.Terminal term;
GLib.Pid shell;
public string pwd;
string[] targs;
public signal void pwd_changed(string pwd);
public Window(string pwd)
{
this.pwd = pwd;
term = new Terminal();
targs = { Vte.get_user_shell() };
try {
term.spawn_sync(Vte.PtyFlags.DEFAULT, pwd, targs, null, 0, null, out shell);
} catch (Error err) {
stderr.printf("%s\n", err.message);
}
focus_in_event.connect(() => {
urgency_hint = false;
return Gdk.EVENT_PROPAGATE;
});
term.child_exited.connect(() => {
destroy();
});
term.bell.connect(() => {
urgency_hint = true;
});
term.window_title_changed.connect(() => {
title = term.window_title;
var newpwd = Utils.cwd_of_pid(shell);
if (newpwd != this.pwd) {
this.pwd = newpwd;
pwd_changed(newpwd);
}
});
add(term);
show_all();
}
}
class Terminal : Vte.Terminal
{
string match_uri = null;
public Terminal()
{
cursor_blink_mode = Vte.CursorBlinkMode.OFF;
scrollback_lines = -1; /* infinity */
pointer_autohide = true;
set_font(font);
set_colors(fg_color, bg_color, palette);
key_press_event.connect(handle_key);
button_press_event.connect(handle_button);
match_add_gregex(uri_regex, 0);
}
private bool handle_key(Gdk.EventKey event)
{
bool handled = false;
if (event.state == Gdk.ModifierType.CONTROL_MASK) {
switch (event.keyval) {
case Gdk.Key.minus:
font_scale /= 1 + FONT_SCALE_STEP;
handled = true;
break;
case Gdk.Key.plus:
font_scale *= 1 + FONT_SCALE_STEP;
handled = true;
break;
case Gdk.Key.@0:
font_scale = 1;
handled = true;
break;
}
}
return handled ? Gdk.EVENT_STOP : Gdk.EVENT_PROPAGATE;
}
private bool handle_button(Gdk.EventButton event)
{
if (event.button == Gdk.BUTTON_PRIMARY) {
check_regex(
(long) event.x/get_char_width(),
(long) event.y/get_char_height()
);
}
/* continue calling signalhandlers, why should we stop? */
return Gdk.EVENT_PROPAGATE;
}
private void check_regex(long x_pos, long y_pos)
{
/*
this tag shouldn't be necessary but if we don't pass it to match_check()
the whole thing just segfaults
*/
int tag;
match_uri = match_check(x_pos, y_pos, out tag);
if (match_uri != null) {
try {
Gtk.show_uri(null, match_uri, Gdk.CURRENT_TIME);
} catch (Error err) {
stderr.printf("%s\n", err.message);
} finally {
match_uri = null;
}
}
}
}
class Utils
{
public static string cwd_of_pid(GLib.Pid pid)
{
var cwdlink = @"/proc/$((int)pid)/cwd";
try {
return GLib.FileUtils.read_link(cwdlink);
} catch (FileError err) {
stderr.printf("%s\n", err.message);
}
return GLib.Environment.get_home_dir();
}
}
}