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

Fixes #3520 - respect special chars width #218

Merged
merged 1 commit into from Nov 28, 2016

Conversation

shlomizadok
Copy link
Member

No description provided.

@@ -1,5 +1,6 @@
require 'table_print'
require File.join(File.dirname(__FILE__), 'wrapper_formatter')
require File.join(File.dirname(__FILE__), '../../../table_print/formatter')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly hate adding this ../../../ - any other way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't just 'table_print/formatter' work? if the gem is available I'd expect it to work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing and the file formatter.rb is actually not being found at all. I'd move it from lib somewhere to 'lib/hammer_cli' and then just do

require 'hammer_cli/...'

@ares
Copy link
Member

ares commented Sep 27, 2016

It would be also great to try fix it in upstream - arches/table_print#2 </unwanted tip>

@shlomizadok
Copy link
Member Author

This monkey patch is breaking tests.
@mbacovsky opened a patch on the gem at arches/table_print#45
I think the only change I made was the gsub /[\u0080-\u00ff]/

Anyhow, we should try and have this on table_print and not here.

Copy link
Member

@tstrachota tstrachota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the fix doesn't work for me. It seems the file with patch is not found and the require fails silently.

@@ -1,5 +1,6 @@
require 'table_print'
require File.join(File.dirname(__FILE__), 'wrapper_formatter')
require File.join(File.dirname(__FILE__), '../../../table_print/formatter')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing and the file formatter.rb is actually not being found at all. I'd move it from lib somewhere to 'lib/hammer_cli' and then just do

require 'hammer_cli/...'

@tstrachota
Copy link
Member

@shlomizadok I discovered a few things while testing this PR:

The tests are failing because they don't find the file with monkey patch. When I was testing it manually it was calculating length for chinese characters ok, but it failed for czech letters like "žščř". I found out that this is primarily because of false assumption in the original table_print code. The author expects every multibyte character to be rendered in two-columns, which is not correct (https://github.com/arches/table_print/blob/master/lib/table_print/formatter.rb#L44).

So I wrote couple of tests and dug deeper into the topic. I realized it's much more complex than one would expect :) I ended up with horrible Pascal-like algorithm that calculates correct length and is able to truncate strings containing both colours and two-column characters. You can find it all here:
shlomizadok#1

Can you please review it? I'll be glad for any suggestions for making the algorithm cleaner and more readable. I wasn't able to improve it any further at the point I was writing it:)

@domcleal
Copy link
Contributor

When I was testing it manually it was calculating length for chinese characters ok, but it failed for czech letters like "žščř". I found out that this is primarily because of false assumption in the original table_print code. The author expects every multibyte character to be rendered in two-columns, which is not correct

The unicode-display_width gem provides a standard way of determining the display length, I'd strongly recommend using this instead of reimplementing it:

irb(main):001:0> Unicode::DisplayWidth.of("žščř")
=> 4

@tstrachota
Copy link
Member

Nice, it's surely better to use existing solution.

@tstrachota
Copy link
Member

Updated. Unfortunately it can't cope with colours so some custom code still remains.

@tstrachota
Copy link
Member

Merging that disqualifies me from doing the review :) May I ask somebody else? @mbacovsky or @ares ?

@ares
Copy link
Member

ares commented Nov 15, 2016

It seems some characters still breaks it :-(

I added second name column in hammer-cli-foreman to see if it's aligned properly...
hammer

@tstrachota
Copy link
Member

@ares you're right, that was introduced after I switched to unicode-display_width. Before that commit it works fine for czech letters. I have to add such values to the tests.

@tstrachota
Copy link
Member

@ares updated in shlomizadok#2

@ares
Copy link
Member

ares commented Nov 16, 2016

I checkout your branch and it works just fine, 👍 ACK once Shlomi merges the PR, I guess some packaging will be required once this gets in.

@mmoll
Copy link
Contributor

mmoll commented Nov 16, 2016

@ares only the unicode-display_width gem needs to get packaged and hammer updated, or am I missing something? :)

@ares
Copy link
Member

ares commented Nov 16, 2016

That's what I meant, I just wanted to kindly remind authors to open packaging PRs :-)

@mmoll
Copy link
Contributor

mmoll commented Nov 22, 2016

what's the status here?

@tstrachota
Copy link
Member

I found out we already have rpm package for unicode-display_width:
https://github.com/theforeman/foreman-packaging/tree/rpm/develop/rubygem-unicode-display_width

We only need debs.

@mmoll
Copy link
Contributor

mmoll commented Nov 24, 2016

@tstrachota the DEB PR is there and marked ready to merge. :)

@tstrachota
Copy link
Member

@mmoll nice! I completely missed that. Thank you!

@tstrachota
Copy link
Member

tstrachota commented Nov 24, 2016

[test]

@tstrachota
Copy link
Member

The last thing blocking the merge is tests. @shlomizadok could you please rebase? It seems that jenkins fails to apply the patches.

@shlomizadok
Copy link
Member Author

@tstrachota rebased :)

@mmoll
Copy link
Contributor

mmoll commented Nov 28, 2016

💚

@tstrachota tstrachota merged commit 3a24459 into theforeman:master Nov 28, 2016
@tstrachota
Copy link
Member

Merged, thanks @shlomizadok and @mmoll !

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