Last active
June 24, 2023 07:51
-
-
Save kugland/443532edf52fb4694d83a4dd396c99d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S perl -CSDA | |
use 5.03; | |
use strict; | |
use warnings; | |
use utf8; | |
use open ':std', ':encoding(UTF-8)'; | |
use URI; | |
use IPC::Open2; | |
use List::Util qw(pairmap); | |
sub load_bookmarks { | |
# Open ~/.my-bookmarks.txt | |
open my $fh, '<', "$ENV{HOME}/.my-bookmarks.txt" or die $!; | |
# Read the file | |
my @bookmarks = <$fh>; | |
# Close the file | |
close $fh; | |
@bookmarks = pairmap { "$a\n<small>$b</small>\N{NUL}" } | |
grep { !m/^\s*$/ && !m/^\s*#/ } | |
map { s/^\s*:?\s*|\s+$//gr; } | |
@bookmarks; | |
# Sort the bookmarks | |
@bookmarks = sort @bookmarks; | |
# Return the bookmarks | |
return join '', @bookmarks; | |
} | |
sub pipe_to_rofi { | |
my ( $bookmarks ) = @_; | |
my $rofi = q{ rofi -dmenu -i -sep '\x00' -no-custom -p bookmarks -theme ~/.config/sway/scripts/my-bookmarks }; | |
my $pid = open2(my $rofi_output, my $pipe, $rofi) or die $!; | |
binmode $pipe, ":raw:encoding(UTF-8)"; | |
binmode $rofi_output, ":raw:encoding(UTF-8)"; | |
# Open pipe to rofi and get rofi's output | |
print {$pipe} $bookmarks; | |
close $pipe; | |
# Get the exit status | |
my $exit_status = $? >> 8; | |
if ($exit_status != 0) { | |
return undef; | |
} else { | |
# Get rofi's output | |
my $output = do { local $/; <$rofi_output> }; | |
# Close rofi's output | |
close $rofi_output; | |
# Return the output | |
return $output; | |
} | |
} | |
sub handle_response { | |
my ( $response ) = @_; | |
my ($title, $url) = split /\n/, $response; | |
if (defined $url) { | |
$url =~ s{ \A \Q<small>\E (.*) \Q</small>\E \z }{$1}msx; | |
my $parsed_url = URI->new($url); | |
if ( $parsed_url->host !~ /[.]onion/ ) { | |
system "xdg-open", $url; | |
} else { | |
print "Opening of .onion links is not implemented yet\n"; | |
} | |
} | |
} | |
my $bookmarks = load_bookmarks(); | |
my $response = pipe_to_rofi($bookmarks); | |
if (defined $response) { | |
handle_response($response); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@theme "~/.config/rofi/config.rasi" | |
listview { | |
spacing: 25; | |
} | |
element-text { | |
markup: true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment