Skip to content

Instantly share code, notes, and snippets.

@bas080
Created December 21, 2015 17:09
Show Gist options
  • Save bas080/535c8b1325d209b9e7ee to your computer and use it in GitHub Desktop.
Save bas080/535c8b1325d209b9e7ee to your computer and use it in GitHub Desktop.
(ns lor.core
(:gen-class))
(require '[clojure.java.io :as io]
'[clojure.string :as string])
(defn user-home
"get the user home directory"
[]
(System/getProperty (str "user.home")))
(def log-interval (* 1000 60 10))
(def log-path (str (user-home) "/.lor"))
; pure
(defn delta
"Takes two numbers and returns a number that resembles the difference in value
between them"
[a b] (apply - (reverse (sort [a b]))))
; impure
(defn timestamp
"returns a timestamp in milliseconds"
[] (System/currentTimeMillis))
(defn file-last-modified
"takes a file object and returns an timestamp in milliseconds"
[file]
(.lastModified (io/file file )))
(defn file-recently-modified?
"takes a file and checks if has been modified within log-interval"
[file]
(> log-interval (delta (file-last-modified file) (timestamp))))
(defn file-name
"takes a file object and returns the filename as str"
[file]
(.getPath (io/file file)))
(defn sleep [milliseconds]
(Thread/sleep milliseconds))
(defn path-files [path]
(vec (file-seq (io/file path))))
(defn log-file-path []
(str log-path (/ (- (timestamp) (mod (timestamp) log-interval)) 1000)))
(defn log-files [files]
(spit (log-file-path) (string/join "\n" files)))
(defn time-till-next-update
"calculates how many milliseconds are left till next update in order to split
logs in nicely rounded timeslots"
[]
(- log-interval (mod (timestamp) log-interval)))
; code
; creates the lor directory if it doesn't exist and otherwise does nothing
(.mkdir (io/file log-path))
; write files changed within log-interval to a file in log-path with the
; timestamp as name
(defn -main
[& args]
((fn [wait]
(println (str 'log' wait))
(sleep wait)
(log-files (map file-name (filter file-recently-modified? (path-files (user-home)))))
(recur (time-till-next-update))) (time-till-next-update)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment