1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Extract Table to its own file

This commit is contained in:
☈king 2013-01-09 17:16:14 -06:00 committed by rking@sharpsaw.org
parent e171029de0
commit 139a2a7a0a
5 changed files with 186 additions and 183 deletions

View file

@ -1,94 +1,4 @@
class Pry
module Helpers
module Formatting
def self.tablify(things, line_length)
table = Table.new(things, :column_count => things.size)
table.column_count -= 1 until 0 == table.column_count or
table.fits_on_line?(line_length)
table.to_s
end
class Table
attr_reader :items, :column_count
def initialize items, args = {}
@column_count = args[:column_count]
self.items = items
end
def to_s
rows_to_s.join("\n")
end
def rows_to_s style = :color_on
widths = columns.map{|e| _max_width(e)}
@rows_without_colors.map do |r|
padded = []
r.each_with_index do |e,i|
next unless e
item = e.ljust(widths[i])
item.sub! e, recall_color_for(e) if :color_on == style
padded << item
end
padded.join(Pry.config.ls.separator)
end
end
def items= items
@items = items
_rebuild_colorless_cache
_recolumn
items
end
def column_count= n
@column_count = n
_recolumn
end
def fits_on_line? line_length
_max_width(rows_to_s :no_color) <= line_length
end
def columns
@rows_without_colors.transpose
end
def ==(other); items == other.to_a end
def to_a; items.to_a end
private
def _max_width(things)
things.compact.map(&:size).max || 0
end
def _rebuild_colorless_cache
@colorless_cache = {}
@plain_items = []
items.map do |e|
plain = Pry::Helpers::Text.strip_color(e)
@colorless_cache[plain] = e
@plain_items << plain
end
end
def _recolumn
@rows_without_colors = []
return if items.size.zero?
row_count = (items.size.to_f/column_count).ceil
row_count.times do |i|
row_indices = (0...column_count).map{|e| row_count*e+i}
@rows_without_colors << row_indices.map{|e| @plain_items[e]}
end
end
def recall_color_for thing
@colorless_cache[thing]
end
end
end
end
class Command::Ls < Pry::ClassCommand
match 'ls'
group 'Context'
@ -385,18 +295,8 @@ class Pry
# Add a new section to the output. Outputs nothing if the section would be empty.
def output_section(heading, body)
return "" if body.compact.empty?
"#{text.bold(color(:heading, heading))}: \n#{tablify(body)}\n"
end
def tablify(things)
things = things.compact
if TerminalInfo.screen_size.nil?
return things.join(Pry.config.ls.separator)
end
screen_width = (TerminalInfo.screen_size || [25, 80])[1]
Pry::Helpers::Formatting.tablify(things, screen_width)
table = Pry::Helpers.tablify_to_screen_width(body)
"#{text.bold(color(:heading, heading))}: \n#{table}\n"
end
# Color output based on config.ls.*_color

View file

@ -2,3 +2,4 @@ require "pry/helpers/base_helpers"
require "pry/helpers/options_helpers"
require "pry/helpers/command_helpers"
require "pry/helpers/text"
require "pry/helpers/table"

100
lib/pry/helpers/table.rb Normal file
View file

@ -0,0 +1,100 @@
class Pry
module Helpers
def self.tablify_to_screen_width(things)
things = things.compact
if TerminalInfo.screen_size.nil?
return things.join(Pry.config.ls.separator)
end
screen_width = (TerminalInfo.screen_size || [25, 80])[1]
tablify(things, screen_width)
end
def self.tablify(things, line_length)
table = Table.new(things, :column_count => things.size)
table.column_count -= 1 until 0 == table.column_count or
table.fits_on_line?(line_length)
table
end
class Table
attr_reader :items, :column_count
def initialize items, args = {}
@column_count = args[:column_count]
self.items = items
end
def to_s
rows_to_s.join("\n")
end
def rows_to_s style = :color_on
widths = columns.map{|e| _max_width(e)}
@rows_without_colors.map do |r|
padded = []
r.each_with_index do |e,i|
next unless e
item = e.ljust(widths[i])
item.sub! e, _recall_color_for(e) if :color_on == style
padded << item
end
padded.join(Pry.config.ls.separator)
end
end
def items= items
@items = items
_rebuild_colorless_cache
_recolumn
items
end
def column_count= n
@column_count = n
_recolumn
end
def fits_on_line? line_length
_max_width(rows_to_s :no_color) <= line_length
end
def columns
@rows_without_colors.transpose
end
def ==(other); items == other.to_a end
def to_a; items.to_a end
private
def _max_width(things)
things.compact.map(&:size).max || 0
end
def _rebuild_colorless_cache
@colorless_cache = {}
@plain_items = []
items.map do |e|
plain = Pry::Helpers::Text.strip_color(e)
@colorless_cache[plain] = e
@plain_items << plain
end
end
def _recolumn
@rows_without_colors = []
return if items.size.zero?
row_count = (items.size.to_f/column_count).ceil
row_count.times do |i|
row_indices = (0...column_count).map{|e| row_count*e+i}
@rows_without_colors << row_indices.map{|e| @plain_items[e]}
end
end
def _recall_color_for thing
@colorless_cache[thing]
end
end
end
end

