mattbrictson/airbrussh

Truncate :auto line breaks when using UTF-8 Ellipsis

brand-it opened this issue · 5 comments

The truncate auto does not seem to take into account that you are adding on extra ellipsis characters to the length of the string. I think this is only a UTF-8 Safe string issue but here is the code I created to create the issue. Also this issue comes up in the capistrano gem when deploying however that is a bit harder to document so I recreted the issue based off the code located here.

class Test
  require "io/console"
  def self.run
    console_width = IO.console.winsize.last
    ellipsis = '…' # UTF-8 Support only
    string_large_2 = (console_width + 2).times.map { 's' }.join
    string_large_1 = (console_width + 1).times.map { 's' }.join
    string_control = console_width.times.map { 's' }.join
    string_small_1 = (console_width - 1).times.map { 's' }.join
    string_small_2 = (console_width - 2).times.map { 's' }.join

    console_width -= ellipsis.length # remove the ellipsis from the width

    string_large_2.chop! while string_large_2.length > console_width
    string_large_1.chop! while string_large_1.length > console_width
    string_control.chop! while string_control.length > console_width
    string_small_1.chop! while string_small_1.length > console_width
    string_small_2.chop! while string_small_2.length > console_width


    puts '-------------- Results -------------------'
    puts 'string_large_2'
    puts string_large_2 + ellipsis
    puts 'string_large_1'
    puts string_large_1 + ellipsis
    puts 'string_control'
    puts string_control + ellipsis
    puts 'string_small_1'
    puts string_small_1 + ellipsis
    puts 'string_small_2'
    puts string_small_2 + ellipsis
    puts '-------------- End Results ---------------'
  end
end

screen shot 2016-07-21 at 1 12 57 pm

If you run this in irb it should result in the breaking to the new line in all the cases except string_small_2, which is under the width of the console.

The solution seam to be that by subtracting one from the lenght on this line https://github.com/mattbrictson/airbrussh/blob/master/lib/airbrussh/console.rb#L46 it will correclty display issues.

Hmm, I don't see that problem when running your test code on my terminal (Mac):

screen shot 2016-07-21 at 11 28 45 am

Do you think it is something specific to your terminal that it does something weird with the UTF-8 ellipsis? I'm wondering what Airbrussh can do in this case. Subtracting 1 seems like a brute force fix. Perhaps better would be to detect the terminal and apply the workaround only if needed?

Oh interesting so this error only happens in iTerm and not in Terminal. This is only happening on iTerm.
screen shot 2016-07-21 at 1 51 53 pm

I can't find any font difference between the two that I am using or really anything that would change the result but yet here it is.

Found the problem it was a setting under text settings.

screen shot 2016-07-21 at 1 58 48 pm

don't know why that was on but that is the reason for the length issues in iTerm