Skip to content

Commit

Permalink
got some fun pretty text. yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
andymeneely committed Nov 28, 2016
1 parent 9668786 commit 5329009
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/squib/import/data_frame.rb
@@ -1,3 +1,5 @@
# encoding: UTF-8

require 'json'
require 'forwardable'

Expand Down Expand Up @@ -63,6 +65,21 @@ def to_h
@hash
end

def to_pretty_text
max_col = columns.inject(0) { |max, c | c.length > max ? c.length : max }
top = " ╭#{'-' * 35}\n"
bottom = " ╰#{'-' * 35}\n"
str = ''
0.upto(nrows - 1) do | i |
str += (' ' * max_col) + top
row(i).each do |col, data|
str += "#{col.rjust(max_col)} #{wrap_n_pad(data, max_col)}"
end
str += (' ' * max_col) + bottom
end
return str
end

private

def snake_case(str)
Expand All @@ -74,5 +91,15 @@ def snake_case(str)
to_sym
end

def wrap_n_pad(str, max_col)
str.to_s.
concat(' '). # handle nil & empty strings
scan(/.{1,34}/).
map { |s| (' ' * max_col) + " | " + s.ljust(34) }.
join(" |\n").
lstrip. # initially no whitespace next to key
concat(" |\n")
end

end
end
77 changes: 77 additions & 0 deletions spec/import/data_frame_spec.rb
@@ -1,3 +1,5 @@
# encoding: UTF-8

require 'spec_helper'
require 'squib/import/data_frame'

Expand Down Expand Up @@ -164,4 +166,79 @@
end
end

context :wrap_n_pad do
subject { Squib::DataFrame.new basic }

it 'returns a handles a single line' do
expect(subject.send(:wrap_n_pad, 'a', 3)).
to eq "| a |\n"
end

it 'wraps multiple a lines at 34 characters' do
expect(subject.send(:wrap_n_pad, 'a' * 40, 3)).
to eq <<-EOS
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| aaaaaa |
EOS
end
end

context :to_pretty_text do

let(:verbose) do
{
'Name' => ['Mage', 'Rogue', 'Warrior'],
'Cost' => [1, 2, 3],
'Description' => [
'Magic, dude.',
'I like to be sneaky',
'I have a long story to tell to test the word-wrapping ability of pretty text formatting.'
],
'Snark' => [nil, '', ' ']
}
end

it 'returns fun ascii art of the card' do
data = Squib::DataFrame.new verbose
expect(data.to_pretty_text).to eq <<-EOS
╭-----------------------------------╮
Name | Mage |
Cost | 1 |
Description | Magic, dude. |
Snark | |
╰-----------------------------------╯
╭-----------------------------------╮
Name | Rogue |
Cost | 2 |
Description | I like to be sneaky |
Snark | |
╰-----------------------------------╯
╭-----------------------------------╮
Name | Warrior |
Cost | 3 |
Description | I have a long story to tell to tes |
| t the word-wrapping ability of pre |
| tty text formatting. |
Snark | |
╰-----------------------------------╯
EOS
end

let(:utf8_fun) do
{
'Fun with UTF8!' => ['👊'],
}
end

it 'is admittedly janky with multibyte chars' do
# I hate how Ruby handles multibyte chars. Blech!
data = Squib::DataFrame.new utf8_fun
expect(data.to_pretty_text).to eq <<-EOS
╭-----------------------------------╮
Fun with UTF8! | 👊 |
╰-----------------------------------╯
EOS
end
end

end

0 comments on commit 5329009

Please sign in to comment.