Class: Pomodoro::Commands::Command

Inherits:
RR::Command
  • Object
show all
Includes:
EnvironmentCommunication, RR::ColouredTerminal
Defined in:
lib/pomodoro/commands.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, config = nil) ⇒ Command

Returns a new instance of Command.


67
68
69
# File 'lib/pomodoro/commands.rb', line 67

def initialize(args, config = nil)
  @args, @config = args, config || Pomodoro.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


66
67
68
# File 'lib/pomodoro/commands.rb', line 66

def config
  @config
end

Class Method Details

.command_nameObject


53
54
55
# File 'lib/pomodoro/commands.rb', line 53

def self.command_name
  self.to_s.split('::').last.downcase
end

.descriptionObject


57
58
59
60
# File 'lib/pomodoro/commands.rb', line 57

def self.description
  self.t(:description)
rescue I18n::MissingTranslationData
end

.t(key, **options) ⇒ Object


62
63
64
# File 'lib/pomodoro/commands.rb', line 62

def self.t(key, **options)
  I18n.t!("commands.#{self.command_name}.#{key}", **options)
end

Instance Method Details

#abort(*args) ⇒ Object Originally defined in module EnvironmentCommunication

#command(command) ⇒ Object


49
50
51
# File 'lib/pomodoro/commands.rb', line 49

def command(command)
  system(command)
end

#edit_next_task_when_no_task_active(config, &block) ⇒ Object


167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pomodoro/commands.rb', line 167

def edit_next_task_when_no_task_active(config, &block)
  time_frame(config) do |day, current_time_frame|
    if active_task = day.task_list.active_task
      abort t(:task_in_progress, task: active_task.body)
    end

    if next_task = current_time_frame.first_unstarted_task
      block.call(next_task, current_time_frame)
      day.save(config.today_path)
    else
      abort t(:no_more_tasks_in_time_frame, time_frame: current_time_frame.name)
    end
  end
end

#ensure_task_listObject


120
121
122
123
124
125
126
# File 'lib/pomodoro/commands.rb', line 120

def ensure_task_list
  unless self.config.task_list_path
    raise Pomodoro::Config::ConfigError, 'task_list_path'
  end

  self.must_exist(self.config.task_list_path)
end

#ensure_today(*args) ⇒ Object


114
115
116
117
118
# File 'lib/pomodoro/commands.rb', line 114

def ensure_today(*args)
  raise Pomodoro::Config::ConfigError, 'today_path' unless self.config.today_path

  self.must_exist(self.config.today_path(*args), "Run the <yellow>g</yellow> command first.")
end

#get_date_incrementObject


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pomodoro/commands.rb', line 71

def get_date_increment
  groups = @args.group_by { |argument| !!argument.match(/^[-+]\d$/) }
  @args = groups[false] || Array.new
  res = (groups[true] || Array.new).grep_v(/^[-+]0$/).map(&:to_i)

  case res.length
  when 0 then 0
  when 1 then res[0]
  else
    raise ArgumentError, "There cannot be more than 1 date indicator, was #{res.inspect}."
  end
end

#get_period_and_date(default_period = :day) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pomodoro/commands.rb', line 84

def get_period_and_date(default_period = :day)
  increment = self.get_date_increment
  period = (@args.first && @args.shift || default_period).to_sym

  if increment == 0
    [period, Date.today]
  else
    today = Date.today.extend(DataExts)

    direction = (increment < 0) ? :prev : :next
    method_name = :"#{direction}_#{period}"

    unless today.respond_to?(method_name)
      raise ArgumentError, "Unknown period: #{period}"
    end

    result = increment.abs.times.reduce(today) do |last_result|
      last_result.send(method_name).extend(DataExts)
    end

    [period, result]
  end
end

#must_exist(path, additional_info = nil) ⇒ Object


108
109
110
111
112
# File 'lib/pomodoro/commands.rb', line 108

def must_exist(path, additional_info = nil)
  unless File.exist?(path)
    abort ["<red>! File #{RR::Homepath.new(path)} doesn't exist.</red>", additional_info].compact.join("\n  ")
  end
end

#parse_task_list(config) ⇒ Object


140
141
142
143
144
# File 'lib/pomodoro/commands.rb', line 140

def parse_task_list(config)
  Pomodoro::Formats::Scheduled.parse(File.new(config.task_list_path, encoding: 'utf-8'))
rescue Errno::ENOENT
  raise Pomodoro::Config::ConfigError
end

#parse_today_list(config) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pomodoro/commands.rb', line 128

def parse_today_list(config)
  day = Pomodoro::Formats::Today.parse(File.new(config.today_path, encoding: 'utf-8'))

  if (active_tasks = day.task_list.each_task.select(&:in_progress?)).length > 1
    raise "There are 2 active tasks: #{active_tasks}"
  end

  day
rescue Errno::ENOENT
  raise Pomodoro::Config::ConfigError
end

#t(key, **options) ⇒ Object


182
183
184
185
186
# File 'lib/pomodoro/commands.rb', line 182

def t(key, **options)
  I18n.t!("commands.#{self.class.command_name}.#{key}", **options)
rescue I18n::MissingTranslationData
  I18n.t!("commands.generic.#{key}", **options)
end

#time_frame(config = self.config, &block) ⇒ Object


146
147
148
149
150
151
152
153
154
# File 'lib/pomodoro/commands.rb', line 146

def time_frame(config = self.config, &block)
  day = parse_today_list(config)

  unless current_time_frame = day.task_list.current_time_frame
    abort t(:no_active_time_frame)
  end

  block.call(day, current_time_frame)
end

#with_active_task(config, &block) ⇒ Object


156
157
158
159
160
161
162
163
164
165
# File 'lib/pomodoro/commands.rb', line 156

def with_active_task(config, &block)
  day = parse_today_list(config)
  if active_task = day.task_list.active_task
    block.call(active_task)
    day.save(config.today_path)
    return true
  else
    return false
  end
end