tty-box icon indicating copy to clipboard operation
tty-box copied to clipboard

Not compatible with TTY::Link

Open oleander opened this issue 4 years ago • 1 comments
trafficstars

Great gem! I'm just having some small issues with TTY::Link:

require "tty/box"
require "tty/link"

link = TTY::Link.link_to("Link", "http://example.com")

box1 = TTY::Box.frame(title: { top_left: link }) do
  "<content>"
end

box2 = TTY::Box.frame(title: { top_left: "No link" }) do
  "<content>"
end

puts box1
puts box2

puts TTY::Box::VERSION
puts TTY::Link::VERSION

image

oleander avatar May 25 '21 14:05 oleander

Hi Linus 👋

Thanks for using tty-box and tty-link. Also thanks for reporting this issue.

The tty-link emits ANSI codes which are incorrectly taken into account when calculating total width, hence the 'abnormal' box size. The same would happen if you tried using a link inside the box content.

Under the covers the tty-box relies on strings and in particular strings-ansi gem to handle a box content formatting. To handle links width calculation, the strings-ansi needs to know how to remove link codes. I merged PR recently which adds support for this.

I still need to finish working on the strings-ansi gem. Unfortunately releasing it won't fix the issue as I'd need to address it in the tty-box gem next. Currently, I'm refactoring internals of tty-box to turn it into a class. The API won't change. However, it will be more flexible so you can retrieve information about box's content size etc.

box = TTY::Box.new("<content>")
box.content_width # width without any ANSI codes
# etc...

This is all to say that this won't be a 'quick fix. The strings-ansi powers a few tty components so I need to tread carefully. This comes under 'do my best' timeline.

piotrmurach avatar May 25 '21 20:05 piotrmurach