From 74ecd05e6a9ccf8cc2e4019b50d4b0839f20264f Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 31 Jan 2022 21:19:05 -0500 Subject: Add bookmark features --- gmi.pl | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'gmi.pl') diff --git a/gmi.pl b/gmi.pl index 0ff093c..1f7d1f6 100755 --- a/gmi.pl +++ b/gmi.pl @@ -10,7 +10,7 @@ use warnings; use feature qw(refaliasing); no warnings qw(experimental::refaliasing); -our $VERSION = 'v0.0.33'; +our $VERSION = 'v0.0.34'; # TODO: # back() only works once; should fix this @@ -51,6 +51,7 @@ my @doc; my @links; my $current_url = ""; my @history; +my @bookmarks; my $prompt = ""; my $pre_block = 0; my @status_code; @@ -115,6 +116,10 @@ my %commands = ( 'navh' => [sub { nav(1, $_[0]) }, 'Navigate to a URL specified from history (use hist)'], 'nh' => [sub { nav(1, $_[0]) }, 'Alias of `navh\'.'], + 'navb' => [sub { nav(2, $_[0]) }, 'Navigate to a URL specified from bookmarks (use bookmarks)'], + 'nb' => [sub { nav(2, $_[0]) }, 'Alias of `navh\'.'], + + 'root' => [sub { root() }, 'Go to the root of the current URL.\n' . 'Exp: gemini://capsule.com/blog/2020112.gmi -> gemini://capsule.com/' ], @@ -164,6 +169,12 @@ my %commands = ( 'keep-history' => [sub {toggle($config{keep_history})}, 'Toggle whether to save history to disk or not,' . ' after \'exit\'. By default it does not.' ], 'clear-history' => [sub { undef(@history) }, 'Clears your history. (The file itself is very parsable)' ], + + 'bookmark-add' => [sub { bookmark_add($_[0]) }, 'Add bookmark. ' . + 'No argument means the current url, otherwise what was supplied.'], + 'bookmark-del' => [sub { bookmark_del($_[0]) }, 'Delete bookmark by number' . + ' (use \'bookmarks\' to see numbers).'], + 'bookmarks' => [sub { bookmarks(); }, 'Returns your bookmarks. Use \'nb\' or \'navb\' along with this.'], ); my @completions = keys %commands; @@ -690,12 +701,14 @@ sub links { sub nav { # Setting 0: from links # Setting 1: from history + # Setting 2: from bookmarks my ($setting,$n) = @_; ### $n ### $setting ### @_ if (! $n or $n !~ /\d+/) { - print $OUT "Try 'links' or 'hist' then 'nav x', where x is the number.\n"; + print $OUT "Try 'links', 'hist', or 'bookmarks' then 'navY X', where x is the number and Y. " . + "is nav mode.\n"; return 1; } $n -= 1; @@ -706,6 +719,8 @@ sub nav { $link = $links[$n][0]; } elsif ($setting == 1 and @history and exists $history[$n][0]) { $link = $history[$n][0]; + } elsif ($setting == 2 and @bookmarks and exists $bookmarks[$n]) { + $link = $bookmarks[$n]; } else { print $OUT "'" . ($n+1) ."' isn't available.\n"; @@ -1111,6 +1126,38 @@ sub cert_del { } } +sub bookmarks { + my $counter = 1; + for (@bookmarks) { + print $OUT "[$counter] $_\n"; + $counter++; + } +} + +sub bookmark_del { + my ($b) = @_; + if ($b and $b =~ m/^\d+$/) { + if (@bookmarks[$b-1]) { + print $OUT "Removing $bookmarks[$b-1]"; + splice(@bookmarks,$b-1,1); + } + ### @bookmarks + } + else { + print $OUT "Doing nothing."; + } +} + +sub bookmark_add { + my ($b) = @_; + if ($b) { + push(@bookmarks, $b); + } + else { + push(@bookmarks, $current_url); + } +} + sub save_config { my $toml = to_toml(\%config); open(my $FH, '>', "$xdg_config/config.toml"); @@ -1139,6 +1186,15 @@ sub load_config { close $FH; } ### @history + if (-e "$xdg_data/bookmarks") { + open(my $FH, '<', "$xdg_data/bookmarks"); + while (<$FH>) { + chomp($_); + push(@bookmarks, $_); + } + close $FH; + } + ### @bookmarks } sub begin_exit { @@ -1150,5 +1206,11 @@ sub begin_exit { } close $FH; } + # over writes the bookmarks with old and new + open(my $FH, '>', "$xdg_data/bookmarks"); + for my $bookmark (@bookmarks) { + print $FH "$bookmark\n"; + } + close $FH; exit 0; } -- cgit v1.2.3