#!/usr/bin/env ruby require 'gli' require 'doing' include GLI::App wwid = WWID.new program_desc 'A CLI for a What Was I Doing system' version Doing::VERSION default_command :recent sort_help :manually desc 'Output notes if included in the template' default_value true switch [:notes], :default_value => true, :negatable => true # desc 'Wrap notes at X chars (0 for no wrap)' # flag [:w,:wrapwidth], :must_match => /^\d+$/, :type => Integer desc 'Add an entry' arg_name 'entry' command :now do |c| c.desc 'Section' c.default_value wwid.current_section c.flag [:s,:section] c.action do |global_options,options,args| if args.length > 0 wwid.add_item(args.join(" ").cap_first, options[:s].cap_first) wwid.write(wwid.doing_file) else raise "You must provide content when creating a new entry" end end end desc 'Add an item to the Later section' arg_name 'entry' command :later do |c| c.action do |global_options,options,args| if args.length > 0 wwid.add_item(args.join(" ").cap_first, "Later") wwid.write(wwid.doing_file) else raise "You must provide content when creating a new entry" end end end desc 'Add a completed item with @done(date)' arg_name 'entry' command :done do |c| c.desc 'Immediately archive the entry' c.switch [:a,:archive], :negatable => true c.desc 'Section' c.default_value wwid.current_section c.flag [:s,:section], :default_value => wwid.current_section c.action do |global_options,options,args| if args.length > 0 task = args.join(" ").cap_first task += " @done(#{Time.now.strftime('%F %R')})" section = options[:a] ? "Archive" : options[:s] wwid.add_item(task, section) wwid.write(wwid.doing_file) else raise "You must provide content when creating a new entry" end end end desc 'List all entries' arg_name 'section' command :show do |c| c.action do |global_options,options,args| if args.length > 0 if wwid.sections.include? args.join(" ").cap_first section = args.join(" ").cap_first else raise "No such section: #{args.join(" ")}" end else section = wwid.current_section end puts wwid.list_section({:section => section, :count => 0}) end end desc 'List recent entries' default_value 10 arg_name 'count' command :recent do |c| c.desc 'Section' c.default_value wwid.current_section c.flag [:s,:section] c.action do |global_options,options,args| if args.length > 0 count = args[0].to_i else count = 10 end puts wwid.recent(count,options[:s].cap_first) end end desc 'List entries from today' command :today do |c| c.action do |global_options,options,args| puts wwid.today.strip end end desc 'Show the last entry' command :last do |c| c.action do |global_options,options,args| puts wwid.last.strip end end desc 'List sections' command :sections do |c| c.action do |global_options,options,args| puts wwid.sections.join(", ") end end desc 'Select a section to display from a menu' command :choose do |c| c.action do |global_options,options,args| section = wwid.choose_section puts wwid.list_section({:section => section.cap_first, :count => 0}) end end desc 'Add a new section to the "doing" file' arg_name 'section_name' command :add_section do |c| c.action do |global_options,options,args| unless wwid.sections.include?(args[0]) wwid.add_section(args[0].cap_first) wwid.write(wwid.doing_file) else raise "Section #{args[0]} already exists" end end end desc 'Display a user-created view' arg_name 'view_name' command :view do |c| c.desc 'Section' c.flag [:s,:section] c.desc 'Count to display' c.flag [:c,:count], :must_match => /^\d+$/, :type => Integer c.action do |global_options,options,args| view = wwid.get_view(args[0]) if view template = view.has_key?('template') ? view['template'] : nil format = view.has_key?('date_format') ? view['date_format'] : nil count = options[:c] ? options[:c] : view.has_key?('count') ? view['count'] : 10 section = options[:s] ? options[:s] : view.has_key?('section') ? view['section'] : wwid.current_section puts wwid.list_section({:section => section, :count => count, :template => template, :format => format}) else raise "View #{args[0]} not found in config" end end end desc 'Move all but the most recent 5 entries in a section to Archive' arg_name 'section' default_value wwid.current_section command :archive do |c| c.desc 'Count to keep' c.default_value 5 c.flag [:k,:keep], :default_value => 5, :must_match => /^\d+$/, :type => Integer c.action do |global_options,options,args| if args.length > 0 section = args.join(" ").capitalize else section = wwid.current_section end wwid.archive(section,options[:k]) end end desc 'Edit the configuration file' command :config do |c| c.desc 'Editor to use' c.default_value ENV['EDITOR'] c.flag [:e,:editor] c.action do |global_options,options,args| system %Q{"#{options[:e]}" "#{File.expand_path(DOING_CONFIG)}"} end end pre do |global,command,options,args| wwid.config[:include_notes] = false unless global[:notes] # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only true end post do |global,command,options,args| # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling true end exit run(ARGV)