From bc2163c379f0fa2b4b35e0bf6c6e54d243aec53c Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 20 Aug 2020 22:29:54 +0530 Subject: Initial commit --- .gitignore | 1 + LICENSE | 13 +++++++++++++ README | 10 ++++++++++ lyra.pl | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README create mode 100755 lyra.pl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f20153b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +README.org diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8cb5164 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2020, Andinus + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..2a3ecfe --- /dev/null +++ b/README @@ -0,0 +1,10 @@ + ━━━━━━━━━ + LYRA + + Andinus + ━━━━━━━━━ + + +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. diff --git a/lyra.pl b/lyra.pl new file mode 100755 index 0000000..3aad720 --- /dev/null +++ b/lyra.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +use Path::Tiny; + +use constant is_OpenBSD => $^O eq "openbsd"; +require OpenBSD::Unveil + if is_OpenBSD; +sub unveil { + if (is_OpenBSD) { + return OpenBSD::Unveil::unveil(@_); + } else { + return 1; + } +} + +# Unveil @INC. +foreach my $path (@INC) { + unveil( $path, 'rx' ) + or die "Unable to unveil: $!\n"; +} + +my %dispatch = ( + "random" => \&random, + "help" => \&HelpMessage, +); + +sub HelpMessage { + say qq{Usage: + random + Print a random fortune. + help + Show this text.} +} + +# Print a random fortune from $path. +sub random { + my $path = "$ENV{HOME}/quotes.txt"; + $path = $ARGV[1] if $ARGV[1]; # Use $ARGV[1] as path if it exists. + unveil( $path, "r" ) + or die "Unable to unveil: $!\n"; # Unveil $path as read-only. + + my $file = path($path)->absolute; + my @fortunes = split/\n%\n/, $file->slurp; + + say $fortunes[ rand @fortunes ]; # Print random fortune. +} + +if ( $dispatch{ $ARGV[0] } ) { + $dispatch{ $ARGV[0] }->(); +} elsif ( scalar @ARGV == 0 ) { + HelpMessage(); +} else { + say "lyra: no such option"; +} -- cgit 1.4.1-2-gfad0