Open markpitchless opened 5 years ago
Digging into this I realised it was the wrapping code that got tripped up, it doesn't know how to wrap such a string. Here: https://github.com/piotrmurach/tty-table/blob/master/lib/tty/table/operation/wrapped.rb#L37 The newline appears in the middle :) Took some digging to get there!
Working around with monkey patch in my app:
module Operation
class Wrapped
def call(field, row, col)
column_width = widths[col] || field.width
@pastel ||= Pastel.new
if @pastel.colored? field.content
field.content
else
Strings.wrap(field.content, column_width)
end
end
end
end
Which just ignores wrapping for colourful strings.
Will work up a proper patch for you, using this https://github.com/alyssais/table/blob/master/lib/ansi/string_view.rb which heroically solves the wrapping coloured text problem I avoided. Hello @alyssais đź‘‹
Hey!
Fwiw, it wouldn’t be possible to include that class of mine in tty-prompt as it stands, because of its licensing (it’s GPLv3). If you want to use it, I’d have to think about whether I want to relicense.
(If you just want to take inspiration from it and basically use it as documentation on ANSI escape handling, but write a new thing that just solves the problem here, that’s a moot point.)
@markpitchless Thank you for pointing out the issue! This is very helpful. As far as I'm concerned I would be keen for the bug to be addressed directly in the strings dependency and in particular the wrap
call. If you have time to code up a patch I would be most appreciative due to my limited time.
@alyssais Hello! As the author/maintainer of tty related gems I can assure you that I don't have the intention of using your gem in any of the tty gems and will not be reading or copying the source from your table gem, I'm already very happy with the strings
gem that I've created quite few years back to handle text transformations that include ANSI strings. This is the gem that is at fault in this case and I would rather fix it than seek alternative solutions. If @markpitchless wants to use your solution in his own project to tacle the problem of handling ANSI codes then that's cool but I won't be using it in tty-table
. I hope this clarifies things and solves any contentions that may arise.
Oh neat, I hadn’t seen the strings gem. That’s awesome!
Spotted an odd bug trying to render multicoloured strings in a cell, tty-tables looks to be getting confused by the end and start of colour codes generated by
pastel
. Can see it most clearly in :unicode render but it also fails with :basic render.The middle one shows the problem.
multiline: true
seems to cause tty-table to splithello\e[0m\e[30;41mworld
with a newline after[30;
but (the 3rd image) it is ok withhello\e[0m \e[30;41mworld
(space between coloured bits).