lib/journal-cli/checkin.rb in journal-cli-1.0.34 vs lib/journal-cli/checkin.rb in journal-cli-1.0.35

- old
+ new

@@ -1,10 +1,15 @@ module Journal # Main class class Checkin attr_reader :key, :date, :data, :config, :journal, :sections, :title, :output + ## + ## Initialize a new checkin using a configured journal + ## + ## @param journal [Journal] The journal + ## def initialize(journal) @key = journal @output = [] @date = Journal.date @date.localtime @@ -17,30 +22,55 @@ @data = {} meridian = (@date.hour < 13) ? "AM" : "PM" @title = @journal["title"].sub(/%M/, meridian) end + ## + ## Add a title (Markdown) to the output + ## + ## @param string [String] The string + ## def add_title(string) @output << "\n## #{string}\n" unless string.nil? end + ## + ## Add a question header (Markdown) to the output + ## + ## @param string [String] The string + ## def header(string) @output << "\n##### #{string}\n" unless string.nil? end + ## + ## Add a section header (Markdown) to the output + ## + ## @param string [String] The string + ## def section(string) @output << "\n###### #{string}\n" unless string.nil? end + ## + ## Add a newline to the output + ## def newline @output << "\n" end + ## + ## Add a horizontal rule (Markdown) to the output + ## def hr @output << "\n* * * * * *\n" end + ## + ## Finalize the checkin, saving data to JSON, Day One, + ## and Markdown as configured + ## def go @sections.each { |key, section| @data[key] = section } save_data save_day_one_entry if @journal["dayone"] @@ -55,24 +85,47 @@ else save_single_markdown end end + ## + ## Launch Day One and quit if it wasn't running + ## + def launch_day_one + # Launch Day One to ensure database is up-to-date + # test if Day One is open + running = !`ps ax | grep "/MacOS/Day One" | grep -v grep`.strip.empty? + # -g do not bring app to foreground + # -j launch hidden + `/usr/bin/open -gj -a "Day One"` + # quit if it wasn't running + `osascript -e 'tell app "Day One" to quit'` if !running + end + + ## + ## Save journal entry to Day One using the command line tool + ## def save_day_one_entry unless TTY::Which.exist?("dayone2") Journal.notify("{br}Day One CLI not installed, no Day One entry created") return end + + launch_day_one + @date.localtime cmd = ["dayone2"] cmd << %(-j "#{@journal["journal"]}") if @journal.key?("journal") cmd << %(-t #{@journal["tags"].join(" ")}) if @journal.key?("tags") cmd << %(-date "#{@date.strftime("%Y-%m-%d %I:%M %p")}") `echo #{Shellwords.escape(to_markdown(yaml: false, title: true))} | #{cmd.join(" ")} -- new` Journal.notify("{bg}Entered one entry into Day One") end + ## + ## Save entry to an existing Markdown file + ## def save_single_markdown dir = if @journal.key?("entries_folder") File.join(File.expand_path(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.expand_path(Journal.config["entries_folder"]), @key) @@ -91,10 +144,13 @@ f.puts to_markdown(yaml: false, title: false) end Journal.notify "{bg}Added new entry to {bw}#{target}" end + ## + ## Save journal entry to daily Markdown file + ## def save_daily_markdown dir = if @journal.key?("entries_folder") File.join(File.expand_path(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.expand_path(Journal.config["entries_folder"]), @key) @@ -112,9 +168,12 @@ File.open(target, "w") { |f| f.puts to_markdown(yaml: true, title: true, date: false, time: true) } end Journal.notify "{bg}Saved daily Markdown to {bw}#{target}" end + ## + ## Save journal entry to an new individual Markdown file + ## def save_individual_markdown dir = if @journal.key?("entries_folder") File.join(File.expand_path(@journal["entries_folder"]), "entries") elsif Journal.config.key?("entries_folder") File.join(File.expand_path(Journal.config["entries_folder"]), @key,