Class: StandupMD::Cli

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/standup_md/cli.rb,
lib/standup_md/cli/helpers.rb

Overview

Class for handing the command-line interface.

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

ZSH_COMPLETION_FILE =

Path to the bundled zsh completion script.

Returns:

  • (String)
::File.expand_path(
  ::File.join(__dir__, "..", "..", "completion", "zsh", "_standup")
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#print

Constructor Details

#initialize(options = [], load_config: true) ⇒ Cli

Constructor. Sets defaults.

Parameters:

  • options (Array) (defaults to: [])


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/standup_md/cli.rb', line 122

def initialize(options = [], load_config: true)
  @config = self.class.config
  @preference_file_loaded = false
  @file_date_argument = false
  @zsh_completion_requested = false
  @options = options
  return if load_zsh_completion_request(options)

  load_preferences if load_config
  load_runtime_preferences(options)
  return if zsh_completion_requested?

  @file = find_file
  @file&.load
  @entry = @file.nil? ? nil : new_entry(@file)
end

Instance Attribute Details

#entryStandupMD::Entry (readonly)

The entry searched for, usually today.

Returns:



88
89
90
# File 'lib/standup_md/cli.rb', line 88

def entry
  @entry
end

#fileStandupMD::File (readonly)

The file loaded.

Returns:



100
101
102
# File 'lib/standup_md/cli.rb', line 100

def file
  @file
end

#optionsArray (readonly)

Arguments passed at runtime.

Returns:

  • (Array)

    ARGV



94
95
96
# File 'lib/standup_md/cli.rb', line 94

def options
  @options
end

Class Method Details

.configStandupMD::Config::Cli

Access to the class’s configuration.



24
25
26
# File 'lib/standup_md/cli.rb', line 24

def self.config
  @config ||= StandupMD.config.cli
end

.echo(msg) ⇒ nil

Prints output if verbose is true.

Returns:

  • (nil)


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
# File 'lib/standup_md/cli.rb', line 68

def self.execute(options = [])
  new(options).tap do |exe|
    if exe.zsh_completion_requested?
      puts zsh_completion_instructions
      next
    end

    exe.write_file if exe.write?
    if config.print
      exe.print(exe.entry)
    elsif config.edit
      exe.edit
    end
  end
end

.zsh_completion_instructionsString

Prints zsh completion setup instructions.

Returns:

  • (String)


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.

Returns:

  • (nil)


190
191
192
# File 'lib/standup_md/cli.rb', line 190

def echo(msg)
  self.class.echo(msg)
end

#editnil

Opens the file in an editor. Abandons the script.

Returns:

  • (nil)


164
165
166
167
# File 'lib/standup_md/cli.rb', line 164

def edit
  echo "Opening file in #{@config.editor}"
  exec("#{@config.editor} #{file.name}")
end

#file_date_argument?Boolean

Was a file date argument passed?

Returns:

  • (Boolean)


106
107
108
# File 'lib/standup_md/cli.rb', line 106

def file_date_argument?
  @file_date_argument
end

#load_preferencesnil

Load the preference file.

Returns:

  • (nil)


143
144
145
146
147
148
149
150
# File 'lib/standup_md/cli.rb', line 143

def load_preferences
  if ::File.exist?(@config.preference_file)
    ::StandupMD.load_config_file(@config.preference_file)
    @preference_file_loaded = true
  else
    echo "Preference file #{@config.preference_file} does not exist."
  end
end

#preference_file_loaded?boolean

Has the preference file been loaded?

Returns:

  • (boolean)


156
157
158
# File 'lib/standup_md/cli.rb', line 156

def preference_file_loaded?
  @preference_file_loaded
end

#write?Boolean

Should the file be written?

Returns:

  • (Boolean)


182
183
184
# File 'lib/standup_md/cli.rb', line 182

def write?
  !!(@config.write && !read_only? && entry)
end

#write_fileBoolean

Writes entries to the file.

Returns:

  • (Boolean)

    true if file was written



173
174
175
176
# File 'lib/standup_md/cli.rb', line 173

def write_file
  echo "Writing file #{file.name}"
  file.write
end

#zsh_completion_requested?Boolean

Was zsh completion output requested?

Returns:

  • (Boolean)


114
115
116
# File 'lib/standup_md/cli.rb', line 114

def zsh_completion_requested?
  @zsh_completion_requested
end