Skip to content

Commit

Permalink
Add support for environment variables configuration (#7)
Browse files Browse the repository at this point in the history
* Add support for environment variables configuration

* Add tmate support
  • Loading branch information
doronbehar authored Sep 21, 2023
1 parent 65917c8 commit 3665185
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ usually used with the latter.
Depends on `perl`, `tmux` and `stty`.

Optional and configurable: `xdg-open` (can be any url opener or browser) and
`xclip` (for yank)
`xclip` (for yank). Use the environment variables `TMUX_URL_SELECT_CLIP_CMD` and `TMUX_URL_SELECT_OPEN_CMD` to change these commands.

## Installation

Expand Down Expand Up @@ -101,3 +101,7 @@ should be fixed now.
Q: Why perl? It's dead and it sucks, cool kids use node.js nowadays.

A: It's fun. Fun things are fun.

Q: Can I use it with [Tmate](https://tmate.io/)?

A: Yes, just set in your environment: `TMUX_URL_SELECT_TMUX_CMD=tmate`
35 changes: 21 additions & 14 deletions tmux-url-select.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

### config

use constant COMMAND => 'xdg-open %s';
use constant YANK_COMMAND => 'echo %s | xclip -i';
our $tmux_command = $ENV{TMUX_URL_SELECT_TMUX_CMD} || 'tmux';

use constant SHOW_STATUS_BAR => 1;
use constant VERBOSE_MESSAGES => 0;
Expand Down Expand Up @@ -81,29 +80,29 @@ sub display_stuff {
# tmux command helpers

sub tmux_display_message {
system 'tmux', 'display-message', shift;
system $tmux_command, 'display-message', shift;
}

sub tmux_switch_to_last {
system 'tmux', 'last-window';
system $tmux_command, 'last-window';
}

sub tmux_select_my_window {
system "tmux", "select-window", "-t", TMUX_WINDOW_ID;
system $tmux_command, "select-window", "-t", TMUX_WINDOW_ID;
}

sub tmux_capture_pane {
system "tmux", "capture-pane", "-eJ";
system $tmux_command, "capture-pane", "-eJ";
}

sub tmux_get_buffer {
return `tmux show-buffer`;
return `$tmux_command show-buffer`;
}

sub tmux_open_inner_window {
system "tmux", "new-window", "-dn", "", "-t", TMUX_WINDOW_ID, "$0 inner";
system "tmux", "setw", "-qt", TMUX_WINDOW_ID, "window-status-format", "";
system "tmux", "setw", "-qt", TMUX_WINDOW_ID, "window-status-current-format", "";
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
Expand Down Expand Up @@ -143,22 +142,30 @@ sub launch_url {
my $url = fix_url(shift);
tmux_switch_to_last() if shift;

my $command = sprintf(COMMAND, single_quote_escape($url));
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(YANK_COMMAND, single_quote_escape($url));
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 delete-buffer";
system $tmux_command, "delete-buffer";

$buffer = $raw_buffer =~ s/\n$//r;
$buffer_first_newline_position = index($raw_buffer, "\n");
Expand Down Expand Up @@ -198,7 +205,7 @@ sub main {

if (!$match_count) {
tmux_display_message("No URLs");
system "tmux delete-buffer";
system $tmux_command, "delete-buffer";
exit 0;
}

Expand Down

0 comments on commit 3665185

Please sign in to comment.