Skip to content

Instantly share code, notes, and snippets.

@Leonidas-from-XIV
Created February 1, 2011 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leonidas-from-XIV/806871 to your computer and use it in GitHub Desktop.
Save Leonidas-from-XIV/806871 to your computer and use it in GitHub Desktop.
Shortened example
(ns karmawhore.color
(:use [clojure.contrib.str-utils :only (str-join)]))
(def force-color false)
(defn- fill-color [color]
(if force-color ""
(format sgr-template (sgr-colors color))))
(defn colorize [color text]
(let [color (fill-color color)
rst (fill-color :reset)]
(str-join "" [color text rst])))
(def red (partial colorize :red))
(ns karmawhore.parser
(:gen-class)
(:use [clojure.contrib.duck-streams :only (read-lines reader)])
(:use [clojure.contrib.seq :only (separate)])
(:use [clojure.contrib.str-utils :only (re-sub)])
(:use [clojure.contrib.generic.functor :only (fmap)])
(:use [clojure.contrib.json :only (read-json)])
(:use [clojure.contrib.command-line :only (with-command-line)])
(:use [clojure.contrib.with-ns :only (with-ns)])
(:use [karmawhore.color :only (bold red white green force-color)])
(:use [karmawhore.html :only (records->html)]))
(defn- text-output [records]
(prn (resolve 'force-color))
(binding [force-color true]
(prn (resolve 'force-color))
(prn force-color)
(doseq [[nick {u :upvotes d :downvotes s :sum}] records]
(printf "%s: Karma %s (Upvotes %s, Downvotes %s)\n"
(white nick) (bold (white s)) (green u) (red d)))))
(defn- html-output [records]
(println (records->html records)))
(defn -main [& args]
(with-command-line
args "Usage: karmawhore [-c|-h] logfile"
[[color? c? "Force colored output" false]
[html? "Output HTML file" false]
rest-args]
;; make the config locally known using dynamic binding
(binding [config (load-config)]
(let [file-name (first rest-args)
line-votes (map get-votes (read-lines file-name))
votes (reduce (fn [a b] (merge-with (partial merge-with +) a b)) line-votes)
summed-karma (for [[k {u :upvotes d :downvotes}] votes]
[k {:upvotes u :downvotes d :sum (- u d)}])
sorted-by-karma (sort-by (comp - :sum second) summed-karma)]
((if html? html-output text-output) sorted-by-karma)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment