diff options
author | Andinus <andinus@nand.sh> | 2020-11-18 11:25:47 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-11-18 11:25:47 +0530 |
commit | be00783294a5ff8f6601b1109f55bb83282b27d6 (patch) | |
tree | 3cc305a604ef1991b4e047a9217f8290d68746fb /lyra.pl | |
parent | a6f670e6756a7a1f5e73f843ab7c914640b74e4c (diff) | |
download | lyra-master.tar.gz |
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.
Diffstat (limited to 'lyra.pl')
-rwxr-xr-x | lyra.pl | 49 |
1 files changed, 32 insertions, 17 deletions
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 <fortune>"; + 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); } |