about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-20 22:29:54 +0530
committerAndinus <andinus@nand.sh>2020-08-20 22:29:54 +0530
commitbc2163c379f0fa2b4b35e0bf6c6e54d243aec53c (patch)
treee2045974acda55a91dbad66a085fe9e0fa13633d
downloadlyra-bc2163c379f0fa2b4b35e0bf6c6e54d243aec53c.tar.gz
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--LICENSE13
-rw-r--r--README10
-rwxr-xr-xlyra.pl58
4 files changed, 82 insertions, 0 deletions
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 <andinus@nand.sh>
+
+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";
+}