Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assistance in writing a Regex to remove Rainbow information #1

Closed
scottrhoyt opened this issue Mar 3, 2016 · 3 comments
Closed

Assistance in writing a Regex to remove Rainbow information #1

scottrhoyt opened this issue Mar 3, 2016 · 3 comments

Comments

@scottrhoyt
Copy link

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!

@onevcat
Copy link
Owner

onevcat commented Mar 6, 2016

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.

@scottrhoyt
Copy link
Author

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.

@onevcat
Copy link
Owner

onevcat commented Mar 14, 2016

@scottrhoyt Cool!

Good luck.

@onevcat onevcat closed this as completed Mar 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants