Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Created August 17, 2010 11:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tormaroe/529391 to your computer and use it in GitHub Desktop.
Save tormaroe/529391 to your computer and use it in GitHub Desktop.
(comment "
SpeedNotes Clojure script, by Torbjørn Marø.
Version 1.1 - 2010.08.18
Clojure version 1.1 (w/Contrib)
======================================================================
Always have it running in a console window to quickly note down stuff
you need to remember - thoughts and ideas you don't want to loose,
but don't want to steal focus away from what you are currently doing
either.
Usage
----------------------------------------------------------------------
Save notes:
-----------
At the prompt type a keyword + the text you want to save.
Example:
>todo Buy milk
This will append 'Buy milk' to todays TODO file, prefixed with the
time you entered it. ANy word can be used as a keyword, and each
keyword will get it's own file (one for each day).
List saved items for a keyword:
-------------------------------
At the prompt enter 'list' + a keyword
Example:
>list todo
This will list everything you have saved in your todo-list today.
List all saved items:
---------------------
Type 'summary' at the prompt. This will list the content of all your
keyword files for today. You can also list older summaries by
subtracting days, like this:
>summary -1
This would show the summary for yesterday.
How are notes saved?
--------------------
Notes are saved to plain text files in the current directory, so you
can easily access and read them using any text viewer.
Exit the script:
----------------
If you really want to close down SpeedNotes you enter 'quit' at the
prompt.
")
(ns marosoft.speednotes
(:use clojure.contrib.duck-streams)
(:import java.util.Calendar)
(:import java.text.SimpleDateFormat)
(:import java.io.File))
(defn split-keyword-text [line]
"Splits a line on keyword and the rest.
Returns it as a vector of the two.
The keyword is made upper case."
(let [space-index (.indexOf line " ")]
(if (neg? space-index)
[(.toUpperCase line) nil]
[(.toUpperCase (.substring line 0 space-index))
(.substring line (inc space-index))])))
(defn now [] (Calendar/getInstance))
(defn get-date-string [date]
(.format (SimpleDateFormat. "yyyy.MM.dd")
(.getTime date)))
(defn get-time-string []
(.format (SimpleDateFormat. "HH:mm")
(.getTime (now))))
(defn get-date-from-param-string [param-string]
(let [date (now)]
(when (not (nil? param-string))
(try
(.add date Calendar/DATE (Integer/parseInt param-string))
(catch java.lang.NumberFormatException e
(println "*** Not a valid argument"))))
date))
(defn get-file-name [keyword]
"Gets todays filename for a given keyword"
(str keyword "-" (get-date-string (now)) ".log"))
(defn write-to-file [file text]
"Write a log line to a given file"
(append-spit file
(str (get-time-string)
\tab
text
\newline)))
(defn print-keyword-file [keyword]
"Prints the content of todays file for a given keyword"
(println "--------------------------------------")
(try (-> keyword
get-file-name
slurp
println)
(catch java.io.FileNotFoundException e
(println "*** No" keyword "file today!"))))
(defn filter-files [pattern files]
"Filters a collection of files on a pattern"
(filter #(-> (.getName %)
(.indexOf pattern)
pos?)
files))
(defn print-summary [params-string]
"Prints the content of all files from today (or earlier days)"
(let [date (get-date-from-param-string params-string)]
(doseq [log-name (->> (File. ".")
.listFiles
(filter-files (get-date-string date))
(map #(.getName %)))]
(do
(println "----------------------------------")
(println \tab log-name)
(println "----------------------------------")
(println (slurp log-name))))))
(defn main-loop []
"Takes action on input and continues"
(println) (print ">") (flush)
(let [[keyword text] (split-keyword-text (read-line))]
(condp = keyword
"QUIT" (System/exit 0)
"LIST" (print-keyword-file text)
"SUMMARY" (print-summary text)
(when (not (nil? text))
(-> keyword
get-file-name
(write-to-file text)))))
(recur))
(main-loop)
@manutter51
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment