-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtaterm.vala
218 lines (183 loc) · 5.54 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
/* 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;
static const string FONT = "11";
static const string[] COLORS = {
"#000000", "#c00000", "#00c000", "#c0c000",
"#0000c0", "#c000c0", "#00c0c0", "#c0c0c0",
"#3f3f3f", "#ff3f3f", "#3fff3f", "#ffff3f",
"#3f3fff", "#ff3fff", "#3fffff", "#ffffff"
};
static const string FG_COLOR = "#c0c0c0";
static 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/
*/
static const string hex_encode = "%[0-9A-F]{2}";
static const string common_chars = "\\\\a-z0-9-._~!$&'()*+,;=";
static 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 (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;
/* TODO change to GDK_EVENT_PROPAGATE, when .vapi provides it */
return false;
});
}); // activate.connect()
} // Taterm()
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(err.message);
}
focus_in_event.connect(() => {
urgency_hint = false;
/* TODO change to GDK_EVENT_PROPAGATE, when .vapi provides it */
return false;
});
term.child_exited.connect(() => {
destroy();
});
/* term.beep.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();
} // Window()
} // class Window
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);
button_press_event.connect(handle_button);
match_add_gregex(uri_regex, 0);
}
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? */
/* TODO change to GDK_EVENT_PROPAGATE, when .vapi provides it */
return false;
}
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(err.message);
} finally {
match_uri = null;
}
} // if
} // check_regex
} // class Terminal
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(err.message);
}
return GLib.Environment.get_home_dir();
}
} // class Utils
}