hbtrack 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f62e6b1a4c9978f97cd51b60ca91ea8508f945d1
4
- data.tar.gz: 463768cf9d7ef99a486c06b0ca93d6ec34c3fc4f
3
+ metadata.gz: 4c701ca5b2187759928514a35a48855eb185fbdc
4
+ data.tar.gz: 8ec5d9cc6b2b90ca2bef90c4d72e32e9fe096844
5
5
  SHA512:
6
- metadata.gz: b870785216a0d1371aeec39e7ff36aedcc57328cea1dfb21bafe1b588a6a26b1d2d404c656782b6b38925e707c5233eb82a2972cf00e773038841cba979da2e8
7
- data.tar.gz: 7d219a47e955b3d647bdc88817c12bc390c4b8caf6fde3f1ee813d57aba42a83a11edeef5f89255ffdf94c4b7fce2c5479a6b9a3a68924a33cb8932e1cbbdef9
6
+ metadata.gz: c8c9ec66a9f2fc796ed2a9c0c8e113625ba57c9cef97b30f274333d78d0ab3bcc0168495778da4a53015d1391e2b35c9a2332c5059b3426ec1329fe713b2fa63
7
+ data.tar.gz: da70844a1d91bc5852b86d76c0ca66de58b77b0359d175dded7da052a7d8489a52df888a1bd4725637521e0ccebc4fad14826869a084545088437d107452b620
data/README.md CHANGED
@@ -45,6 +45,42 @@ To look at individual habit progress:
45
45
  hbtrack list habit_name
