-
Notifications
You must be signed in to change notification settings - Fork 17
/
tmux-url-select.pl
executable file
·220 lines (175 loc) · 5.12 KB
/
tmux-url-select.pl
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
#!/usr/bin/env perl
#
# tmux-url-select
# mit licensed
#
use strict;
use warnings;
### config
our $tmux_command = $ENV{TMUX_URL_SELECT_TMUX_CMD} || 'tmux';
use constant SHOW_STATUS_BAR => 1;
use constant VERBOSE_MESSAGES => 0;
use constant TMUX_WINDOW_TITLE => 'Select URL';
use constant TMUX_WINDOW_ID => 9999;
use constant HIDE_WINDOW => 1;
use constant PROMPT_COLOR => "\033[42;30m";
use constant ACTIVE_LINK_HIGHLIGHT => "\033[44;4m";
use constant NORMAL_LINK_HIGHLIGHT => "\033[94;1;4m";
# other options:
# - blue background, underlined: \033[44;4m
# - 256 color term light blue: \033[38;5;39m
# - bold bright blue: \033[94;1;4m
# - bright blue background: \033[104;4m
# - just underlined: \033[4m
# regex stolen from urxvtperls url-select.pl
my $url_pattern = qr{(
(?:https?://|ftp://|news://|git://|mailto:|file://|www\.)
[\w\-\@;\/?:&=%\$_.+!*\x27(),~#\x1b\[\]]+[\w\-\@;\/?&=%\$_+!*\x27(~]
)}x;
### config end
my $raw_buffer;
my $buffer;
my $buffer_first_newline_position;
my @matches;
my $match_count;
my $selection = 0;
# terminal helper functions
sub clear {
print "\033[H\033[2J";
}
sub display_status_bar {
my $is_first_line = shift;
my $position = $is_first_line ? "2;2" : "1;2";
print sprintf("\033[%sH%s URL select: (%s/%s) [j/k/y/o/q/enter] \033[0m", $position, PROMPT_COLOR, $selection+1, $match_count);
}
sub display_highlighted_buffer {
my $i = 0;
my $is_first_line = 0;
my $cb = sub {
if ($i++ == $selection) {
$is_first_line = 1 if ($+[0] < $buffer_first_newline_position);
return ACTIVE_LINK_HIGHLIGHT."$1\033[0m";
return;
}
return NORMAL_LINK_HIGHLIGHT."$1\033[0m" if NORMAL_LINK_HIGHLIGHT;
return $1;
};
print $buffer =~ s/($url_pattern)/&$cb()/ger;
return $is_first_line;
}
sub display_stuff {
clear();
my $is_first_line = display_highlighted_buffer();
display_status_bar($is_first_line) if SHOW_STATUS_BAR;
}
# tmux command helpers
sub tmux_display_message {
system $tmux_command, 'display-message', shift;
}
sub tmux_switch_to_last {
system $tmux_command, 'last-window';
}
sub tmux_select_my_window {
system $tmux_command, "select-window", "-t", TMUX_WINDOW_ID;
}
sub tmux_capture_pane {
system $tmux_command, "capture-pane", "-eJ";
}
sub tmux_get_buffer {
return `$tmux_command show-buffer`;
}
sub tmux_open_inner_window {
system $tmux_command, "new-window", "-dn", "", "-t", TMUX_WINDOW_ID, "$0 inner";
system $tmux_command, "setw", "-qt", TMUX_WINDOW_ID, "window-status-format", "";
system $tmux_command, "setw", "-qt", TMUX_WINDOW_ID, "window-status-current-format", "";
}
# other shell helpers
sub enable_canonical_mode {
# "canonical mode" to read char by char, thanks roger.
system "stty", "-icanon", "cbreak", "min", "1", "-echo";
}
sub single_quote_escape {
return "'".(shift =~ s/\'/%27/gr)."'";
}
# actions
sub fix_url {
my $url = shift;
# some silly url openers think ^www. urls are files
$url = "http://".$url if $url =~ /^www\./;
# clear out color codes
$url =~ s/\x1b\[[0-9;]*m//g;
return $url;
}
sub safe_exec {
my ($command, $message) = @_;
$SIG{CHLD} = 'IGNORE';
$SIG{HUP} = 'IGNORE';
unless (fork) {
tmux_display_message($message) if VERBOSE_MESSAGES;
exec $command;
}
}
sub launch_url {
my $url = fix_url(shift);
tmux_switch_to_last() if shift;
my $command = sprintf(
"%s %s",
$ENV{TMUX_URL_SELECT_OPEN_CMD} || 'xdg-open',
single_quote_escape($url)
);
safe_exec($command, "Launched ". $url);
}
sub yank_url {
my $url = fix_url(shift);
tmux_switch_to_last() if shift;
my $command = sprintf(
"echo %s | %s",
single_quote_escape($url),
$ENV{TMUX_URL_SELECT_CLIP_CMD} || 'xclip -i'
);
safe_exec($command, "Yanked ". $url);
}
# main functions
sub main_inner {
$raw_buffer = tmux_get_buffer();
system $tmux_command, "delete-buffer";
$buffer = $raw_buffer =~ s/\n$//r;
$buffer_first_newline_position = index($raw_buffer, "\n");
@matches = ($buffer =~ /$url_pattern/g);
$match_count = @matches;
exit 1 if !$match_count;
$selection = $#matches;
display_stuff();
enable_canonical_mode();
# switch to the tmux-url-select window now to avoid 'flickering'
tmux_select_my_window();
# main loop
while(defined($_ = getc)) {
$selection++ if /[jB]/;
$selection-- if /[kA]/;
$selection = ($_-1) if /[0-9]/;
$selection %= $match_count;
my $do_return = /[qyo\n]/;
yank_url($matches[$selection], $do_return) if /[yY]/;
launch_url($matches[$selection], $do_return) if /[\noO]/;
return if $do_return;
display_stuff();
}
}
sub main {
tmux_capture_pane();
@matches = tmux_get_buffer() =~ /$url_pattern/g;
$match_count = @matches;
if (!$match_count) {
tmux_display_message("No URLs");
system $tmux_command, "delete-buffer";
exit 0;
}
# open window here, backgrounded
tmux_open_inner_window();
}
if (!@ARGV) {
main();
} elsif ($ARGV[0] eq "inner") {
main_inner();
}