Assistance in writing a Regex to remove Rainbow information
scottrhoyt opened this issue · comments
First of all, thank you for putting together this library. It's great work!
I share your motivation that Swift will become incredibly useful for command line programs. This is the reason that I started SwiftyTextTable to better organize complex console output.
I think our libraries are a natural pair, and I would like to make them work seamlessly together. Unfortunately, right now the escape sequences that Rainbow uses to inject color and style information are not compatible with my (rather naive) method of calculating string lengths to determine column widths. This is being tracked in this issue: scottrhoyt/SwiftyTextTable#5.
It would be my preference to not introduce a dependency on Rainbow to keep the library as lightweight as possible. Therefore I was wondering if you could assist me in designing an algorithm to strip Rainbow styles and colors from a string for length calculations. From my light reading of the extraction methods already present in Rainbow, it looks like I can detect the presence of a formatted string, and if necessary, apply a regex to extract the text information out of the style information, but I would like to hear your thoughts on implementation. Thanks!
Hi, @scottrhoyt
Thanks for contacting and sorry for the late reply.
Yes, I guess you could have a regex for it. All attributed string in terminal would be started with the CSI control code \u{001B}[
. Then followed by some mode code in the pattern of some digits and an "m". The attribute string would be terminated with u{001B}[0m
.
Although I am not sure whether it is a complete set or not (I guess there are some other cases to display the same string), Rainbow
is using the rule above and works fine. You can find the created string in the test files (RainbowTests.swift
) for a reference as well.
Thanks @onevcat ! That's gives me most of what I need to know. Unless you have any better ideas than using a regex to pull out the text, feel free to close this.
@scottrhoyt Cool!
Good luck.