View file

@ -28,87 +28,6 @@ describe "ls" do
end
end
describe 'Formatting Table' do
it 'knows about colorized fitting' do
t = Pry::Helpers::Formatting::Table.new %w(hihi), :column_count => 1
t.fits_on_line?(4).should == true
t.items = []
t.fits_on_line?(4).should == true
t.items = %w(hi hi)
t.fits_on_line?(4).should == true
t.column_count = 2
t.fits_on_line?(4).should == false
t.items = %w(
a ccc
bb dddd
).sort
t.fits_on_line?(8).should == true
t.fits_on_line?(7).should == false
end
end
describe 'formatting - should order downward and wrap to columns' do
FAKE_COLUMNS = 62
def try_round_trip(expected)
things = expected.split(/\s+/).sort
actual = Pry::Helpers::Formatting.tablify(things, FAKE_COLUMNS).strip
[expected, actual].each{|e| e.gsub! /\s+$/, ''}
if actual != expected
bar = '-'*25
puts \
bar+'expected'+bar,
expected,
bar+'actual'+bar,
actual
end
actual.should == expected
end
it 'should handle a tiny case' do
try_round_trip(<<-eot)
asdf asfddd fdass
eot
end
it 'should handle the basic case' do
try_round_trip(<<-eot)
aadd ddasffssdad sdsaadaasd ssfasaafssd
adassdfffaasds f sdsfasddasfds ssssdaa
assfsafsfsds fsasa ssdsssafsdasdf
eot
end
it 'should handle... another basic case' do
try_round_trip(<<-EOT)
aaad dasaasffaasf fdasfdfss safdfdddsasd
aaadfasassdfff ddadadassasdf fddsasadfssdss sasf
aaddaafaf dddasaaaaaa fdsasad sddsa
aas dfsddffdddsdfd ff sddsfsaa
adasadfaaffds dsfafdsfdfssda ffadsfafsaafa ss
asddaadaaadfdd dssdss ffssfsfafaadss ssas
asdsdaa faadf fsddfff ssdfssff
asfadsssaaad fasfaafdssd s
EOT
end
it 'should handle colors' do
try_round_trip(<<-EOT)
\e[31maaaaaaaaaa\e[0m \e[31mccccccccccccccccccccccccccccc\e[0m
\e[31mbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\e[0m \e[31mddddddddddddd\e[0m
EOT
end
it 'should handle empty input' do
try_round_trip('')
end
it 'should handle one-token input' do
try_round_trip('asdf')
end
end
describe "help" do
it 'should show help with -h' do
pry_eval("ls -h").should =~ /Usage: ls/

View file

@ -0,0 +1,83 @@
require 'helper'
describe 'Formatting Table' do
it 'knows about colorized fitting' do
t = Pry::Helpers::Table.new %w(hihi), :column_count => 1
t.fits_on_line?(4).should == true
t.items = []
t.fits_on_line?(4).should == true
t.items = %w(hi hi)
t.fits_on_line?(4).should == true
t.column_count = 2
t.fits_on_line?(4).should == false
t.items = %w(
a ccc
bb dddd
).sort
t.fits_on_line?(8).should == true
t.fits_on_line?(7).should == false
end
describe 'formatting - should order downward and wrap to columns' do
FAKE_COLUMNS = 62
def try_round_trip(expected)
things = expected.split(/\s+/).sort
actual = Pry::Helpers.tablify(things, FAKE_COLUMNS).to_s.strip
[expected, actual].each{|e| e.gsub! /\s+$/, ''}
if actual != expected
bar = '-'*25
puts \
bar+'expected'+bar,
expected,
bar+'actual'+bar,
actual
end
actual.should == expected
end
it 'should handle a tiny case' do
try_round_trip(<<-eot)
asdf asfddd fdass
eot
end
it 'should handle the basic case' do
try_round_trip(<<-eot)
aadd ddasffssdad sdsaadaasd ssfasaafssd
adassdfffaasds f sdsfasddasfds ssssdaa
assfsafsfsds fsasa ssdsssafsdasdf
eot
end
it 'should handle... another basic case' do
try_round_trip(<<-EOT)
aaad dasaasffaasf fdasfdfss safdfdddsasd
aaadfasassdfff ddadadassasdf fddsasadfssdss sasf
aaddaafaf dddasaaaaaa fdsasad sddsa
aas dfsddffdddsdfd ff sddsfsaa
adasadfaaffds dsfafdsfdfssda ffadsfafsaafa ss
asddaadaaadfdd dssdss ffssfsfafaadss ssas
asdsdaa faadf fsddfff ssdfssff
asfadsssaaad fasfaafdssd s
EOT
end
it 'should handle colors' do
try_round_trip(<<-EOT)
\e[31maaaaaaaaaa\e[0m \e[31mccccccccccccccccccccccccccccc\e[0m
\e[31mbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\e[0m \e[31mddddddddddddd\e[0m
EOT
end
it 'should handle empty input' do
try_round_trip('')
end
it 'should handle one-token input' do
try_round_trip('asdf')
end
end
end