Class: StandupMD::Cli
- Inherits:
-
Object
- Object
- StandupMD::Cli
- Includes:
- Helpers
- Defined in:
- lib/standup_md/cli.rb,
lib/standup_md/cli/helpers.rb
Overview
Class for handling the command-line interface.
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- ZSH_COMPLETION_FILE =
Path to the bundled zsh completion script.
::File.( ::File.join(__dir__, "..", "..", "completion", "zsh", "_standup") ).freeze
Instance Attribute Summary collapse
-
#config ⇒ StandupMD::Config
readonly
Runtime configuration snapshot for this CLI invocation.
-
#entry ⇒ StandupMD::Entry
readonly
The entry searched for, usually today.
-
#file ⇒ StandupMD::File
readonly
The file loaded.
-
#options ⇒ Array
readonly
Arguments passed at runtime.
Class Method Summary collapse
-
.config ⇒ StandupMD::Config::Cli
Access to the class’s configuration.
-
.echo(msg) ⇒ nil
Prints output if
verboseis true. -
.execute(options = []) ⇒ Object
Creates an instance of
StandupMDand runs what the user requested. -
.zsh_completion_instructions ⇒ String
Prints zsh completion setup instructions.
Instance Method Summary collapse
-
#echo(msg) ⇒ nil
Quick access to
Cli.echo. -
#edit ⇒ nil
Opens the file in an editor.
-
#file_date_argument? ⇒ Boolean
Was a file date argument passed?.
-
#initialize(options = [], load_config: true) ⇒ Cli
constructor
Constructor.
-
#load_preferences ⇒ nil
Load the preference file.
-
#post? ⇒ Boolean
Should the CLI post the entry to a chat adapter?.
-
#preference_file_loaded? ⇒ boolean
Has the preference file been loaded?.
-
#write? ⇒ Boolean
Should the file be written?.
-
#write_file ⇒ Boolean
Writes entries to the file.
-
#zsh_completion_requested? ⇒ Boolean
Was zsh completion output requested?.
Methods included from Helpers
Constructor Details
#initialize(options = [], load_config: true) ⇒ Cli
Constructor. Sets defaults.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/standup_md/cli.rb', line 130 def initialize( = [], load_config: true) @config = nil @preference_file_loaded = false @file_date_argument = false @zsh_completion_requested = false @options = return if load_zsh_completion_request() load_preferences if load_config @config = StandupMD.config.copy load_runtime_preferences() return if zsh_completion_requested? @file = find_file @file&.load @entry = @file.nil? ? nil : new_entry(@file) end |
Instance Attribute Details
#config ⇒ StandupMD::Config (readonly)
Runtime configuration snapshot for this CLI invocation.
90 91 92 |
# File 'lib/standup_md/cli.rb', line 90 def config @config end |
#entry ⇒ StandupMD::Entry (readonly)
The entry searched for, usually today.
96 97 98 |
# File 'lib/standup_md/cli.rb', line 96 def entry @entry end |
#file ⇒ StandupMD::File (readonly)
The file loaded.
108 109 110 |
# File 'lib/standup_md/cli.rb', line 108 def file @file end |
#options ⇒ Array (readonly)
Arguments passed at runtime.
102 103 104 |
# File 'lib/standup_md/cli.rb', line 102 def @options end |
Class Method Details
.config ⇒ StandupMD::Config::Cli
Access to the class’s configuration.
24 25 26 |
# File 'lib/standup_md/cli.rb', line 24 def self.config StandupMD.config.cli end |
.echo(msg) ⇒ nil
Prints output if verbose is true.
32 33 34 |
# File 'lib/standup_md/cli.rb', line 32 def self.echo(msg) puts msg if config.verbose end |
.execute(options = []) ⇒ Object
Creates an instance of StandupMD and runs what the user requested.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/standup_md/cli.rb', line 68 def self.execute( = []) new().tap do |exe| if exe.zsh_completion_requested? puts zsh_completion_instructions next end exe.write_file if exe.write? if exe.config.cli.print exe.print(exe.entry) elsif exe.config.cli.post exe.post(exe.entry) elsif exe.config.cli.edit exe.edit end end end |
.zsh_completion_instructions ⇒ String
Prints zsh completion setup instructions.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/standup_md/cli.rb', line 40 def self.zsh_completion_instructions completion_dir = ::File.dirname(ZSH_COMPLETION_FILE) <<~INSTRUCTIONS Zsh completion file: #{ZSH_COMPLETION_FILE} To load it directly, add this before compinit runs: fpath=("#{completion_dir}" $fpath) autoload -Uz compinit compinit Or symlink it into your own completion directory: mkdir -p ~/.zsh/completions ln -sf "#{ZSH_COMPLETION_FILE}" ~/.zsh/completions/_standup Then make sure that directory is in fpath before compinit runs: fpath=(~/.zsh/completions $fpath) autoload -Uz compinit compinit INSTRUCTIONS end |
Instance Method Details
#echo(msg) ⇒ nil
Quick access to Cli.echo.
208 209 210 |
# File 'lib/standup_md/cli.rb', line 208 def echo(msg) puts msg if @config&.cli&.verbose end |
#edit ⇒ nil
Opens the file in an editor. Abandons the script.
174 175 176 177 |
# File 'lib/standup_md/cli.rb', line 174 def edit echo "Opening file in #{@config.cli.editor}" exec("#{@config.cli.editor} #{file.name}") end |
#file_date_argument? ⇒ Boolean
Was a file date argument passed?
114 115 116 |
# File 'lib/standup_md/cli.rb', line 114 def file_date_argument? @file_date_argument end |
#load_preferences ⇒ nil
Load the preference file.
152 153 154 155 156 157 158 159 160 |
# File 'lib/standup_md/cli.rb', line 152 def load_preferences preference_file = StandupMD.config.cli.preference_file if ::File.exist?(preference_file) ::StandupMD.load_config_file(preference_file) @preference_file_loaded = true else self.class.echo "Preference file #{preference_file} does not exist." end end |
#post? ⇒ Boolean
Should the CLI post the entry to a chat adapter?
200 201 202 |
# File 'lib/standup_md/cli.rb', line 200 def post? @config.cli.post end |
#preference_file_loaded? ⇒ boolean
Has the preference file been loaded?
166 167 168 |
# File 'lib/standup_md/cli.rb', line 166 def preference_file_loaded? @preference_file_loaded end |
#write? ⇒ Boolean
Should the file be written?
192 193 194 |
# File 'lib/standup_md/cli.rb', line 192 def write? !!(@config.cli.write && !read_only? && entry) end |
#write_file ⇒ Boolean
Writes entries to the file.
183 184 185 186 |
# File 'lib/standup_md/cli.rb', line 183 def write_file echo "Writing file #{file.name}" file.write end |
#zsh_completion_requested? ⇒ Boolean
Was zsh completion output requested?
122 123 124 |
# File 'lib/standup_md/cli.rb', line 122 def zsh_completion_requested? @zsh_completion_requested end |