无法解释 ansi 颜色代码

发布于 2024-11-10 16:21:10 字数 642 浏览 0 评论 0原文

我整个周末都在做一个个人项目,但被困在这里了。基本上,我需要转动

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

变为

o0================== =============~o[0]o~================================ 0o(现在只有这个文本是黄色的)

使用cocoa的正则表达式功能,我能够分别找到并捕获“[0;”,“37m”和“[33m”。 “0;”指示服务器希望删除任何先前的文本样式并返回到默认值,即黑色背景和白色文本。 “37m”表示服务器希望文本颜色为白色(不确定为什么会出现在这里,但这就是服务器发送的内容)。最后的“33m”表示服务器希望文本颜色为黄色。我的代码正确地查找、删除并识别字符串中请求的颜色更改,但我在将这些颜色应用到我创建的 NSAttributedString 时遇到问题。一旦我删除了最终字符串中的颜色序列,正则表达式搜索提供的范围就不再有效,有什么有效方法可以确定颜色更改应应用于删除的字符串的位置?在此示例中,所有颜色代码都在开头提供,但在其他情况下,颜色代码可能位于中间,导致字符串在中线更改颜色。如果我能找出分配请求颜色的正确范围,NSAttributedString 就可以处理这个问题。

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 the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2024-11-17 16:21:10

现在 Lion 已经出来了,我可以发布答案了。基本上,您可以使用 Lion 中奇特的正则表达式功能来弄清楚发生了什么。可以在这里找到执行此操作的代码(需要重构,但至少可以工作):

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文