开发者

Having trouble interperating ansi color codes

I have spent the weekend working on a personal project and got stuck here. Basically, I need to turn

[0;37m[33m o0==============================~o[0]o~==============================0o

into

o0==============================~o[0]o~==============================0o (only this text would be yellow now)

Using cocoa's regex functionality, I was able to find and capture the "[0;", "37m" and "[33m" individually. the "0;" indicates the server's desire for any previous text styling to be removed and returned to the default which is black background and white text. The "37m" indicates that the server would like for text to be colored white (not sure why this is here, but this is what the server sends). The final "33m" indicates that the server wants the text to be colored yellow. My code correctly finds, strips out, and identifies the requested color changes in the string, but I am having trouble applying these colors to the NSAttributedString I create. The ranges supplied by the regex searches are no longer valid once I strip out the color sequences in the final string, what is an effective way to figure out where the color changes should be applied to the stripped string? In this example, all the color codes are supplied at th开发者_运维问答e beginning, but in other cases, the color codes could be in the middle to cause the string to change color mid-line. NSAttributedString can handle this if I could figure out the proper ranges to assign the requested colors to.


Now that Lion is out I can post the answer. Basically you can use the fancy regex abilities in Lion to figure out what is up. The code to do this (which needs to be refactored, but at least it works) can be found here:

https://github.com/sgoodwin/Turbo-Mud/blob/experiment/Turbo%20Mud/Turbo_MudAppDelegate.m

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