go-ansi-parser icon indicating copy to clipboard operation
go-ansi-parser copied to clipboard

Hyperlink support

Open diamondburned opened this issue 1 year ago • 4 comments

Right now, go-ansi-parser's ansi.Length includes the URL part of a terminal hyperlink. This makes the line count super long even when it's visibly just a small piece of text.

ansi.Length should ideally only return the visible text component of a hyperlink.

Example:

For string:

"\x1b[33;1mSource Code: \x1b[0m\x1b]8;;https://libdb.so/libdb.so\x1b\\diamondburned/libdb.so\x1b]8;;\x1b\\"

ansi.Length(str) should return 35 (probably maybe?), but it currently returns 74.

diamondburned avatar Mar 19 '23 23:03 diamondburned

The current workaround is to have a regex:

var ansiLinkRe = regexp.MustCompile(`(?m)\x1b]8;;([^\x1b]*)\x1b\\([^\x1b]*)\x1b]8;;\x1b\\`)

Then do line = ansiLinkRe.ReplaceAllString(line, "$2"). This replaces the links away, and ansi.Length() yields the correct number for the string (35).

diamondburned avatar Mar 19 '23 23:03 diamondburned

Apologies for the delay in getting back to you. Do you have more information about "terminal links" as I'm not aware of them? My instinct is that as a library, it shouldn't try to derive any semantic meaning from the input, but I'm keen to understand more about this use case.

leaanthony avatar Jun 09 '23 00:06 leaanthony

I can't find any standard page that I can link, but searching "OSC 8 Terminal" should give a bunch of results on hyperlink support in various terminals.

diamondburned avatar Jun 09 '23 02:06 diamondburned

This looks great. Happy to accept a PR for this if you're willing to do it 👍

leaanthony avatar Jun 09 '23 08:06 leaanthony