gpt4 book ai didi

ruby - Ruby 中带有 html 标签的 ANSI 转义代码?

转载 作者:数据小太阳 更新时间:2023-10-29 07:27:40 25 4
gpt4 key购买 nike

有趣的是有 built-in ansi escape code in Ruby .

a gem还有一个更强大的版本.

不幸的是,这些日志输出到控制台。我的文本显示在页面中,因此我需要 HTML 标记来环绕我的文本。

你们知道怎么做吗?

最佳答案

我猜您想要的是将转义字符转换为 HTML。

我通过假设转义字符使用以下代码/颜色散列来完成一次:

{ :reset          =>  0,
:bright => 1,
:dark => 2,
:underline => 4,
:blink => 5,
:negative => 7,
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:back_black => 40,
:back_red => 41,
:back_green => 42,
:back_yellow => 43,
:back_blue => 44,
:back_magenta => 45,
:back_cyan => 46,
:back_white => 47}

我所做的是以下转换(远非优化):

def escape_to_html(data)
{ 1 => :nothing,
2 => :nothing,
4 => :nothing,
5 => :nothing,
7 => :nothing,
30 => :black,
31 => :red,
32 => :green,
33 => :yellow,
34 => :blue,
35 => :magenta,
36 => :cyan,
37 => :white,
40 => :nothing,
41 => :nothing,
43 => :nothing,
44 => :nothing,
45 => :nothing,
46 => :nothing,
47 => :nothing,
}.each do |key, value|
if value != :nothing
data.gsub!(/\e\[#{key}m/,"<span style=\"color:#{value}\">")
else
data.gsub!(/\e\[#{key}m/,"<span>")
end
end
data.gsub!(/\e\[0m/,'</span>')
return data
end

好吧,您需要填补我没有考虑的颜色或背景的空白。但我想你能明白这一点。

希望对你有帮助

关于ruby - Ruby 中带有 html 标签的 ANSI 转义代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com