From 7062a6f7fbd43910e2b08e3829687bf506a55eb1 Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 28 Aug 2020 00:06:55 +0530 Subject: Add fortunes, update README, update cpanfile --- README | 25 ++++++++++++-- cpanfile | 1 + lyra.pl | 114 +++++++++++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 104 insertions(+), 36 deletions(-) diff --git a/README b/README index 2a3ecfe..0d022e8 100644 --- a/README +++ b/README @@ -5,6 +5,25 @@ ━━━━━━━━━ -Lyra is a simple cli program to manage fortune files. I wrote this for -my quotes file. Previously I used a simple `sh(1)' function to append -quotes & `more(1)' to read them, Lyra will replace both. +Lyra is a simple cli program to manage fortune files. It can read +directly from plain-text files, creating datafiles through `strfile' is +not required. + +This also means that it has less features compared to fortune(6). +Default directory is assumed to be `$HOME/fortunes'. + +This program is not useful if you have `strfile' installed, just use it +& place the datafiles in fortune directory. + + +1 Usage +═══════ + + You have to run `lyra mirror' once before using it, it'll mirror all + the fortune files in `%fortune'. + ┌──── + │ lyra mst # Print a random fortune from mst fortunes. + │ lyra mirror # Mirror fortune files. + │ lyra latest # Get latest fortune files. + │ lyra ls # List all fortune files. + └──── diff --git a/cpanfile b/cpanfile index 73229cb..f850fca 100644 --- a/cpanfile +++ b/cpanfile @@ -1,2 +1,3 @@ requires 'Path::Tiny', '0.114'; requires 'IPC::Run3', '0.048'; +requires 'HTTP::Simple', '0.004'; diff --git a/lyra.pl b/lyra.pl index 3fa0700..43568f2 100755 --- a/lyra.pl +++ b/lyra.pl @@ -7,49 +7,97 @@ use feature 'say'; use Path::Tiny; use IPC::Run3; -my %dispatch = ( - "edit" => \&edit, - "random" => \&random, - "help" => \&HelpMessage, +my $fortune_dir = "$ENV{HOME}/fortunes"; +my %fortunes = ( + # mst quotes. + mst => "http://www.trout.me.uk/quotes.txt", + + # All these are from rindolf's website. + sholmif => "https://www.shlomifish.org/humour/fortunes/shlomif", + "shlomif-fav" + => "https://www.shlomifish.org/humour/fortunes/shlomif-fav", + "shlomif-factoids" + => "https://www.shlomifish.org/humour/fortunes/shlomif-factoids", + "sholmif-email-sig" + => "https://raw.githubusercontent.com/shlomif/shlomif-email-signature/master/shlomif-sig-quotes.txt", + + # Quotes from the Joel on Software site. + # (http://www.joelonsoftware.com/) + "joel-on-software" + => "https://www.shlomifish.org/humour/fortunes/joel-on-software", + + # Quotes from the essays and writings of Paul Graham. + # (http://www.paulgraham.com/) + "paul-graham" + => "https://www.shlomifish.org/humour/fortunes/paul-graham", + + # “The Rules of Open-Source Programming”. + osp_rules => "https://www.shlomifish.org/humour/fortunes/osp_rules", + + # Excerpts from the online Subversion folklore. + # (http://subversion.tigris.org/) + "subversion" + => "https://www.shlomifish.org/humour/fortunes/subversion", + + # A collection of conversations from Freenode’s #perl . + "sharp-perl" + => "https://www.shlomifish.org/humour/fortunes/sharp-perl", + # A collection of conversations from Freenode’s ##programming . + "sharp-programming" + => "https://www.shlomifish.org/humour/fortunes/sharp-programming", + + # katspace quotes (ref: rindolf's website). + "katspace_sayings" + => "http://katspace.com/fandom/quotes/sayings", + "katspace_more-sayings" + => "http://katspace.com/fandom/quotes/kaijen", + "katspace_books" + => "http://katspace.com/fandom/quotes/book", + "katspace_quotes" + => "http://katspace.com/fandom/quotes/quotes", ); -if ( $ARGV[0] - and $dispatch{ $ARGV[0] } ) { - $dispatch{ $ARGV[0] }->(); +if ( $ARGV[0] ) { + if ($fortunes{ $ARGV[0] }) { + my $fortune_file = "$fortune_dir/$ARGV[0]"; + random($fortune_file); } + elsif ( $ARGV[0] eq "latest") { get_latest(); } + elsif ( $ARGV[0] eq "mirror") { get_mirror(); } + elsif ( $ARGV[0] eq "ls") { run3[ "ls", $fortune_dir] } } elsif ( scalar @ARGV == 0 ) { - HelpMessage(); + say "Usage: lyra "; } else { - say "lyra: no such option"; + say "lyra: no such fortune"; } -sub HelpMessage { - say qq{Usage: - edit - Edit the file. (opens in \$EDITOR) - random - Print a random fortune. - help - Show this text.} +sub random { + my $fortune_file = shift @_; + my $file = path($fortune_file)->absolute; + my @fortunes = split/\n%\n/, $file->slurp; + say $fortunes[ rand @fortunes ]; # Print random fortune. } -sub fortune_file_path { - my $fortune_file = "$ENV{HOME}/quotes.txt"; - $fortune_file = $ARGV[1] if $ARGV[1]; # Use $ARGV[1] as path if it exists. - return $fortune_file; +sub get_latest { + foreach my $fortune (sort keys %fortunes) { + ftp("$fortune_dir/$fortune", $fortunes{$fortune}); + } } -sub edit { - my @editor = split(/ /, $ENV{EDITOR}); - push @editor, "vi" - unless scalar @editor; # Push "vi" if @editor is empty. +sub get_mirror { + require HTTP::Simple; - my $fortune_file = fortune_file_path(); - run3 [ @editor, $fortune_file ]; -} + # Ignore a warning, next line would've printed a warning. + no warnings 'once'; + $HTTP::Simple::UA->verify_SSL(1); -sub random { - my $fortune_file = fortune_file_path(); - my $file = path($fortune_file)->absolute; - my @fortunes = split/\n%\n/, $file->slurp; - say $fortunes[ rand @fortunes ]; # Print random fortune. + foreach my $fortune (sort keys %fortunes) { + say "$fortune $fortunes{$fortune}"; + my $status = + HTTP::Simple::getstore($fortunes{$fortune}, + "$fortune_dir/$fortune"); + warn "[WARN] Failed to fetch latest fortune\n" + unless HTTP::Simple::is_success($status); + } } + +sub ftp { run3 ["ftp", "-mvo", @_]; } -- cgit 1.4.1-2-gfad0