-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtimer.rb
More file actions
executable file
·133 lines (107 loc) · 2.75 KB
/
Copy pathbtimer.rb
File metadata and controls
executable file
·133 lines (107 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/ruby
require 'yaml'
TIME_NOW = Time.now
class Timer
attr_reader :data
def initialize(data)
@data = data
end
def start
return if @data[:state] != :stopped
@data[:start] = TIME_NOW
@data[:state] = :started
end
def stop
return if @data[:state] != :started
@data[:state] = :stopped
@data[:stopped_at] = TIME_NOW
@data[:elapsed] += (TIME_NOW - @data[:start]).to_i
end
def set(str)
if str
@data[:elapsed] = self.class.get_seconds(str)
else
@data[:elapsed] = 0
@data[:state] = :started
end
@data[:start] = TIME_NOW
end
def add(minutes)
@data[:elapsed] += minutes * 60
end
def sub(minutes)
@data[:elapsed] -= minutes * 60
end
def elapsed_seconds
@data[:state] == :started ?
(TIME_NOW - @data[:start]).to_i + @data[:elapsed] : @data[:elapsed]
end
def elapsed_last_stop_seconds
TIME_NOW - @data[:stopped_at]
end
def formatted_elapsed_seconds
self.class.formatted_output(self.elapsed_seconds)
end
def formatted_elapsed_last_stop_seconds
self.class.formatted_output(self.elapsed_last_stop_seconds)
end
def formatted_state
@data[:state].to_s.capitalize
end
def self.formatted_output(seconds)
secs = seconds.to_i
mins = secs / 60
hours = mins / 60
fsecs = (secs % 60).to_s.rjust(2, '0')
fmins = (mins % 60).to_s.rjust(2, '0')
fhours = hours.to_s.rjust(2, '0')
if hours > 0
"#{fhours}:#{fmins}:#{fsecs}"
elsif mins > 0
"00:#{fmins}:#{fsecs}"
elsif secs >= 0
"00:00:#{fsecs}"
end
end
# get number of seconds from a time str like 'xx:xx:xx'
def self.get_seconds(str)
time = str.split(':')
time[0].to_i * 3600 + time[1].to_i * 60 + time[2].to_i
end
end
FILE_DIR = File.join(ENV['HOME'], '.btimer')
Dir.mkdir FILE_DIR unless File.exists? FILE_DIR
FILE_PATH = File.join(FILE_DIR, 'data.yml')
def save(data)
File.open(FILE_PATH, 'w') do |out|
YAML.dump(data, out)
end
end
data = YAML.load_file(FILE_PATH) rescue
{:start => TIME_NOW, :elapsed => 0,
:state => :stopped, :stopped_at => TIME_NOW}
t = Timer.new(data)
case ARGV[0]
when "start"
t.start
puts "Since last stop: " + t.formatted_elapsed_last_stop_seconds
when "stop"
t.stop
when "set"
t.set(ARGV[1])
t.start if ARGV[2] == "start"
t.stop if ARGV[2] == "stop"
when "add"
t.add ARGV[1].to_i
t.start if ARGV[2] == "start"
t.stop if ARGV[2] == "stop"
when "sub"
t.sub ARGV[1].to_i
t.start if ARGV[2] == "start"
t.stop if ARGV[2] == "stop"
when "help"
puts "timer [start|stop|set|add|sub]"
end
save t.data if ["start", "stop", "set", "add", "sub"].include? ARGV[0]
puts t.formatted_state
puts t.formatted_elapsed_seconds