class StandupMD::CLI
Class for handing the command-line interface.
Constants
- PREFERENCE_FILE
The user's preference file.
Attributes
Arguments passed at runtime.
@return [Array] ARGV
Preferences after reading config file and parsing ARGV.
@return [Array] ARGV
Public Class Methods
Creates an instance of StandupMD
and runs what the user requested.
# File lib/standup_md/cli.rb, line 18 def self.execute(options = []) exe = new(options) exe.append_to_previous_entry_tasks if exe.should_append? exe.print_current_entry if exe.print_current_entry? exe.print_all_entries if exe.print_all_entries? exe.write_file if exe.write? exe.edit if exe.edit? end
Constructor. Sets defaults.
@param [Array] options
# File lib/standup_md/cli.rb, line 44 def initialize(options) @edit = true @write = true @append_previous = true @print_current_entry = false @json = false @verbose = false @print_all_entries = false @options = options @preferences = get_preferences end
Public Instance Methods
Should `previous_entry_tasks` be appended? If false, previous_entry_tasks
will be overwritten.
@return [Boolean] Default is true
# File lib/standup_md/cli.rb, line 203 def append_previous? @append_previous end
Appends entries passed at runtime to existing previous entries.
@return [Hash]
# File lib/standup_md/cli.rb, line 128 def append_to_previous_entry_tasks echo 'Appending previous entry tasks' additions = preferences.delete('previous_entry_tasks') standup.previous_entry_tasks.concat(additions) end
Prints output if verbose
is true.
@return [nil]
# File lib/standup_md/cli.rb, line 219 def echo(msg) puts msg if verbose? end
Opens the file in an editor. Abandons the script.
# File lib/standup_md/cli.rb, line 136 def edit echo " Opening file in #{editor}" exec("#{editor} #{standup.file}") end
Should the standup file be opened in the editor?
@return [Boolean] Default is true
# File lib/standup_md/cli.rb, line 194 def edit? @edit end
Tries to determine the editor, first by checking if the user has one set in their preferences. If not, the VISUAL
and EDITOR
environmental variables are checked. If none of the above are set, defaults to vim
.
@return [String] The editor
# File lib/standup_md/cli.rb, line 76 def editor @editor ||= if preferences.key?('editor') echo "Editor set to [#{preferences.key('editor')}] via preferences" preferences.delete('editor') elsif ENV['VISUAL'] echo "Editor set to [#{ENV['VISUAL']}] (ENV['VISUAL'])" ENV['VISUAL'] elsif ENV['EDITOR'] echo "Editor set to [#{ENV['EDITOR']}] (ENV['EDITOR'])" ENV['EDITOR'] else echo "Editor set to [vim] (default)" 'vim' end end
If printing an entry, should it be printed as json?
@return [Boolean] Default is false
# File lib/standup_md/cli.rb, line 162 def json? @json end
Prints all entries to the command line.
@return [nil]
# File lib/standup_md/cli.rb, line 97 def print_all_entries echo 'Display all entries' unless json? standup.all_entries.keys.reverse.each do |head| print_entry(head, standup.all_entries[head]) end return end echo ' ...as json' puts standup.all_entries.to_json end
Should all entries be printed? If true, disables editing.
@return [Boolean] Default is false
# File lib/standup_md/cli.rb, line 170 def print_all_entries? @print_all_entries end
Prints the current entry to the command line.
@return [nil]
# File lib/standup_md/cli.rb, line 113 def print_current_entry echo 'Print current entry' unless json? print_entry(standup.header, standup.current_entry) return end echo ' ...as json' entry = {standup.header => standup.current_entry}.to_json puts entry end
Should current entry be printed? If true, disables editing.
@return [Boolean] Default is false
# File lib/standup_md/cli.rb, line 154 def print_current_entry? @print_current_entry end
Did the user pass previous_entry_tasks
, and should we append?
@return [Boolean]
# File lib/standup_md/cli.rb, line 211 def should_append? preferences.key?('previous_entry_tasks') && append_previous? end
Sets up an instance of StandupMD
and passes all user preferences.
@return [StandupMD]
# File lib/standup_md/cli.rb, line 60 def standup @standup ||= ::StandupMD.new do |s| echo 'Runtime options:' preferences.each do |k, v| echo " #{k} = #{v}" s.send("#{k}=", v) end end.load end
Should debug info be printed?
@return [Boolean] Default is false
# File lib/standup_md/cli.rb, line 178 def verbose? @verbose end
Should the file be written?
@return [Boolean] Default is true
# File lib/standup_md/cli.rb, line 186 def write? @write end
Writes entries to the file.
@return [Boolean] true if file was written
# File lib/standup_md/cli.rb, line 145 def write_file echo ' Writing file' standup.write end