From be00783294a5ff8f6601b1109f55bb83282b27d6 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 18 Nov 2020 11:25:47 +0530 Subject: Remove scripts, improve lyra Now it prints fortunes from all directories by default, if the user inputs an argument then it'll load that fortune file if it exists in mentioned directories. --- .gitignore | 1 + README | 12 ------- cpanfile | 2 -- lyra.pl | 49 +++++++++++++++++---------- scripts/fortunes.pl | 97 ----------------------------------------------------- scripts/kirch.sh | 6 ---- 6 files changed, 33 insertions(+), 134 deletions(-) delete mode 100755 scripts/fortunes.pl delete mode 100755 scripts/kirch.sh diff --git a/.gitignore b/.gitignore index f20153b..36ec77b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ README.org +scripts/ diff --git a/README b/README index 6b8d4ca..24470d8 100644 --- a/README +++ b/README @@ -14,15 +14,3 @@ 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 -═══════ - - Get the fortunes by executing `fortunes.pl' & `kirch.sh' from scripts - directory. Read the source before running them, it's short. - - ┌──── - │ lyra mst # Print a random fortune from mst fortunes. - │ lyra ls # List all fortune files. - └──── diff --git a/cpanfile b/cpanfile index f850fca..e611047 100644 --- a/cpanfile +++ b/cpanfile @@ -1,3 +1 @@ requires 'Path::Tiny', '0.114'; -requires 'IPC::Run3', '0.048'; -requires 'HTTP::Simple', '0.004'; diff --git a/lyra.pl b/lyra.pl index e2b57c8..6ad63dd 100755 --- a/lyra.pl +++ b/lyra.pl @@ -2,29 +2,44 @@ use strict; use warnings; -use feature 'say'; use Path::Tiny; -use IPC::Run3; -my $fortune_dir = "$ENV{HOME}/fortunes"; +my @fortunes; +my @fortune_paths = ("$ENV{HOME}/fortunes/", "/usr/share/games/fortune/"); -if ( $ARGV[0] ) { - if (-e "$fortune_dir/$ARGV[0]") { - my $fortune_file = "$fortune_dir/$ARGV[0]"; - random($fortune_file); - } elsif ( $ARGV[0] eq "ls") { - run3[ "ls", $fortune_dir]; - } else { - say "lyra: no such fortune"; +if (scalar @ARGV) { + foreach my $arg (@ARGV) { + foreach (@fortune_paths) { + my $path = path($_); + if (-e "$path/$arg") { + my $fortune_file = "$path/$arg"; + read_fortunes("$path/$arg") + } + } } } else { - say "Usage: lyra "; + foreach (@fortune_paths) { + my $path = path($_); + if (-d $path) { + for ($path->children) { + read_fortunes($_) unless $_ =~ /\.dat$/i; + } + } else { + read_fortunes($path); + } + } +} + + +if (scalar @fortunes > 0) { + print $fortunes[ rand @fortunes ], "\n" ; +} else { + print "lyra: no such fortune.\n"; } -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 read_fortunes { + my $path = shift @_; + my $file = path($path)->absolute; + push @fortunes, split(/\n%\n/, $file->slurp); } diff --git a/scripts/fortunes.pl b/scripts/fortunes.pl deleted file mode 100755 index ec785e3..0000000 --- a/scripts/fortunes.pl +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use feature 'say'; - -use Path::Tiny; -use IPC::Run3; - -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", - - # levonk quotes (had starred kirch's fortunes repo, checked - # profile & found this). - "levon" - => "https://raw.githubusercontent.com/levonk/fortune/master/levonkquotes", -); - -if ( $ARGV[0] ) { - if ( $ARGV[0] eq "latest") { get_latest(); } - elsif ( $ARGV[0] eq "mirror") { get_mirror(); } - else { say "fortunes.pl: no such option"; } -} else { say "Usage: ./fortunes.pl latest or ./fortune.pl mirror"; } - -sub get_latest { - foreach my $fortune (sort keys %fortunes) { - ftp("$fortune_dir/$fortune", $fortunes{$fortune}); - $? # We assume non-zero is an error. - ? warn "[WARN] Failed to get $fortune :: $?\n" - : say "got $fortune"; - } -} - -sub get_mirror { - require HTTP::Simple; - - # Ignore a warning, next line would've printed a warning. - no warnings 'once'; - $HTTP::Simple::UA->verify_SSL(1); - - 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", @_]; } diff --git a/scripts/kirch.sh b/scripts/kirch.sh deleted file mode 100755 index cb79343..0000000 --- a/scripts/kirch.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -mkdir -p /tmp \ - && git -C /tmp clone https://github.com/JKirchartz/fortunes.git kirch \ - && cd /tmp/kirch && rm -r README.md LICENSE Makefile bin \ - && cp -r /tmp/kirch/* $HOME/fortunes/ -- cgit 1.4.1-2-gfad0