hbtrack 0.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6548ef2dc5d0749d46b22a6efa32862a394dc92
4
+ data.tar.gz: f0c93fbc9490a2e9edb909b022efa775828120eb
5
+ SHA512:
6
+ metadata.gz: c75506ae3edd3e8593ed1c679a82581ce450dd7073357a9f5e65e0fcac6029392fd6b9a2b0a2869716d2ccf98733ff9169ed730bbfb37680a97992b5c79ff6b7
7
+ data.tar.gz: c10a01dbb04a7f1f9610211c5c1c6afb8cc93bb9850d2cac0abf0ab09dffe2f3be2191aca76f3ef8ae8ea91164ab6ac2d915de03a09ea1faac3bd7499187a161
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ .DS_Store
2
+ .rubocop.yml
3
+ test/test_output
4
+ /.bundle/
5
+ /.yardoc
6
+ /Gemfile.lock
7
+ /_yardoc/
8
+ /coverage/
9
+ /doc/
10
+ /pkg/
11
+ /spec/reports/
12
+ /tmp/
13
+ *.gem
14
+ *.gemspec
15
+
16
+
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at choongkwern@hotmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in apple.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Choong Kai Wern
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ #### Hbtrack
2
+
3
+ TODO: Add intro
4
+
5
+ #### Installation
6
+
7
+ TODO
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/exe/hbtrack ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'hbtrack'
3
+ Hbtrack.run(ARGV)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A class that contains the helper methods
4
+ # used for CLI.
5
+ module Hbtrack
6
+ class CLI
7
+ FONT_COLOR = {
8
+ green: "\e[32m",
9
+ red: "\e[31m",
10
+ blue: "\e[34m"
11
+ }.freeze
12
+
13
+ class << self
14
+ # Define CLI.green, CLI.red, CLI.blue
15
+ # which colorize string output in terminal
16
+ FONT_COLOR.each do |key, value|
17
+ define_method(key) do |string|
18
+ value + string + "\e[0m"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configuration for the application.
4
+ module Hbtrack
5
+ # File to read during testing
6
+ TEST_FILE = 'test/test_data'
7
+
8
+ # File to write to during testing
9
+ OUTPUT_FILE = 'test/test_output'
10
+
11
+ # File to store your data
12
+ FILE_NAME = Dir.home + '/.habit'
13
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'hbtrack/cli'
5
+
6
+ module Hbtrack
7
+ # Habit provide a rich library to track the
8
+ # progress of your habit.
9
+ class Habit
10
+ attr_accessor :name
11
+ attr_reader :progress
12
+
13
+ # Class Methods
14
+ class << self
15
+ # Generate hash key for progress based
16
+ # on date.
17
+ #
18
+ # Example
19
+ #
20
+ # Habit.get_progress_key_from(Date.new(2017, 7, 18))
21
+ # # => :"2017,7"
22
+ def get_progress_key_from(date)
23
+ date.strftime('%Y,%-m').to_sym
24
+ end
25
+
26
+ # Initialize Habit object from string.
27
+ #
28
+ # string - The string to be parse.
29
+ #
30
+ # Example
31
+ #
32
+ # Habit.initialize_from_string("workout\n2017,6:1001")
33
+ # # => #<Habit:0x007f9be6041b70 @name="workout",
34
+ # # @progress={:"2017,6"=>"1001"}>
35
+ #
36
+ # Habit.initialize_from_string("")
37
+ # # => nil
38
+ def initialize_from_string(string)
39
+ return nil if string.empty?
40
+ arr = string.split("\n")
41
+ habit_name = arr.shift
42
+ hash = {}
43
+ arr.each do |s|
44
+ a = s.split(':')
45
+ hash[a[0].to_sym] = a[1]
46
+ end
47
+ Habit.new(habit_name, hash)
48
+ end
49
+ end
50
+
51
+ # Public APIs
52
+ def initialize(name,
53
+ progress = {
54
+ Habit.get_progress_key_from(Date.today) => ' '
55
+ })
56
+ @name = name
57
+ @progress = progress
58
+ end
59
+
60
+ def name_length
61
+ name.length
62
+ end
63
+
64
+ def latest_progress
65
+ key = Habit.get_progress_key_from(Date.today)
66
+ progress[key]
67
+ end
68
+
69
+ def done(done = true, date = Date.today)
70
+ key = Habit.get_progress_key_from(date)
71
+ initialize_progress_hash_from(key)
72
+ update_progress_for(key, date.day, done)
73
+ end
74
+
75
+ def pretty_print_all
76
+ @progress.map do |key, value|
77
+ convert_key_to_date(key) + pretty_print_progress(value)
78
+ end.join("\n")
79
+ end
80
+
81
+ def pretty_print_latest(no_of_space = 0)
82
+ name.to_s + ' ' * no_of_space + ' : ' +
83
+ pretty_print_progress(latest_progress)
84
+ end
85
+
86
+ def pretty_print_progress(progress_value)
87
+ stat = progress_value.lstrip.split('').map do |x|
88
+ x == '0' ? Hbtrack::CLI.red('*') : Hbtrack::CLI.green('*')
89
+ end.join('')
90
+ stat
91
+ end
92
+
93
+ def convert_key_to_date(key)
94
+ key = key.to_s.split(',')
95
+ "#{Date::MONTHNAMES[key[1].to_i]} #{key[0]}: "
96
+ end
97
+
98
+ def progress_output
99
+ arr = @progress.map do |key, value|
100
+ "#{key}:#{value}\n"
101
+ end
102
+ arr.join('')
103
+ end
104
+
105
+ def to_s
106
+ "#{name}\n" + progress_output + "\n"
107
+ end
108
+
109
+ # Private APIs
110
+
111
+ private
112
+
113
+ def initialize_progress_hash_from(key)
114
+ @progress[key] = ' ' unless @progress.key? key
115
+ end
116
+
117
+ def update_progress_for(key, day, done)
118
+ i = day - @progress[key].length
119
+ result = @progress[key].split('')
120
+ i.times { result << '0' }
121
+ result[day] = done ? '1' : '0'
122
+ @progress[key] = result.join('')
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hbtrack/version'
4
+ require 'hbtrack/habit'
5
+ require 'hbtrack/cli'
6
+ require 'hbtrack/config'
7
+
8
+ module Hbtrack
9
+ # This class contains the methods to
10
+ # handle the operation of mutliple habits
11
+ class HabitTracker
12
+ attr_reader :habits
13
+
14
+ def self.help # Refactoring needed
15
+ puts 'usage: hbtrack list [ habit_name ]'
16
+ puts ' hbtrack add habit_name'
17
+ puts ' hbtrack done habit_name'
18
+ puts ' hbtrack undone habit_name'
19
+ end
20
+
21
+ def initialize(file = Hbtrack::FILE_NAME,
22
+ output = Hbtrack::FILE_NAME)
23
+ @habits = []
24
+ @file_name = file
25
+ @output_file_name = output
26
+ initialize_habits_from_file
27
+ end
28
+
29
+ def parse_arguments(args)
30
+ head = args.shift
31
+ tail = args
32
+ send(head, tail)
33
+ end
34
+
35
+ # This methods find a habit based on the name given.
36
+ # Blocks are executed when habit is not found.
37
+ def find(habit_name)
38
+ habit = @habits.find do |h|
39
+ h.name == habit_name
40
+ end
41
+ yield if habit.nil? && block_given?
42
+ habit
43
+ end
44
+
45
+ def longest_name
46
+ @habits.max_by(&:name_length).name
47
+ end
48
+
49
+ private
50
+
51
+ def initialize_habits_from_file
52
+ return unless File.exist?(@file_name)
53
+ input = File.read(@file_name).split(/\n\n/)
54
+ input.each { |string| @habits << Hbtrack::Habit.initialize_from_string(string) }
55
+ end
56
+
57
+ def list(args)
58
+ habit_name, _options = parse_options(args)
59
+ habit = find(habit_name) do
60
+ @habits.each_with_index do |h, index|
61
+ space = longest_name.length - h.name_length
62
+ puts "#{index + 1}. #{h.pretty_print_latest(space)}"
63
+ end
64
+ return
65
+ end
66
+ puts habit.pretty_print_all
67
+ end
68
+
69
+ def add(args)
70
+ habit_name, _options = parse_options(args)
71
+ find(habit_name) do
72
+ @habits << Hbtrack::Habit.new(habit_name)
73
+ save
74
+ puts Hbtrack::CLI.green("#{habit_name} added succesfully!")
75
+ return
76
+ end
77
+ puts Hbtrack::CLI.blue("#{habit_name} already existed!")
78
+ end
79
+
80
+ def done(args)
81
+ habit_name, options = parse_options(args)
82
+ day = get_day_based_on(options)
83
+ habit = find_or_create(habit_name)
84
+ habit.done(true, day)
85
+ save
86
+ puts Hbtrack::CLI.green("Done #{habit_name}!") # Similar code
87
+ end
88
+
89
+ def undone(args)
90
+ habit_name, options = parse_options(args)
91
+ day = get_day_based_on(options)
92
+ habit = find(habit_name) { raise_habit_not_found }
93
+ habit.done(false, day)
94
+ save
95
+ puts Hbtrack::CLI.blue("Undone #{habit_name}!") # Similar code
96
+ end
97
+
98
+ def remove(args)
99
+ habit_name, _options = parse_options(args)
100
+ habit = find(habit_name) { raise_habit_not_found }
101
+ @habits.delete(habit)
102
+ save
103
+ puts Hbtrack::CLI.blue("#{habit_name} removed!") # Similar code
104
+ end
105
+
106
+ def find_or_create(habit_name)
107
+ habit = find(habit_name) do
108
+ habit = Hbtrack::Habit.new(habit_name)
109
+ @habits << habit
110
+ end
111
+ habit
112
+ end
113
+
114
+ def parse_options(args)
115
+ options = args.select { |x| x =~ /\A-/ }
116
+ args -= options
117
+ [args[0], options]
118
+ end
119
+
120
+ def get_day_based_on(options)
121
+ return Date.today unless options[0] == '-y'
122
+ Date.today - 1
123
+ end
124
+
125
+ def raise_habit_not_found
126
+ puts Hbtrack::CLI.red "#{habit_name} not found."
127
+ end
128
+
129
+ def save
130
+ File.open(@output_file_name, 'w') do |f|
131
+ @habits.each do |habit|
132
+ f.puts habit
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hbtrack
4
+ VERSION = '0.0.0'
5
+ end
data/lib/hbtrack.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hbtrack/habit_tracker'
4
+
5
+ module Hbtrack
6
+ class << self
7
+ def run *args
8
+ if ARGV.empty?
9
+ HabitTracker.help
10
+ else
11
+ HabitTracker.new.parse_arguments(*args)
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hbtrack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kw7oe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Habit Tracker CLI
56
+ email:
57
+ - choongkwern@hotmail.com
58
+ executables:
59
+ - hbtrack
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rubocop.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - exe/hbtrack
71
+ - lib/hbtrack.rb
72
+ - lib/hbtrack/cli.rb
73
+ - lib/hbtrack/config.rb
74
+ - lib/hbtrack/habit.rb
75
+ - lib/hbtrack/habit_tracker.rb
76
+ - lib/hbtrack/version.rb
77
+ homepage: https://github.com/kw7oe/hbtrack
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A CLI to track your habits.
101
+ test_files: []