From d0cc9ca7adc2a357160dc9187b19e6d3dc7e37ab Mon Sep 17 00:00:00 2001
From: tec <tec@ucc.gu.uwa.edu.au>
Date: Tue, 4 Feb 2020 18:17:06 +0800
Subject: [PATCH] Cowsay without args uses fortune and random cow

---
 src/voting.rs | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/voting.rs b/src/voting.rs
index cc26869..b5ea473 100644
--- a/src/voting.rs
+++ b/src/voting.rs
@@ -56,16 +56,25 @@ impl Commands {
         );
     }
     pub fn cowsay(ctx: Context, msg: Message, content: &str) {
-        let mut text = content.to_owned();
-        text.escape_default();
-        // Guess what buddy! You definitely are passing a string to cowsay
-        text.insert(0, '\'');
-        text.insert(text.len(), '\'');
-        let output = std::process::Command::new("cowsay")
-            .arg(text)
-            .output()
-            // btw, if we can't execute cowsay we crash
-            .expect("failed to execute cowsay");
+        let output;
+        if !content.trim().is_empty() {
+            let mut text = content.to_owned();
+            text.escape_default();
+            // Guess what buddy! You definitely are passing a string to cowsay
+            text.insert(0, '\'');
+            text.insert(text.len(), '\'');
+            output = std::process::Command::new("cowsay")
+                .arg(text)
+                .output()
+                // btw, if we can't execute cowsay we crash
+                .expect("failed to execute cowsay");
+        } else {
+            output = std::process::Command::new("sh")
+                .arg("-c")
+                .arg("sh -c fortune | cowsay -f \"/usr/share/cowsay/cows/$(echo 'www\nhellokitty\nbud-frogs\nkoala\nsuse\nthree-eyes\npony-smaller\nsheep\nvader\ncower\nmoofasa\nelephant\nflaming-sheep\nskeleton\nsnowman\ntux\napt\nmoose' | shuf -n 1).cow\"")
+                .output()
+                .expect("failed to execute fortune/cowsay");
+        }
         let mut message = MessageBuilder::new();
         message.push_codeblock(
             String::from_utf8(output.stdout).expect("unable to parse stdout to String"),
-- 
GitLab