Created
June 21, 2012 15:09
-
-
Save gtrak/2966298 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 pomoclojo.core | |
(:import [org.apache.commons.exec | |
PumpStreamHandler | |
DefaultExecutor | |
ExecuteWatchdog | |
CommandLine] | |
[java.io ByteArrayOutputStream]) | |
(:use [clj-time.core :only [in-secs in-minutes now interval plus minus within? minutes secs]])) | |
(defn execute | |
"Executes a command-line program, returning stdout if a zero return code, else the | |
error out. Takes a list of strings which represent the command & arguments" | |
[cmd & args] | |
(let [output-stream (new ByteArrayOutputStream) | |
error-stream (new ByteArrayOutputStream)] | |
(try | |
(let [stream-handler (new PumpStreamHandler output-stream error-stream) | |
executor (doto | |
(new DefaultExecutor) | |
(.setExitValue 0) | |
(.setStreamHandler stream-handler) | |
(.setWatchdog (new ExecuteWatchdog 20000))) | |
cmdline (CommandLine. cmd)] | |
(dorun (map #(.addArgument cmdline %) args)) | |
(if (= 0 (.execute executor cmdline)) | |
(.toString output-stream) | |
(.toString error-stream))) | |
(catch Exception e | |
{:exception e | |
:os (str output-stream) | |
:es (str error-stream)})))) | |
(defn notify [message] | |
(execute "kdialog" "--title Pomoclojo" "--passivepopup" message)) | |
(defn pom [task mins] | |
(let [start (now) | |
end (plus start (minutes mins)) | |
pom-interval (interval start end)] | |
(loop [cur (plus start (secs 1))] | |
(when (within? pom-interval cur) | |
(let [left (interval cur end)] | |
(Thread/sleep 1000) | |
(println (str (in-minutes left) " minutes, " (mod (in-secs left) 60) " seconds left"))) | |
(recur (now))))) | |
(notify (str "Task Complete: " task)) | |
nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment