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. --- lyra.pl | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'lyra.pl') 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); } -- cgit 1.4.1-2-gfad0