Class: Muon::App

Inherits:
Object
  • Object
show all
Defined in:
lib/muon/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, home_dir = ENV["HOME"], output = $stdout) ⇒ App

Returns a new instance of App.



10
11
12
13
14
15
16
# File 'lib/muon/app.rb', line 10

def initialize(working_dir, home_dir = ENV["HOME"], output = $stdout)
  @working_dir = working_dir
  @home_dir = home_dir
  @output = output
  @project = Project.new(working_dir)
  @config = Config.new(config_file, global_config_file)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



18
19
20
# File 'lib/muon/app.rb', line 18

def project
  @project
end

Instance Method Details

#abort_trackingObject



34
35
36
# File 'lib/muon/app.rb', line 34

def abort_tracking
  project.abort_tracking
end

#commit_entry(start_time, end_time) ⇒ Object



38
39
40
41
42
# File 'lib/muon/app.rb', line 38

def commit_entry(start_time, end_time)
  start_time = Time.parse(start_time)
  end_time = Time.parse(end_time)
  project.commit_entry(start_time, end_time)
end

#dealiasify(args) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/muon/app.rb', line 85

def dealiasify(args)
  dealiased = @config.get_option("alias.#{args.first}")
  if dealiased
    [dealiased] + args.drop(1)
  else
    args
  end
end

#global_projectsObject



110
111
112
113
114
# File 'lib/muon/app.rb', line 110

def global_projects
  File.open(global_projects_file, "r") do |f|
    f.lines.to_a.map(&:strip).map { |path| Project.new(path) }
  end
end

#init_directoryObject



20
21
22
# File 'lib/muon/app.rb', line 20

def init_directory
  project.init_directory
end

#read_config_option(key, options = {}) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/muon/app.rb', line 94

def read_config_option(key, options = {})
  if options[:global]
    @output.puts @config.get_global_option(key)
  else
    @output.puts @config.get_option(key)
  end
end

#register_global_projectObject



116
117
118
# File 'lib/muon/app.rb', line 116

def register_global_project
  File.open(global_projects_file, "a") { |f| f << "#{@working_dir}\n" }
end

#set_config_option(key, value, options = {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/muon/app.rb', line 102

def set_config_option(key, value, options = {})
  if options[:global]
    @config.set_global_option(key, value)
  else
    @config.set_option(key, value)
  end
end

#set_goal(duration) ⇒ Object



79
80
81
82
83
# File 'lib/muon/app.rb', line 79

def set_goal(duration)
  duration = ChronicDuration.parse(duration)
  project.goal = duration
  @output.puts "Setting goal for this month to #{Format.duration duration}."
end

#show_goalObject



70
71
72
73
74
75
76
77
# File 'lib/muon/app.rb', line 70

def show_goal
  if project.has_goal?
    @output.puts "The goal for this month is #{Format.duration project.goal}."
    @output.puts "Time left to achieve this goal: #{Format.duration project.goal_remaining_time}."
  else
    @output.puts "No goal has been set."
  end
end

#show_log(limit = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/muon/app.rb', line 62

def show_log(limit = nil)
  entries = @project.history_entries
  entries = entries.take(limit) if limit
  entries.each do |entry|
    @output.puts "#{entry.start_time} - #{entry.end_time} (#{Format.duration entry.duration})"
  end
end

#show_statusObject



44
45
46
47
48
49
50
51
# File 'lib/muon/app.rb', line 44

def show_status
  if @project.tracking?
    @output.puts "Time tracking is running since #{Format.duration @project.tracking_duration}."
  else
    @output.puts "Time tracking is stopped."
  end
  @output.puts "Today's total time is #{Format.duration @project.day_total_time(Time.now)}."
end

#show_total(date = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/muon/app.rb', line 53

def show_total(date = nil)
  if date.nil?
    date = Time.now
  else
    date = Time.parse(date)
  end
  @output.puts "Total time on #{date.strftime('%Y-%m-%d')} is #{Format.duration @project.day_total_time(date)}."
end

#start_tracking(start_time = nil) ⇒ Object



24
25
26
27
# File 'lib/muon/app.rb', line 24

def start_tracking(start_time = nil)
  start_time = Time.parse(start_time) if start_time
  project.start_tracking(start_time)
end

#stop_tracking(end_time = nil) ⇒ Object



29
30
31
32
# File 'lib/muon/app.rb', line 29

def stop_tracking(end_time = nil)
  end_time = Time.parse(end_time) if end_time
  project.stop_tracking(end_time)
end