lib/journal-cli/checkin.rb in journal-cli-1.0.17 vs lib/journal-cli/checkin.rb in journal-cli-1.0.18

- old
+ new

@@ -61,20 +61,20 @@ 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` - puts "Entered into Day One" + Journal.notify('{bg}Entered one entry into Day One') end 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, 'entries') + File.join(File.expand_path(Journal.config['entries_folder']), @key) else - File.expand_path('~/.local/share/journal', @key, 'entries') + File.expand_path("~/.local/share/journal/#{@key}/entries") end FileUtils.mkdir_p(dir) unless File.directory?(dir) filename = "#{@key}.md" @date.localtime @@ -83,20 +83,20 @@ f.puts f.puts "## #{@title} #{@date.strftime('%x %X')}" f.puts f.puts to_markdown(yaml: false, title: false) end - puts "Saved #{target}" + Journal.notify "{bg}Added new entry to {bw}#{target}" end 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, 'entries') + File.join(File.expand_path(Journal.config['entries_folder']), @key) else - File.join(File.expand_path('~/.local/share/journal/entries'), @key, 'entries') + File.join(File.expand_path("~/.local/share/journal/#{@key}/entries")) end FileUtils.mkdir_p(dir) unless File.directory?(dir) @date.localtime filename = "#{@key}_#{@date.strftime('%Y-%m-%d')}.md" @@ -104,11 +104,11 @@ if File.exist? target File.open(target, 'a') { |f| f.puts to_markdown(yaml: false, title: true, date: false, time: true) } else File.open(target, 'w') { |f| f.puts to_markdown(yaml: true, title: true, date: false, time: true) } end - puts "Saved #{target}" + Journal.notify "{bg}Saved daily Markdown to {bw}#{target}" end def save_individual_markdown dir = if @journal.key?('entries_folder') File.join(File.expand_path(@journal['entries_folder']), 'entries') @@ -122,11 +122,11 @@ FileUtils.mkdir_p(dir) unless File.directory?(dir) @date.localtime filename = @date.strftime('%Y-%m-%d_%H:%M.md') target = File.join(dir, filename) File.open(target, 'w') { |f| f.puts to_markdown(yaml: true, title: true) } - puts "Saved #{target}" + puts "Saved new entry to #{target}" end def print_answer(prompt, type, key, data) case type when /^(weather|forecast)/ @@ -142,22 +142,37 @@ end hr end end + def weather_to_yaml(answers) + data = {} + answers.each do |k, v| + case v.class.to_s + when /Hash/ + data[k] = weather_to_yaml(v) + when /Weather/ + data[k] = v.to_s + else + data[k] = v + end + end + data + end + def to_markdown(yaml: false, title: false, date: false, time: false) @output = [] if yaml @date.localtime - @output << <<~EOYAML - --- - title: #{@title} - date: #{@date.strftime('%x %X')} - --- + yaml_data = { 'title' => @title, 'date' => @date.strftime('%x %X')} + @data.each do |key, data| + yaml_data = yaml_data.merge(weather_to_yaml(data.answers)) + end - EOYAML + @output << YAML.dump(yaml_data).strip + @output << '---' end if title if date || time fmt = '' @@ -197,11 +212,11 @@ dir = if @journal.key?('entries_folder') File.expand_path(@journal['entries_folder']) elsif Journal.config.key?('entries_folder') File.expand_path(Journal.config['entries_folder']) else - File.expand_path("~/.local/share/journal") + File.expand_path('~/.local/share/journal') end FileUtils.mkdir_p(dir) unless File.directory?(dir) db = File.join(dir, "#{@key}.json") data = if File.exist?(db) JSON.parse(IO.read(db)) @@ -216,17 +231,22 @@ journal.answers.each do |k, v| if v.is_a? Hash v.each do |key, value| result = case value.class.to_s when /Weather/ - { 'high' => value.data[:high], 'low' => value.data[:low], 'condition' => value.data[:condition] } + { + 'high' => value.data[:high], + 'low' => value.data[:low], + 'condition' => value.data[:condition] + } else value end if jk == k output[jk][key] = result else + output[jk][k] ||= {} output[jk][k][key] = result end end elsif jk == k output[jk] = v @@ -244,9 +264,9 @@ end data.sort_by! { |e| e['date'] } File.open(db, 'w') { |f| f.puts JSON.pretty_generate(data) } - puts "Saved #{db}" + Journal.notify "{bg}Saved {bw}#{db}" end end end