Module: Hbtrack::CLI::View
Instance Method Summary collapse
-
#convert_entry_to_view(entry) ⇒ Object
Create the string representation of a entry to be presented to the user.
-
#convert_status_to_view(status) ⇒ Object
Create the string representation of a status and colorized it accordingly.
-
#list_all_habits(habits, entries, month_key) ⇒ Object
Create the string output of command ‘hbtrack list` a.k.a ListCommand.list_all.
-
#max_char_count(strings) ⇒ Object
Iterate through an array of string to find the largest character count from the array of string.
-
#print_entries(entries) ⇒ Object
Create the string representation of a hash of entries.
-
#print_entry(period, entry, space) ⇒ Object
Create the string representation of an entry with its date period.
-
#print_habit(index, title, entry, space = 0) ⇒ Object
Create the string representation of a habit and its entries to be presented to the user.
-
#print_habits(habits, entries) ⇒ Object
Create the string representation of the habits and its entries to be presented to the user.
- #show_habit(habit, entries) ⇒ Object
Instance Method Details
#convert_entry_to_view(entry) ⇒ Object
Create the string representation of a entry to be presented to the user
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hbtrack/cli/view.rb', line 60 def convert_entry_to_view(entry) max_day_of_month = 31 index = 0 result = Array.new(max_day_of_month, ' ') entry.each do |e| date = e[:timestamp] index = date.day result[index - 1] = convert_status_to_view(e[:type]) end result.slice!(0, index).join('') end |
#convert_status_to_view(status) ⇒ Object
Create the string representation of a status and colorized it accordingly
74 75 76 77 |
# File 'lib/hbtrack/cli/view.rb', line 74 def convert_status_to_view(status) return Util.green '*' if status.start_with? 'completed' return Util.red '*' end |
#list_all_habits(habits, entries, month_key) ⇒ Object
Create the string output of command ‘hbtrack list` a.k.a ListCommand.list_all
12 13 14 15 16 |
# File 'lib/hbtrack/cli/view.rb', line 12 def list_all_habits(habits, entries, month_key) date = Util.get_date_from(key: month_key) Util.title(date.strftime('%B %Y')) + print_habits(habits, entries) end |
#max_char_count(strings) ⇒ Object
Iterate through an array of string to find the largest character count from the array of string.
82 83 84 |
# File 'lib/hbtrack/cli/view.rb', line 82 def max_char_count(strings) strings.map(&:size).max end |
#print_entries(entries) ⇒ Object
Create the string representation of a hash of entries.
44 45 46 47 48 49 |
# File 'lib/hbtrack/cli/view.rb', line 44 def print_entries(entries) char_count = max_char_count entries.keys entries.map do |month, entry| print_entry(month, entry, char_count - month.size) end.join("\n") end |
#print_entry(period, entry, space) ⇒ Object
Create the string representation of an entry with its date period. E.g “September 2017”.
54 55 56 |
# File 'lib/hbtrack/cli/view.rb', line 54 def print_entry(period, entry, space) "#{period}#{' ' * space} : " + convert_entry_to_view(entry) end |
#print_habit(index, title, entry, space = 0) ⇒ Object
Create the string representation of a habit and its entries to be presented to the user
37 38 39 40 |
# File 'lib/hbtrack/cli/view.rb', line 37 def print_habit(index, title, entry, space = 0) "#{index}. #{title}#{' ' * space} : " + convert_entry_to_view(entry) end |
#print_habits(habits, entries) ⇒ Object
Create the string representation of the habits and its entries to be presented to the user
26 27 28 29 30 31 32 |
# File 'lib/hbtrack/cli/view.rb', line 26 def print_habits(habits, entries) char_count = max_char_count habits.map { |h| h[:title] } habits.map.with_index(1) do |habit, index| title = habit[:title] print_habit(index, title, entries[title], char_count - title.size) end.join("\n") end |