46
46
  ```
47
47
 
48
+ ## Limitations
49
+
50
+ For the current version, some invalid inputs are not handled properly.
51
+
52
+ ## Data
53
+
54
+ All the data is stored in your home directory, in a hidden file named `.habit`. In Unix/Unix-like system, you can directly edit the file by:
55
+
56
+ ```
57
+ vim ~/.habit
58
+
59
+ ```
60
+
61
+ All the data is stored in the form of:
62
+
63
+ ```
64
+ workout
65
+ 2017,6: 111111100000110100110000000001
66
+ 2017,7: 11111111111111111111
67
+ ```
68
+
69
+ The first line represent the name of the habit. The second rows onward represent the progress of the habit for each month. More details:
70
+
71
+ * `2017,6` represent your progress during June 2017.
72
+ * `1` is used to represent done.
73
+ * `0` is used to represent undone.
74
+ * Each habit is seperated by a newline. For example:
75
+
76
+ ```
77
+ workout
78
+ 2017,7: 11111111111111111111
79
+
80
+ read
81
+ 2017,7: 11111111111110011111
82
+ ```
83
+
48
84
  ## Bugs/Features
49
85
  If there are any bugs/feature requesst, feel free to create a new issues.
50
86
 
data/lib/hbtrack/habit.rb CHANGED
@@ -11,6 +11,7 @@ module Hbtrack
11
11
  attr_reader :progress
12
12
 
13
13
  # Class Methods
14
+
14
15
  class << self
15
16
  # Generate hash key for progress based
16
17
  # on date.
@@ -49,6 +50,7 @@ module Hbtrack
49
50
  end
50
51
 
51
52
  # Public APIs
53
+
52
54
  def initialize(name,
53
55
  progress = {
54
56
  Habit.get_progress_key_from(Date.today) => ' '
@@ -95,6 +97,13 @@ module Hbtrack
95
97
  "#{Date::MONTHNAMES[key[1].to_i]} #{key[0]}: "
96
98
  end
97
99
 
100
+ # Get all of the progress of the habit in string form
101
+ #
102
+ # Example:
103
+ #
104
+ # habit.progress_output
105
+ # # => "2017,5: 0010001010\n2017,6: 000010010\n"
106
+ #
98
107
  def progress_output
99
108
  arr = @progress.map do |key, value|
100
109
  "#{key}:#{value}\n"
@@ -102,6 +111,33 @@ module Hbtrack
102
111
  arr.join('')
103
112
  end
104
113
 
114
+ def progress_stat
115
+ @progress.map do |key, value|
116
+ convert_key_to_date(key) + "\n" +
117
+ progress_stat_output_for(key)
118
+ end.join("\n")
119
+ end
120
+
121
+ def progress_stat_output_for(key)
122
+ hash = progress_stat_for(key)
123
+ Hbtrack::CLI.green("Done: #{hash[:done]}") + "\n" +
124
+ Hbtrack::CLI.red("Undone: #{hash[:undone]}") + "\n"
125
+ end
126
+
127
+ # Get the stat of the progress.
128
+ #
129
+ # key - Key for the progress (hash)
130
+ #
131
+ # Example:
132
+ #
133
+ # habit.progress_stat_for("2017,5".to_sym)
134
+ # # => { done: 5, undone: 2 }
135
+ def progress_stat_for(key)
136
+ undone = @progress[key].split("").count { |x| x == '0'}
137
+ done = @progress[key].lstrip.length - undone
138
+ { done: done, undone: undone }
139
+ end
140
+
105
141
  def to_s
106
142
  "#{name}\n" + progress_output + "\n"
107
143
  end
@@ -74,9 +74,8 @@ module Hbtrack
74
74
  def add(args)
75
75
  habit_name, _options = parse_options(args)
76
76
  find(habit_name) do
77
- @habits << Hbtrack::Habit.new(habit_name)
78
- save
79
- puts Hbtrack::CLI.green("#{habit_name} added succesfully!")
77
+ habit = create(habit_name)
78
+ save_to_file(habit, "Add") unless habit.nil?
80
79
  return
81
80
  end
82
81
  puts Hbtrack::CLI.blue("#{habit_name} already existed!")
@@ -86,34 +85,42 @@ module Hbtrack
86
85
  habit_name, options = parse_options(args)
87
86
  day = get_day_based_on(options)
88
87
  habit = find_or_create(habit_name)
89
- habit.done(true, day)
90
- save
91
- puts Hbtrack::CLI.green("Done #{habit_name}!") # Similar code
88
+ save_to_file(habit, "Done") do
89
+ habit.done(true, day)
90
+ end
92
91
  end
93
92
 
94
93
  def undone(args)
95
94
  habit_name, options = parse_options(args)
96
95
  day = get_day_based_on(options)
97
- habit = find(habit_name) { raise_habit_not_found }
98
- habit.done(false, day)
99
- save
100
- puts Hbtrack::CLI.blue("Undone #{habit_name}!") # Similar code
96
+ habit = find(habit_name) { raise_habit_not_found(habit_name) }
97
+ save_to_file(habit, "Undone", "blue") do
98
+ habit.done(false, day)
99
+ end
101
100
  end
102
101
 
103
102
  def remove(args)
104
103
  habit_name, _options = parse_options(args)
105
- habit = find(habit_name) { raise_habit_not_found }
106
- @habits.delete(habit)
107
- save
108
- puts Hbtrack::CLI.blue("#{habit_name} removed!") # Similar code
104
+ habit = find(habit_name) { raise_habit_not_found(habit_name) }
105
+ save_to_file(habit, "Remove", "blue") do
106
+ @habits.delete(habit)
107
+ end
109
108
  end
110
109
 
111
110
  def find_or_create(habit_name)
112
111
  habit = find(habit_name) do
112
+ habit = create(habit_name)
113
+ return habit
114
+ end
115
+ end
116
+
117
+ def create(habit_name)
118
+ unless habit_name.nil? || habit_name =~ /\s+/
113
119
  habit = Hbtrack::Habit.new(habit_name)
114
120
  @habits << habit
121
+ return habit
115
122
  end
116
- habit
123
+ puts Hbtrack::CLI.red("Invalid argument: habit_name is expected.")
117
124
  end
118
125
 
119
126
  def parse_options(args)
@@ -127,8 +134,10 @@ module Hbtrack
127
134
  Date.today - 1
128
135
  end
129
136
 
130
- def raise_habit_not_found
131
- puts Hbtrack::CLI.red "#{habit_name} not found."
137
+ def raise_habit_not_found(habit_name)
138
+ last_words = habit_name ? 'not found' : 'is expected'
139
+ habit_name ||= "habit_name"
140
+ puts Hbtrack::CLI.red "Invalid argument: #{habit_name} #{last_words}."
132
141
  end
133
142
 
134
143
  def save
@@ -138,6 +147,15 @@ module Hbtrack
138
147
  end
139
148
  end
140
149
  end
150
+
151
+ def save_to_file(habit, action, color = "green")
152
+ unless habit.nil?
153
+ yield if block_given?
154
+ save
155
+ output = "#{action} #{habit.name}!"
156
+ puts Hbtrack::CLI.public_send(color, output)
157
+ end
158
+ end
141
159
  end
142
160
  end
143
161
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hbtrack
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kw7oe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-18 00:00:00.000000000 Z
11
+ date: 2017-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler