hbtrack 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +67 -18
- data/lib/hbtrack/habit.rb +54 -80
- data/lib/hbtrack/habit_printer.rb +45 -0
- data/lib/hbtrack/habit_tracker.rb +46 -23
- data/lib/hbtrack/stat_formatter.rb +69 -0
- data/lib/hbtrack/util.rb +73 -0
- data/lib/hbtrack/version.rb +1 -1
- data/lib/hbtrack.rb +7 -0
- metadata +11 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 202f4fe27cf01abbf78f19ca7a3fd075e151c9ad
|
4
|
+
data.tar.gz: d0433a6543c279963e4876f372f0147d23b376fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33653f712de657b5fd7e40192a0d371f540a121740c265544c7dde950c221d7d8d58343e53d08105652de0e2e9f86af8344ec04e02cfa063aeb80e5a183d1565
|
7
|
+
data.tar.gz: be1d05fc272fbcf4a31ec40c92c5a7b49f4aa6ca5519424459b8e70963d7f581866b1265302f9b5896e8efe4f512fb0ca19a8b240633b8dbbc20428b6f62a68d
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
## Hbtrack
|
2
2
|
|
3
|
-
`hbtrack` is a simple
|
3
|
+
`hbtrack` is a simple command lines tool to keep track of your daily habits. The functionality of the current version is very limited.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -10,44 +10,91 @@ gem install hbtrack
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
|
13
|
+
#### Add a new habit
|
14
14
|
```
|
15
|
-
hbtrack add habit_name
|
15
|
+
$ hbtrack add habit_name
|
16
16
|
```
|
17
|
+
Duplicate habit name will be ignore.
|
17
18
|
|
18
|
-
|
19
|
+
#### Mark habit as done/undone
|
19
20
|
```
|
20
|
-
hbtrack done habit_name
|
21
|
+
$ hbtrack done habit_name
|
21
22
|
```
|
23
|
+
This will mark the current habit as done for the current day.
|
22
24
|
|
23
|
-
To mark your habit as undone for the current day:
|
24
25
|
```
|
25
|
-
hbtrack undone habit_name
|
26
|
+
$ hbtrack undone habit_name
|
26
27
|
```
|
28
|
+
This will mark the current habit as undone for the current day.
|
27
29
|
|
28
|
-
You can also mark your habit done/undone for the previous day by adding `-y` option:
|
30
|
+
You can also mark your habit done/undone for the previous day by adding `-y` or `--yesterday` option:
|
29
31
|
```
|
30
|
-
hbtrack done/undone -y habit_name
|
32
|
+
$ hbtrack done/undone -y habit_name
|
31
33
|
```
|
32
34
|
|
33
|
-
|
35
|
+
#### Remove a habit
|
34
36
|
```
|
35
|
-
hbtrack remove habit_name
|
37
|
+
$ hbtrack remove habit_name
|
36
38
|
```
|
37
39
|
|
38
|
-
|
40
|
+
#### Listing Progress
|
41
|
+
|
42
|
+
You can list all your habits progress by:
|
39
43
|
```
|
40
|
-
hbtrack list
|
44
|
+
$ hbtrack list
|
41
45
|
```
|
46
|
+
This will list all the habits you added and its progress for the current month.
|
42
47
|
|
43
|
-
|
48
|
+
**Output:**
|
44
49
|
```
|
45
|
-
|
50
|
+
August 2017
|
51
|
+
-----------
|
52
|
+
1. workout : *** All: 3, Done: 3, Undone: 0
|
53
|
+
2. read : *** All: 3, Done: 3, Undone: 0
|
54
|
+
3. programming : *** All: 3, Done: 3, Undone: 0
|
55
|
+
4. ukulele : *** All: 3, Done: 3, Undone: 0
|
56
|
+
5. sleep_early : *** All: 3, Done: 1, Undone: 2
|
57
|
+
|
58
|
+
Total
|
59
|
+
-----
|
60
|
+
All: 15, Done: 13, Undone: 2
|
46
61
|
```
|
47
62
|
|
48
|
-
|
63
|
+
**Note:** The actual output is colorized where green color font indicate done and red color font indicate undone.
|
64
|
+
|
65
|
+
You can also look at the progress of an individual habit by:
|
66
|
+
```
|
67
|
+
$ hbtrack list habit_name
|
68
|
+
```
|
69
|
+
|
70
|
+
**Output:**
|
71
|
+
```
|
72
|
+
workout
|
73
|
+
-------
|
74
|
+
July 2017 : ******************************* All: 31, Done: 26, Undone: 5
|
75
|
+
August 2017 : *** All: 3, Done: 3, Undone: 0
|
76
|
+
```
|
49
77
|
|
50
|
-
|
78
|
+
|
79
|
+
Extra options such as `-p` or `--percentage` can be provided to list the stats of your habits in terms of completion rate.
|
80
|
+
```
|
81
|
+
$ hbtrack list -p habit_name
|
82
|
+
```
|
83
|
+
|
84
|
+
With the extra options `-p`, the output will be:
|
85
|
+
```
|
86
|
+
August 2017
|
87
|
+
-----------
|
88
|
+
1. workout : *** Completion rate: 100.00%
|
89
|
+
2. read : *** Completion rate: 100.00%
|
90
|
+
3. programming : *** Completion rate: 100.00%
|
91
|
+
4. ukulele : *** Completion rate: 100.00%
|
92
|
+
5. sleep_early : *** Completion rate: 33.33%
|
93
|
+
|
94
|
+
Total
|
95
|
+
-----
|
96
|
+
Completion rate: 86.67%
|
97
|
+
```
|
51
98
|
|
52
99
|
## Data
|
53
100
|
|
@@ -82,7 +129,9 @@ read
|
|
82
129
|
```
|
83
130
|
|
84
131
|
## Bugs/Features
|
85
|
-
|
132
|
+
The project is still in development.
|
133
|
+
|
134
|
+
If there are any bugs/features request, feel free to create a new issues.
|
86
135
|
|
87
136
|
|
88
137
|
|
data/lib/hbtrack/habit.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'date'
|
4
|
-
require 'hbtrack/cli'
|
5
4
|
|
6
|
-
module Hbtrack
|
7
|
-
|
8
|
-
#
|
5
|
+
module Hbtrack
|
6
|
+
|
7
|
+
# Habit class
|
9
8
|
class Habit
|
10
9
|
attr_accessor :name
|
11
10
|
attr_reader :progress
|
@@ -26,9 +25,10 @@ module Hbtrack
|
|
26
25
|
|
27
26
|
# Initialize Habit object from string.
|
28
27
|
#
|
29
|
-
# string
|
28
|
+
# @param string [String] The string to be parse.
|
29
|
+
# @return [Habit] a habit object
|
30
30
|
#
|
31
|
-
# Example
|
31
|
+
# == Example
|
32
32
|
#
|
33
33
|
# Habit.initialize_from_string("workout\n2017,6:1001")
|
34
34
|
# # => #<Habit:0x007f9be6041b70 @name="workout",
|
@@ -50,8 +50,6 @@ module Hbtrack
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
# Public APIs
|
54
|
-
|
55
53
|
def initialize(name,
|
56
54
|
progress = {
|
57
55
|
Habit.get_progress_key_from(Date.today) => ''
|
@@ -60,66 +58,78 @@ module Hbtrack
|
|
60
58
|
@progress = progress
|
61
59
|
end
|
62
60
|
|
61
|
+
# The length of the habit name.
|
62
|
+
#
|
63
|
+
# @return [Numeric] length of the habit name
|
63
64
|
def name_length
|
64
65
|
name.length
|
65
66
|
end
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
# Get the latest progress key.
|
69
|
+
#
|
70
|
+
# @return [Symbol] latest progress key
|
71
|
+
def latest_key
|
72
|
+
key = Habit.get_progress_key_from(Date.today)
|
73
|
+
if progress[key].nil?
|
74
|
+
initialize_progress_hash_from(key)
|
75
|
+
end
|
76
|
+
key
|
69
77
|
end
|
70
78
|
|
79
|
+
# Get the latest progress.
|
80
|
+
#
|
81
|
+
# @return [String] value of the progress
|
71
82
|
def latest_progress
|
72
83
|
progress[latest_key]
|
73
84
|
end
|
74
85
|
|
86
|
+
# Get the stat for the latest progress.
|
87
|
+
#
|
88
|
+
# @return [Hash] stat for the progress.
|
89
|
+
def latest_stat
|
90
|
+
stat_for_progress(latest_key)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Find the month in progress with
|
94
|
+
# the longest name
|
95
|
+
#
|
96
|
+
# @return [String] month
|
75
97
|
def longest_month
|
76
98
|
key = progress.keys.max_by do |x|
|
77
|
-
get_month_from(x).length
|
99
|
+
Util.get_month_from(x).length
|
78
100
|
end
|
79
|
-
get_month_from(key)
|
101
|
+
Util.get_month_from(key)
|
80
102
|
end
|
81
103
|
|
104
|
+
# Update the status of the progress
|
105
|
+
#
|
106
|
+
# @param done [true, false] If true, it is marked as done.
|
107
|
+
# Else, marked as undone.
|
108
|
+
# @param date [Date] The date of the progress
|
109
|
+
# @return [void]
|
82
110
|
def done(done = true, date = Date.today)
|
83
111
|
key = Habit.get_progress_key_from(date)
|
84
112
|
initialize_progress_hash_from(key)
|
85
113
|
update_progress_for(key, date.day, done)
|
86
114
|
end
|
87
115
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
def
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
def pretty_print_progress(key)
|
102
|
-
stat = progress[key].split('').map do |x|
|
103
|
-
x == '0' ? Hbtrack::CLI.red('*') : Hbtrack::CLI.green('*')
|
104
|
-
end.join('')
|
105
|
-
stat + ' ' * (32 - progress[key].size) +
|
106
|
-
one_liner_progress_stat_output_for(key)
|
107
|
-
end
|
108
|
-
|
109
|
-
def convert_key_to_date(key, no_of_space)
|
110
|
-
year = key.to_s.split(",")[0]
|
111
|
-
' ' * no_of_space + get_month_from(key) +
|
112
|
-
" #{year}" + " : "
|
113
|
-
end
|
114
|
-
|
115
|
-
def get_month_from(key)
|
116
|
-
key = key.to_s.split(',')
|
117
|
-
Date::MONTHNAMES[key[1].to_i]
|
116
|
+
# Get the stat of the progress.
|
117
|
+
#
|
118
|
+
# @param key [Symbol] key for the progress
|
119
|
+
# @return [Hash] stat of the progress in the form of
|
120
|
+
# == Example:
|
121
|
+
#
|
122
|
+
# habit.stat_for_progress("2017,5".to_sym)
|
123
|
+
# # => { done: 5, undone: 2 }
|
124
|
+
def stat_for_progress(key)
|
125
|
+
undone = @progress[key].split('').count { |x| x == '0' }
|
126
|
+
done = @progress[key].length - undone
|
127
|
+
{ done: done, undone: undone }
|
118
128
|
end
|
119
129
|
|
120
130
|
# Get all of the progress of the habit in string form
|
121
131
|
#
|
122
|
-
# Example:
|
132
|
+
# == Example:
|
123
133
|
#
|
124
134
|
# habit.progress_output
|
125
135
|
# # => "2017,5: 0010001010\n2017,6: 000010010\n"
|
@@ -131,47 +141,11 @@ module Hbtrack
|
|
131
141
|
arr.join('')
|
132
142
|
end
|
133
143
|
|
134
|
-
def progress_stat
|
135
|
-
@progress.map do |key, _value|
|
136
|
-
convert_key_to_date(key, 0) + "\n" +
|
137
|
-
progress_stat_output_for(key)
|
138
|
-
end.join("\n")
|
139
|
-
end
|
140
|
-
|
141
|
-
def one_liner_progress_stat_output_for(key)
|
142
|
-
hash = progress_stat_for(key)
|
143
|
-
"(All: #{progress[key].size}," \
|
144
|
-
" Done: #{hash[:done]}, Undone: #{hash[:undone]})"
|
145
|
-
end
|
146
|
-
|
147
|
-
def progress_stat_output_for(key)
|
148
|
-
hash = progress_stat_for(key)
|
149
|
-
Hbtrack::CLI.green("Done: #{hash[:done]}") + "\n" +
|
150
|
-
Hbtrack::CLI.red("Undone: #{hash[:undone]}") + "\n"
|
151
|
-
end
|
152
|
-
|
153
|
-
# Get the stat of the progress.
|
154
|
-
#
|
155
|
-
# key - Key for the progress (hash)
|
156
|
-
#
|
157
|
-
# Example:
|
158
|
-
#
|
159
|
-
# habit.progress_stat_for("2017,5".to_sym)
|
160
|
-
# # => { done: 5, undone: 2 }
|
161
|
-
def progress_stat_for(key)
|
162
|
-
undone = @progress[key].split('').count { |x| x == '0' }
|
163
|
-
done = @progress[key].length - undone
|
164
|
-
{ done: done, undone: undone }
|
165
|
-
end
|
166
|
-
|
167
144
|
def to_s
|
168
145
|
"#{name}\n" + progress_output + "\n"
|
169
146
|
end
|
170
147
|
|
171
|
-
# Private APIs
|
172
|
-
|
173
148
|
private
|
174
|
-
|
175
149
|
def initialize_progress_hash_from(key)
|
176
150
|
@progress[key] = '' unless @progress.key? key
|
177
151
|
end
|
@@ -180,7 +154,7 @@ module Hbtrack
|
|
180
154
|
i = day - @progress[key].length - 1
|
181
155
|
result = @progress[key].split('')
|
182
156
|
i.times { result << '0' }
|
183
|
-
result[day] = done ? '1' : '0'
|
157
|
+
result[day-1] = done ? '1' : '0'
|
184
158
|
@progress[key] = result.join('')
|
185
159
|
end
|
186
160
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hbtrack
|
4
|
+
# This class contains the methods that
|
5
|
+
# are used to format the progress of a Habit
|
6
|
+
# into string
|
7
|
+
class HabitPrinter
|
8
|
+
|
9
|
+
def initialize(stat_formatter)
|
10
|
+
@stat_formatter = stat_formatter
|
11
|
+
end
|
12
|
+
|
13
|
+
def calculate_space_needed_for(habit, key)
|
14
|
+
habit.longest_month.length - Util.get_month_from(key).length
|
15
|
+
end
|
16
|
+
|
17
|
+
def print_latest_progress(habit, no_of_space = 0)
|
18
|
+
habit.name.to_s + ' ' * no_of_space + ' : ' +
|
19
|
+
print_progress(
|
20
|
+
habit.latest_progress,
|
21
|
+
habit.latest_stat
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def print_all_progress(habit)
|
26
|
+
habit.progress.map do |key, value|
|
27
|
+
space = calculate_space_needed_for(habit, key)
|
28
|
+
Util.convert_key_to_date(key, space) +
|
29
|
+
print_progress(
|
30
|
+
value,
|
31
|
+
habit.stat_for_progress(key)
|
32
|
+
)
|
33
|
+
end.join("\n")
|
34
|
+
end
|
35
|
+
|
36
|
+
def print_progress(progress, stat)
|
37
|
+
output = progress.split('').map do |x|
|
38
|
+
x == '0' ? CLI.red('*') : CLI.green('*')
|
39
|
+
end.join('')
|
40
|
+
output + ' ' * (32 - progress.size) +
|
41
|
+
@stat_formatter.format(stat)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -1,32 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'hbtrack/version'
|
4
|
-
require 'hbtrack/habit'
|
5
|
-
require 'hbtrack/cli'
|
6
|
-
require 'hbtrack/config'
|
7
|
-
|
8
3
|
module Hbtrack
|
9
4
|
# This class contains the methods to
|
10
5
|
# handle the operation of mutliple habits
|
11
6
|
class HabitTracker
|
12
|
-
attr_reader :habits
|
7
|
+
attr_reader :habits, :hp
|
13
8
|
|
14
9
|
def self.help # Refactoring needed
|
15
|
-
puts 'usage: hbtrack list [ habit_name ]'
|
10
|
+
puts 'usage: hbtrack list [-p] [ habit_name ]'
|
16
11
|
puts ' hbtrack add habit_name'
|
17
12
|
puts ' hbtrack done [-y] habit_name'
|
18
13
|
puts ' hbtrack undone [-y] habit_name'
|
19
14
|
puts ' hbtrack remove habit_name'
|
20
15
|
end
|
21
16
|
|
22
|
-
def initialize(file =
|
23
|
-
output =
|
17
|
+
def initialize(file = FILE_NAME,
|
18
|
+
output = FILE_NAME)
|
24
19
|
@habits = []
|
25
20
|
@file_name = file
|
26
21
|
@output_file_name = output
|
22
|
+
@sf = CompleteSF.new
|
23
|
+
@hp = HabitPrinter.new(@sf)
|
27
24
|
initialize_habits_from_file
|
28
25
|
end
|
29
26
|
|
27
|
+
def initialize_habits_from_file
|
28
|
+
return unless File.exist?(@file_name)
|
29
|
+
input = File.read(@file_name).split(/\n\n/)
|
30
|
+
input.each { |string| @habits << Habit.initialize_from_string(string) }
|
31
|
+
end
|
32
|
+
|
30
33
|
def parse_arguments(args)
|
31
34
|
head = args.shift
|
32
35
|
tail = args
|
@@ -51,16 +54,23 @@ module Hbtrack
|
|
51
54
|
HabitTracker.help
|
52
55
|
end
|
53
56
|
|
54
|
-
|
57
|
+
def total_habits_stat
|
58
|
+
@habits.reduce({done: 0, undone: 0}) do |stat, habit|
|
59
|
+
stat[:done] += habit.latest_stat[:done]
|
60
|
+
stat[:undone] += habit.latest_stat[:undone]
|
61
|
+
stat
|
62
|
+
end
|
63
|
+
end
|
55
64
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
input.each { |string| @habits << Hbtrack::Habit.initialize_from_string(string) }
|
65
|
+
def overall_stat_description
|
66
|
+
Util.title("Total") +
|
67
|
+
@sf.format(total_habits_stat)
|
60
68
|
end
|
61
69
|
|
70
|
+
private
|
62
71
|
def list(args)
|
63
|
-
habit_name,
|
72
|
+
habit_name, options = parse_options(args)
|
73
|
+
set_sf_based_on(options)
|
64
74
|
habit = find(habit_name) do
|
65
75
|
if habit_name.nil?
|
66
76
|
list_all_habits
|
@@ -69,14 +79,26 @@ module Hbtrack
|
|
69
79
|
end
|
70
80
|
return
|
71
81
|
end
|
72
|
-
puts habit.
|
82
|
+
puts Util.title habit.name
|
83
|
+
puts @hp.print_all_progress(habit)
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_sf_based_on(options)
|
87
|
+
return if options.empty?
|
88
|
+
if options[0] == '-p' || options[0] == '--percentage'
|
89
|
+
@sf = CompletionRateSF.new
|
90
|
+
@hp = HabitPrinter.new(@sf)
|
91
|
+
end
|
73
92
|
end
|
74
93
|
|
75
94
|
def list_all_habits
|
95
|
+
puts Util.title Util.current_month
|
76
96
|
@habits.each_with_index do |h, index|
|
77
97
|
space = longest_name.length - h.name_length
|
78
|
-
puts "#{index + 1}.
|
98
|
+
puts "#{index + 1}. " +
|
99
|
+
"#{@hp.print_latest_progress(h, space)}"
|
79
100
|
end
|
101
|
+
puts "\n" + overall_stat_description
|
80
102
|
end
|
81
103
|
|
82
104
|
def add(args)
|
@@ -86,7 +108,7 @@ module Hbtrack
|
|
86
108
|
save_to_file(habit, 'Add') unless habit.nil?
|
87
109
|
return
|
88
110
|
end
|
89
|
-
puts
|
111
|
+
puts CLI.blue("#{habit_name} already existed!")
|
90
112
|
end
|
91
113
|
|
92
114
|
def done(args)
|
@@ -124,7 +146,7 @@ module Hbtrack
|
|
124
146
|
|
125
147
|
def create(habit_name)
|
126
148
|
unless invalid_habit_name?(habit_name)
|
127
|
-
habit =
|
149
|
+
habit = Habit.new(habit_name)
|
128
150
|
@habits << habit
|
129
151
|
return habit
|
130
152
|
end
|
@@ -143,12 +165,13 @@ module Hbtrack
|
|
143
165
|
end
|
144
166
|
|
145
167
|
def get_day_based_on(options)
|
146
|
-
|
168
|
+
yesterday = options[0] == '-y' || options[0] == '--yesterday'
|
169
|
+
return Date.today unless yesterday
|
147
170
|
Date.today - 1
|
148
171
|
end
|
149
172
|
|
150
173
|
def raise_error_msg(msg)
|
151
|
-
puts
|
174
|
+
puts CLI.red msg
|
152
175
|
return
|
153
176
|
end
|
154
177
|
|
@@ -158,7 +181,7 @@ module Hbtrack
|
|
158
181
|
end
|
159
182
|
|
160
183
|
def raise_habit_not_found(habit_name)
|
161
|
-
raise_error_msg "Invalid
|
184
|
+
raise_error_msg "Invalid habit: #{habit_name} not found."
|
162
185
|
end
|
163
186
|
|
164
187
|
def raise_invalid_arguments
|
@@ -187,7 +210,7 @@ module Hbtrack
|
|
187
210
|
yield if block_given?
|
188
211
|
save
|
189
212
|
output = "#{action} #{habit.name}!"
|
190
|
-
puts
|
213
|
+
puts CLI.public_send(color, output)
|
191
214
|
end
|
192
215
|
end
|
193
216
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hbtrack
|
4
|
+
# This is an abstract class for classes that
|
5
|
+
# are used to format the progress of a Habit
|
6
|
+
# into string
|
7
|
+
class StatFormatter
|
8
|
+
def initialize
|
9
|
+
end
|
10
|
+
|
11
|
+
def format
|
12
|
+
raise "Not Implemented"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class DoneUndoneSF < StatFormatter
|
17
|
+
# Format in terms of the count of done and undone.
|
18
|
+
# @param hash [Hash]
|
19
|
+
# @option hash [String] :done total of done
|
20
|
+
# @option hash [String] :undone total of undone
|
21
|
+
# @return [String] formatted result
|
22
|
+
def format(hash)
|
23
|
+
CLI.green("Done: #{hash[:done]}") + "\n" +
|
24
|
+
CLI.red("Undone: #{hash[:undone]}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class CompleteSF < StatFormatter
|
29
|
+
# Format in terms of the total and the count of
|
30
|
+
# done and undone.
|
31
|
+
# @param hash [Hash]
|
32
|
+
# @option hash [String] :done total of done
|
33
|
+
# @option hash [String] :undone total of undone
|
34
|
+
# @return [String] formatted result
|
35
|
+
def format(hash)
|
36
|
+
total = hash[:done] + hash[:undone]
|
37
|
+
"All: #{total}, Done: #{hash[:done]}, Undone: #{hash[:undone]}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class CompletionRateSF < StatFormatter
|
42
|
+
# Format in terms of the completion rate of the habit.
|
43
|
+
# @param hash [Hash]
|
44
|
+
# @option hash [String] :done total of done
|
45
|
+
# @option hash [String] :undone total of undone
|
46
|
+
# @return [String] formatted result
|
47
|
+
def format(hash)
|
48
|
+
percentage = to_percentage(hash)[:done]
|
49
|
+
"Completion rate: #{'%.2f' % percentage}%"
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convert the value in the hash into percentage
|
53
|
+
# @param hash [Hash]
|
54
|
+
# @option hash [String] :done total of done
|
55
|
+
# @option hash [String] :undone total of undone
|
56
|
+
# @return [Hash] formatted result
|
57
|
+
def to_percentage(hash)
|
58
|
+
total = hash[:done] + hash[:undone]
|
59
|
+
done_p = 0
|
60
|
+
undone_p = 0
|
61
|
+
unless total == 0
|
62
|
+
done_p = hash[:done] / total.to_f * 100
|
63
|
+
undone_p = hash[:undone] / total.to_f * 100
|
64
|
+
end
|
65
|
+
{ done: done_p, undone: undone_p }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
data/lib/hbtrack/util.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hbtrack
|
4
|
+
# This class contains the methods that
|
5
|
+
# are used to format the progress of a Habit
|
6
|
+
# into string
|
7
|
+
class Util
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Format the string with title style.
|
12
|
+
#
|
13
|
+
# @param string [String] the string to be styled as title
|
14
|
+
# @return [Nil]
|
15
|
+
#
|
16
|
+
# == Example
|
17
|
+
#
|
18
|
+
# puts Util.title("Title")
|
19
|
+
# # Title
|
20
|
+
# # -----
|
21
|
+
# #=> nil
|
22
|
+
def title(string)
|
23
|
+
string + "\n" +
|
24
|
+
"-" * string.length + "\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
# Convert key into date in string form.
|
28
|
+
#
|
29
|
+
# @param key [Symbol] The key of the progress in the
|
30
|
+
# form of :'year, month'. Example: :"2017,7"
|
31
|
+
# @param no_of_space [Numeric] number of space to be
|
32
|
+
# added in front
|
33
|
+
# @return [String] a string in date form.
|
34
|
+
#
|
35
|
+
# == Example
|
36
|
+
#
|
37
|
+
# Util.convert_key_to_date(:"2017,7", 0)
|
38
|
+
# #=> "July 2016 : "
|
39
|
+
def convert_key_to_date(key, no_of_space)
|
40
|
+
year = key.to_s.split(",")[0]
|
41
|
+
' ' * no_of_space + get_month_from(key) +
|
42
|
+
" #{year}" + " : "
|
43
|
+
end
|
44
|
+
|
45
|
+
# Format the current month and year into string
|
46
|
+
#
|
47
|
+
# @return [String] Month and Year in String.
|
48
|
+
#
|
49
|
+
# == Example
|
50
|
+
#
|
51
|
+
# Util.current_month
|
52
|
+
# #=> "August 2017"
|
53
|
+
def current_month
|
54
|
+
Date.today.strftime("%B %Y")
|
55
|
+
end
|
56
|
+
|
57
|
+
# Get the month in string form from given key
|
58
|
+
#
|
59
|
+
# @param key [Symbol] The key of the progress
|
60
|
+
# @return [String] month
|
61
|
+
#
|
62
|
+
# == Example
|
63
|
+
#
|
64
|
+
# Util.get_month_from(:"2017,7")
|
65
|
+
# #=> "July"
|
66
|
+
def get_month_from(key)
|
67
|
+
key = key.to_s.split(',')
|
68
|
+
Date::MONTHNAMES[key[1].to_i]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
data/lib/hbtrack/version.rb
CHANGED
data/lib/hbtrack.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'hbtrack/habit_tracker'
|
4
|
+
require 'hbtrack/version'
|
5
|
+
require 'hbtrack/cli'
|
6
|
+
require 'hbtrack/util'
|
7
|
+
require 'hbtrack/config'
|
8
|
+
require 'hbtrack/habit'
|
9
|
+
require 'hbtrack/stat_formatter'
|
10
|
+
require 'hbtrack/habit_printer'
|
4
11
|
|
5
12
|
module Hbtrack
|
6
13
|
class << self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hbtrack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kw7oe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '5.10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '5.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
|
-
description: Habit Tracker CLI
|
55
|
+
description: Habit Tracker CLI
|
56
56
|
email:
|
57
57
|
- choongkwern@hotmail.com
|
58
58
|
executables:
|
@@ -72,7 +72,10 @@ files:
|
|
72
72
|
- lib/hbtrack/cli.rb
|
73
73
|
- lib/hbtrack/config.rb
|
74
74
|
- lib/hbtrack/habit.rb
|
75
|
+
- lib/hbtrack/habit_printer.rb
|
75
76
|
- lib/hbtrack/habit_tracker.rb
|
77
|
+
- lib/hbtrack/stat_formatter.rb
|
78
|
+
- lib/hbtrack/util.rb
|
76
79
|
- lib/hbtrack/version.rb
|
77
80
|
homepage: https://github.com/kw7oe/hbtrack
|
78
81
|
licenses:
|
@@ -94,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
97
|
version: '0'
|
95
98
|
requirements: []
|
96
99
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.5.2
|
98
101
|
signing_key:
|
99
102
|
specification_version: 4
|
100
103
|
summary: A CLI to track your habits.
|