Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`TextChart` is a gem that helps you generate text charts, like this one:
```text
text_chart demonstration
Goal: Show you how cool this is
Show you how cool this is

9 |'''''''''''''''''''''''''''''''''''''''''''''''''''''###
8 | ###
Expand Down
22 changes: 14 additions & 8 deletions lib/text_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# representing the chart, just like this one:
#
# text_chart demonstration
# Goal: Show you how cool this is
# Show you how cool this is
#
# 9 |'''''''''''''''''''''''''''''''''''''''''''''''''''''###
# 8 | ###
Expand All @@ -25,24 +25,30 @@ class TextChart
class Error < StandardError; end

# @param [String] title
# @param [String] goal
# @param [String] subtitle
# @param [Array] data
def initialize(title, goal, data)
# @param [Boolean] colors
def initialize(title, subtitle, data, colors = false)
@title = title
@goal = goal
@subtitle = subtitle
@data = data.empty? ? [0] : data
@colors = colors
@refs = define_references
@size_calculator = SizeCalculator.new(self)
@designer = Designer.new(self, @size_calculator)
end

attr_reader :title, :goal, :refs, :data, :size_calculator, :designer
attr_reader :title, :subtitle, :refs, :data, :size_calculator, :designer

# @return [String]
def to_s
@designer.draw_axis
@designer.draw_bars
@designer.draw_header.join
result = @designer.draw_axis &&
@designer.draw_bars &&
@designer.draw_header

result = @designer.paint if @colors

result.join
end

# @param [Symbol] key
Expand Down
30 changes: 27 additions & 3 deletions lib/text_chart/designer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

class TextChart::Designer
# @param [TextChart] text_chart
# @param [TextChart::SizeCalculator] size_calc
Expand All @@ -14,7 +12,7 @@ def initialize(text_chart, size_calc)
def draw_header
header = []
header << "#{@text_chart.title}\n"
header << "Goal: #{@text_chart.goal}\n"
header << "#{@text_chart.subtitle}\n"
header << "\n"

@chart_canvas.prepend(*header)
Expand Down Expand Up @@ -67,6 +65,18 @@ def draw_bars
@chart_canvas
end

# @return [Array<String>]
def paint
@chart_canvas.map do |row|
next if row.gsub!(@text_chart.title, colorize(@text_chart.title, :bold))

row.gsub!(/-?\d/) { colorize($&, :cyan) }
row.gsub!(/'+/) { colorize($&, :cyan) }
row.gsub!(/#+/) { colorize($&, :blue) }
end
@chart_canvas
end

private

def build_empty_chart
Expand Down Expand Up @@ -134,4 +144,18 @@ def draw_reference_line(chart_line, line_start, line_end)
current_position += 1
end
end

# @param string to be colorized [String]
# @param desired color/formatting [Symbol]
# @return colorized string [String]
def colorize(str, format)
case format
when :bold
"\e[1m#{str}\e[22m"
when :cyan
"\e[36m#{str}\e[0m"
when :blue
"\e[34m#{str}\e[0m"
end
end
end
31 changes: 11 additions & 20 deletions test/text_chart/designer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

class TextChart::DesignerTest < Test::Unit::TestCase
test "#draw_header" do
text_chart = TextChart.new("This is a nice title", "This is a nice goal", [])
text_chart = TextChart.new("This is a nice title", "This is a nice subtitle", [])

result = text_chart.designer.draw_header.join

assert_equal result, <<~EXPECTED
This is a nice title
Goal: This is a nice goal
This is a nice subtitle



Expand All @@ -28,13 +28,12 @@ class TextChart::DesignerTest < Test::Unit::TestCase
with_gaps_result = with_gaps_designer.draw_axis.join
with_negative_number_result = with_negative_number.draw_axis.join

expected_no_sample = <<~END
assert_equal no_sample_result, <<~END
0 |
----------
END
assert_equal no_sample_result, expected_no_sample

expected_small_sample = <<~END
assert_equal small_sample_result, <<~END
10 |
9 |
8 |
Expand All @@ -48,9 +47,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
0 |
----------------------------------------------------------------------------------------------------
END
assert_equal small_sample_result, expected_small_sample

expected_with_gaps = <<~END
assert_equal with_gaps_result, <<~END
10 |
9 |
8 |
Expand All @@ -64,9 +62,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
0 |
------------------------------
END
assert_equal with_gaps_result, expected_with_gaps

expected_with_negative_number = <<~END
assert_equal with_negative_number_result, <<~END
3 |
2 |
1 |
Expand All @@ -76,7 +73,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
-3 |
----------------------------------------------------------------------
END
assert_equal with_negative_number_result, expected_with_negative_number
end

test "#draw_bars" do
Expand All @@ -100,7 +96,7 @@ class TextChart::DesignerTest < Test::Unit::TestCase
gaps_result = gaps.draw_bars.join
negative_result = negative.draw_bars.join

expected_sorted_result = <<-END
assert_equal sorted_result, <<-END
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ### ###
Expand All @@ -114,9 +110,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ### ### ### ### ### ###

END
assert_equal sorted_result, expected_sorted_result

expected_random_order_result = <<-END
assert_equal random_order_result, <<-END
'''''''''''''###
'''''''''''''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ###
Expand All @@ -130,9 +125,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ### ### ### ### ### ###

END
assert_equal random_order_result, expected_random_order_result

expected_with_zero_result = <<-END
assert_equal with_zero_result, <<-END
'''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''''''''''''### ###
'''''''''''''''''''''''###'''''''''''''''''### ###
Expand All @@ -141,9 +135,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
'''###'''''''###'''''''###'''''''### ### ###

END
assert_equal with_zero_result, expected_with_zero_result

expected_gaps_result = <<-END
assert_equal gaps_result, <<-END
'''''''''''''''''''''''###
###
###
Expand All @@ -159,9 +152,8 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ###

END
assert_equal gaps_result, expected_gaps_result

expected_negative_result = <<-END
assert_equal negative_result, <<-END
'''###
'''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''###'''''''''''''''''''''''''''''''''''''### ###
Expand All @@ -171,6 +163,5 @@ class TextChart::DesignerTest < Test::Unit::TestCase
'''###'''''''###'''''''###'''''''### ### ### ###

END
assert_equal negative_result, expected_negative_result
end
end
28 changes: 23 additions & 5 deletions test/text_chart_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ class TextChartTest < Test::Unit::TestCase
with_negative_numbers = TextChart.new(
"With negative numbers sample", "Testing", [*-3..3].shuffle(random: Random.new(1))
)
with_colors = TextChart.new(
"With colors", "Testing", [*-3..3].shuffle(random: Random.new(1)), true
)

no_sample_result = no_sample.to_s
sorted_result = sorted_sample.to_s
random_order_result = random_order_sample.to_s
duplicated_and_gaps_result = duplicated_and_gaps.to_s
with_negative_numbers_result = with_negative_numbers.to_s
with_colors_result = with_colors.to_s

assert_equal no_sample_result, <<~EXPECTED
No sample
Goal: Testing
Testing

0 |'''###
----------
EXPECTED
assert_equal sorted_result, <<~EXPECTED
Sorted sample
Goal: Testing
Testing

10 |'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
9 |'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ###
Expand All @@ -54,7 +58,7 @@ class TextChartTest < Test::Unit::TestCase
EXPECTED
assert_equal random_order_result, <<~EXPECTED
Random order sample
Goal: Testing
Testing

10 |'''''''''''''###
9 |'''''''''''''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
Expand All @@ -71,7 +75,7 @@ class TextChartTest < Test::Unit::TestCase
EXPECTED
assert_equal duplicated_and_gaps_result, <<~EXPECTED
Duplicated and gaps sample
Goal: Testing
Testing

12 |'''###
11 | ###
Expand All @@ -90,7 +94,7 @@ class TextChartTest < Test::Unit::TestCase
EXPECTED
assert_equal with_negative_numbers_result, <<~EXPECTED
With negative numbers sample
Goal: Testing
Testing

3 |'''###
2 |'''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
Expand All @@ -101,5 +105,19 @@ class TextChartTest < Test::Unit::TestCase
-3 |'''###'''''''###'''''''###'''''''### ### ### ###
----------------------------------------------------------------------
EXPECTED

assert_equal with_colors_result, <<~EXPECTED
\e[1mWith colors\e[22m
Testing

\e[36m3\e[0m |\e[36m'''\e[0m\e[34m###\e[0m
\e[36m2\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''''''''''''''''''''''''''''''''''''''''''''''''''''\e[0m\e[34m###\e[0m
\e[36m1\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''''''''''''''''''''''''''''''''\e[0m\e[34m###\e[0m \e[34m###\e[0m
\e[36m0\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''''''''''''''''''''''''''''''''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m \e[34m###\e[0m
\e[36m-1\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m
\e[36m-2\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m
\e[36m-3\e[0m |\e[36m'''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m\e[36m'''''''\e[0m\e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m \e[34m###\e[0m
----------------------------------------------------------------------
EXPECTED
end
end