Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Forked from tormaroe/speednotes.clj
Created August 18, 2010 08:48
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 juergenhoetzel/534064 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/534064 to your computer and use it in GitHub Desktop.
(comment "
SpeedNotes Clojure script, by Torbjørn Marø.
Version 1.0 - 2010.08.17
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.
What about notes from yesterday?
--------------------------------
Notes are saved to plain text files in the current directory, so you
can easily access and read them using any text viewer. They can not
be accessed however by the current version of SpeedNotes, which is
only concerned with todays notes.
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 [[key rst] (.split line " " 2)]
[(.toUpperCase key) rst]))
(defn get-date-string []
(.format (SimpleDateFormat. "yyyy.MM.dd")
(.getTime (Calendar/getInstance))))
(defn get-time-string []
(.format (SimpleDateFormat. "HH:mm")
(.getTime (Calendar/getInstance))))
(defn get-file-name [keyword]
"Gets todays filename for a given keyword"
(str keyword "-" (get-date-string) ".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
.toUpperCase
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 []
"Prints the content of all files from today"
(doseq [log-name (->> (File. ".")
.listFiles
(filter-files (get-date-string))
(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)
(-> keyword
get-file-name
(write-to-file text))))
(recur))
(main-loop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment