Last active
December 17, 2015 18:29
-
-
Save olix0r/5653802 to your computer and use it in GitHub Desktop.
my first clojure program
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.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 (inc (.length penus)) | |
slot-count (max 1 (int (/ columns slot-size))) | |
mk-penuses (fn [c] (join " " (repeat c penus))) | |
mk-spaces (fn [c] (join "" (repeat (* c slot-size) " ")))] | |
(if (< columns 1) | |
(fn [_] | |
(str penus " ")) | |
(if magic? | |
(fn [n] | |
(let [forward? (even? (int (/ n slot-count))) | |
offset (mod n slot-count) | |
left-count (if forward? offset (- slot-count offset)) | |
right-count (- slot-count left-count)] | |
(if inverted? | |
(str (mk-spaces left-count) (mk-penuses right-count) "\n") | |
(str (mk-penuses left-count) (mk-spaces right-count) "\n")))) | |
(fn [_] | |
(mk-penuses slot-count)))))) | |
(defn- penus-loop [get-penus limit] | |
(loop [i 0] | |
(if (or (nil? limit) (< i limit)) | |
(do | |
(let [p (get-penus i)] | |
(if p | |
(do (print p) (flush)))) | |
(recur (inc i)))))) | |
(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) | |
(penus-loop (which-penus config) (config "limit"))))) |
Author
olix0r
commented
May 28, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment