Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align cells via filter #3

Open
wwboynton opened this issue Feb 23, 2015 · 5 comments
Open

Align cells via filter #3

wwboynton opened this issue Feb 23, 2015 · 5 comments
Labels

Comments

@wwboynton
Copy link

Given the ability to align a cell by replacing its value with a hash containing {value: val, alignment: align) of some sort, one would expect the ability to do this in filters at render-time as well. For instance,

table               = TTY::Table.new(table_data).render(*@render_info) do |renderer|
  renderer.border.style = @color
  renderer.filter       = Proc.new { |val, _, _| {:value => val, :alignment => :right} }
end

However, this produces the following:

1.9.3-p392 :028 > puts t.render(:unicode, multiline: true) do |renderer|
1.9.3-p392 :029 >     renderer.filter       = Proc.new { |val, _, _| {:value => val, :alignment => :right} }
1.9.3-p392 :030?>   end
┌───────────────┬────────────────┐
│host           │total           │
├───────────────┼────────────────┤
│test_value     │37              │
└───────────────┴────────────────┘
@wwboynton
Copy link
Author

In addition, I have had times where it has actually printed the hash into the table instead of the value

┌──────────────────────────────────────────┬─────────────────────────────────────┐
│{:value=>"host", :alignment=>:right}      │{:value=>"total", :alignment=>:right}│
├──────────────────────────────────────────┼─────────────────────────────────────┤
│{:value=>"test_value", :alignment=>:right}│{:value=>"37", :alignment=>:right}   │
└──────────────────────────────────────────┴─────────────────────────────────────┘

@wwboynton
Copy link
Author

On further inspection, the code from the readme does not work doing basic alignment at all.

1.9.3-p392 :041 > table = TTY::Table.new header: ['header1', 'header2']
 => #<TTY::Table header=#<TTY::Table::Header attributes=[#<TTY::Table::Field value="header1" name=nil width=7 colspan=1 rowspan=1 align=nil>, #<TTY::Table::Field value="header2" name=nil width=7 colspan=1 rowspan=1 align=nil>]> rows=[] orientation=#<TTY::Table::Orientation::Horizontal:0x007faa4b257dd8 @name=:horizontal> original_rows=nil original_columns=nil>
1.9.3-p392 :042 > table << [{value: 'a1', alignment: :right}, 'a2']
 => #<TTY::Table header=#<TTY::Table::Header attributes=[#<TTY::Table::Field value="header1" name=nil width=7 colspan=1 rowspan=1 align=nil>, #<TTY::Table::Field value="header2" name=nil width=7 colspan=1 rowspan=1 align=nil>]> rows=[#<TTY::Table::Row fields=["a1", "a2"]>] orientation=#<TTY::Table::Orientation::Horizontal:0x007faa4b257dd8 @name=:horizontal> original_rows=nil original_columns=nil>
1.9.3-p392 :043 > table << ['b1', {value: 'b2', alignment: :center}]
 => #<TTY::Table header=#<TTY::Table::Header attributes=[#<TTY::Table::Field value="header1" name=nil width=7 colspan=1 rowspan=1 align=nil>, #<TTY::Table::Field value="header2" name=nil width=7 colspan=1 rowspan=1 align=nil>]> rows=[#<TTY::Table::Row fields=["a1", "a2"]>, #<TTY::Table::Row fields=["b1", "b2"]>] orientation=#<TTY::Table::Orientation::Horizontal:0x007faa4b257dd8 @name=:horizontal> original_rows=nil original_columns=nil>
1.9.3-p392 :044 > puts table.render(:ascii)
+-------+-------+
|header1|header2|
+-------+-------+
|a1     |a2     |
|b1     |b2     |
+-------+-------+
 => nil

@piotrmurach
Copy link
Owner

I haven't had time to look into filtering yet. It is another thing on my list.

I'm rather surprised that the alignment doesn't work for you. Have you run the code of master branch? I haven't released any of the fixes as I want to make sure that all the major issues and refactoring are done. I have written quite few tests including this one to ensure that alignment works fine.

@piotrmurach
Copy link
Owner

The thing with filtering is that it is run as a last operation after the padding, alignment had a go on formatting the individual field. Therefore, even if I change the filter to accept the hash this would have no impact on rendering in terms of alignment etc... I guess I would need to provide before_filter and after_filter ? I'm not sure whether this is taking this a step too far?

I kind of like how the filter is the last step and allows you to transform the raw content to anything you want. Also filtering is the property of rendering and thus I would feel that hash syntax which is used for constructing data would feel a bit off the track. For example, what you could do is:

renderer.filter = proc { |val, row, col| Verse.align(val, :center) } 

Did you have a chance to look at the alignment?

@wwboynton
Copy link
Author

Yes, alignment by hash before render-time does indeed seem to be working as intended.

I do certainly feel that the inability to perform per-cell granular formatting would seem at home in the filter, though perhaps in some way other than the hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants