Sha256: 366ebe1c30c0150c6664ba855acb8a46aa6448abe85f24f5794eaa7eae69a63d

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'hbtrack/util'

module Hbtrack
  # This class contains the methods that
  # are used to format the progress of a Habit
  # into string
  class HabitPrinter
    attr_accessor :formatter

    def initialize(formatter = CompleteSF.new)
      @formatter = formatter
    end

    def calculate_space_needed_for(habit, key)
      habit.longest_month.length - Util.get_month_from(key).length
    end

    def print_latest_progress(habit, no_of_space = 0)
      habit.name.to_s + ' ' * no_of_space + ' : ' +
        print_progress(
          habit.latest_progress,
          habit.latest_stat
        )
    end

    def print_all_progress(habit)
      habit.progress.map do |key, value|
        space = calculate_space_needed_for(habit, key)
        Util.convert_key_to_date(key, space) +
          print_progress(
            value,
            habit.stat_for_progress(key)
          )
      end.join("\n")
    end

    def print_progress(progress, stat)
      output = progress.split('').map do |x|
        if x == '0'
          Util.red('*')
        elsif x == '1'
          Util.green('*')
        else
          ' '
        end
      end.join('')
      output + ' ' * (32 - progress.size) +
        @formatter.format(stat)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hbtrack-0.0.7 lib/hbtrack/habit_printer.rb
hbtrack-0.0.6 lib/hbtrack/habit_printer.rb