Class: Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(clockout) ⇒ Printer

Returns a new instance of Printer.



29
30
31
# File 'lib/printer.rb', line 29

def initialize(clockout)
	@clockout = clockout
end

Instance Method Details

#align(strings, cols = COLS, sep = " ") ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/printer.rb', line 33

def align(strings, cols = COLS, sep = " ")
    ret = ""
    size = 0
    strings.each do |string, method|
        ultimate = (string == strings.keys[-1])
        penultimate = (string == strings.keys[-2])

        out = string
        out += " " unless (ultimate || penultimate)

        if ultimate
            # Add seperator
            cols_left = cols - size - out.length
            ret += sep*cols_left if cols_left > 0
        elsif penultimate
            last = strings.keys.last.length
            max_len = cols - size - last - 1
            if string.length > max_len
                # Truncate
                out = string[0..max_len-5].strip + "... "
            end
        end

        # Apply color & print
        ret += method.to_proc.call(out)

        size += out.length
    end

    ret
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/printer.rb', line 65

def print_chart(condensed)
    cols = condensed ? 30 : COLS
    total_sum = 0
    current_day = nil
    @clockout.blocks.each do |block|
        date = block.first.date.strftime(DAY_FORMAT)
        if date != current_day
            puts if (!condensed && current_day)

            current_day = date

            sum = @clockout.time_per_day[date]
            total_sum += sum

            puts align({date => :magenta, sum.as_time(:hours) => :red}, cols, ".".magenta)
        end

        print_timeline(block) if (!condensed)
    end

    puts align({"-"*10 => :magenta}, cols)
    puts align({total_sum.as_time(:hours) => :red}, cols)
end


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/printer.rb', line 118

def print_estimations
    sum = 0
    estimations = []
    @clockout.blocks.each do |block|
        estimations << block.first if block.first.estimated
    end

    if estimations.empty?
        puts "No estimations made."
    else
        estimations.each do |c|
            date = c.date.strftime('%b %e')+":"
            sha = c.sha[0..7]
            time = c.minutes.as_time

            puts align({date => :yellow, sha => :red, c.message => :to_s, time => :light_blue})

            sum += c.minutes
        end

        puts align({"-"*10 => :light_blue})
        puts align({sum.as_time(:hours) => :light_blue})
    end
end


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/printer.rb', line 89

def print_timeline(block)
    # subtract from the time it took for first commit
    time = (block.first.date - block.first.minutes*60).strftime('%l:%M %p')+":  "
    print time.yellow

    char_count = time.length

    block.each do |commit|
        c_mins = commit.minutes.as_time(nil, "m", "h")
        c_mins = "*#{c_mins}" if commit.clocked_in
        c_mins += "*" if commit.clocked_out

        seperator = " | "
    
        add = c_mins.length+seperator.length
        if char_count + add > COLS-5
            puts
            char_count = time.length # indent by the length of the time label on left
            print " "*char_count
        end

        char_count += add

        # Blue for clockin/out commits
        print c_mins+(commit.message ? seperator.red : seperator.light_blue)
    end
    puts
end