Created
June 4, 2014 01:44
-
-
Save olix0r/9ef9acb21c998b582271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns gob.core | |
[:use [clojure.core.reducers :only (cat)]] | |
[:use [clojure.string :only (join)]]) | |
(def penus "PENUS") | |
(defn env-columns [config] | |
(let [columns (System/getenv "COLUMNS")] | |
(if (nil? columns) | |
config | |
(assoc config "columns" (Integer/decode columns))))) | |
(defn env-config [config] | |
(env-columns config)) | |
(defn parse-config | |
"Parse command-line configuration" | |
[args config] | |
(loop [[opt & more] args | |
config config] | |
(cond | |
(nil? opt) config | |
(= opt "-y") (recur more (assoc config "ready" true)) | |
(= opt "-m") (recur more (assoc config "magic" true)) | |
(= opt "-i") (recur more (assoc config "inverted" true)) | |
(= opt "-l") (recur (rest more) (assoc config "limit" (Integer/decode (first more)))) | |
(= opt "-p") (recur (rest more) (assoc config "penus" (first more))) | |
:else (recur more config)))) | |
(defn- gob-prompt [] | |
(do | |
(println "Gob's Program: Y/N?") | |
(print "? ") | |
(flush) | |
(.toUpperCase (read-line)))) | |
(defn- ready-for-gob? [config] | |
(if (contains? config "ready") | |
(config "ready") | |
(loop [rsp (gob-prompt)] | |
(cond | |
(.startsWith rsp "Y") | |
true | |
(or (.startsWith rsp "N") (= rsp "EXIT")) | |
false | |
:else | |
(recur (gob-prompt)))))) | |
(defn- which-penus [config] | |
(let [columns (config "columns" 0) | |
penus (config "penus" "PENUS") | |
magic? (config "magic" false) | |
inverted? (config "inverted" false) | |
slot-size (.length penus) | |
slot-count (max 1 (int (/ (inc columns) (inc slot-size)))) | |
space (join "" (repeat slot-size " ")) | |
left-str (if inverted? space penus) | |
right-str (if inverted? penus space)] | |
(if (< columns 1) | |
(fn [_] | |
(str penus " ")) | |
(if magic? | |
(fn [n] | |
(let [forward? (even? (int (/ n slot-count))) | |
fulcrum (mod n slot-count) | |
left-count (if forward? fulcrum (- slot-count fulcrum)) | |
right-count (- slot-count left-count) | |
slots (cat (repeat left-count left-str) (repeat right-count right-str)) | |
text (join " " slots)] | |
(str text "\n"))) | |
(fn [_] | |
(str (join " " (repeat slot-count penus)) "\n")))))) | |
(defn -main | |
"Gob's program | |
The zazziest darned penus printer on the Internet. | |
Command-line arguments: | |
-y You hereby state that you are ready for Gob's program. | |
-m Formatting illusions | |
-i invert the illusion | |
-l <limit> Limit the peen | |
-p use another penus | |
" | |
[& args] | |
(let [config (parse-config args (env-config {}))] | |
(if (ready-for-gob? config) | |
(let [limit (config "limit") | |
penus (which-penus config) | |
indexes (if (nil? limit) (range) (range 0 limit))] | |
(doseq [n indexes] | |
(print (penus n))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://vine.co/v/bYZLItLJFr6