Skip to content

Commit

Permalink
First version of width formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
albertbellonch committed Feb 9, 2013
1 parent 5a483e4 commit 1916ff1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
Empty file modified demo.rb
100644 → 100755
Empty file.
25 changes: 21 additions & 4 deletions lib/nyan_cat_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ def eol
#
# @return [Fixnum]
def current_width
padding = @example_count.to_s.length * 2 + 6
cat_length = nyan_cat.split("\n").group_by(&:size).max.first
padding + @current + cat_length
padding_width + example_width + cat_length
end

# Gets the padding for the current example count
#
# @return [Fixnum]
def padding_width
@example_count.to_s.length * 2 + 6
end

# A Unix trick using stty to get the console columns
Expand Down Expand Up @@ -112,13 +117,18 @@ def scoreboard
#
# @return [String] the sprintf format of the Nyan cat
def nyan_trail
marks = @example_results.map{ |mark| highlight(mark) }
marks = @example_results.map.with_index{ |mark, i| highlight(mark) * example_width(i) }
marks.shift(current_width - terminal_width) if current_width >= terminal_width
nyan_cat_lines = nyan_cat.split("\n").each_with_index.map do |line, index|
format("%s#{line}", marks.join)
end.join("\n")
end

# Times a mark has to be repeated
def example_width(item = 1)
1
end

# Ascii version of Nyan cat. Two cats in the array allow Nyan to animate running.
#
# @param o [String] Nyan's eye
Expand Down Expand Up @@ -202,5 +212,12 @@ def failed_or_pending?
(@failure_count.to_i > 0 || @pending_count.to_i > 0)
end

# Returns the cat length
#
# @returns [Fixnum]
def cat_length
nyan_cat.split("\n").group_by(&:size).max.first

This comment has been minimized.

Copy link
@docwhat

docwhat Feb 25, 2014

Contributor

simpler: nyat_cat.split("\n").map(&:size).max

end

end

15 changes: 15 additions & 0 deletions lib/nyan_cat_wide_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
require 'nyan_cat_formatter'

NyanCatWideFormatter = Class.new(NyanCatFormatter) do
def example_width(item = current)
net_width_for(item) - net_width_for(item - 1)
end

def net_width_for(example)
return 0 if example < 0
net_width = terminal_width - padding_width - cat_length
rough_example_width = (net_width * example.to_f / @example_count.to_f)
rough_example_width.round
end
end
32 changes: 32 additions & 0 deletions spec/nyan_cat_wide_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'stringio'
require 'nyan_cat_wide_formatter'

describe NyanCatWideFormatter do
before do
@output = StringIO.new
@formatter = NyanCatWideFormatter.new(@output)
end

describe "cat situation" do
before { @formatter.stub!(:terminal_width).and_return(100) }

[35].each do |n|
context "for #{n} examples" do
before do
@formatter.start(n)
end

(0..n).each do |i|
context "when in example #{i}" do
before { i.times { @formatter.tick } }

it "should calculate the example width properly" do
[2,3].include?(@formatter.example_width(i))
end
end
end
end
end
end
end

0 comments on commit 1916ff1

Please sign in to comment.