|
发表于 2017-2-5 15:13:12
|
显示全部楼层
本帖最后由 hypnos 于 2017-2-5 03:17 PM 编辑
不要问……自己当年写的自己已经看不懂了……虽然本来也是low到爆
- # coding: utf-8
- class Ansi
- attr_accessor :Maps, :ESC
- # Class variable
- @@ESC = "\e"
-
- @@Maps = {
- # @@ESC = "\e",
- #define @@ESC ""
- #define CSI @@ESC + "["
- # Foregroud
-
- @@ESC + "[30m" => "BLK", # Black
- @@ESC + "[31m" => "RED", # Red
- @@ESC + "[32m" => "GRN", # Green
- @@ESC + "[33m" => "YEL", # Yellow
- @@ESC + "[34m" => "BLU", # Blue
- @@ESC + "[0;34m" => "BLU", # Blue(pkuxkx)
- @@ESC + "[35m" => "MAG", # Magenta
- @@ESC + "[36m" => "CYN", # Cyan
- @@ESC + "[0;36m" => "CYN", # Cyan(pkuxkx)
- @@ESC + "[37m" => "WHT", # White
- @@ESC + "[37;0m" => "WHT", # White(pkuxkx)
- @@ESC + "[0;37m" => "WHT", # White(pkuxkx)
-
- # Hi Intensity Foreground Colors
- @@ESC + "[1;30m" => "灰", # 灰色?(pkuxkx)
- @@ESC + "[1;31m" => "HIR", # Red
- @@ESC + "[0;1;31m" => "HIR", # Red(pkuxkx)
- @@ESC + "[1;32m" => "HIG", # Green
- @@ESC + "[1;33m" => "HIY", # Yellow
- @@ESC + "[1;34m" => "HIB", # Blue
- @@ESC + "[1;35m" => "HIM", # Magenta
- @@ESC + "[1;36m" => "HIC", # Cyan
- @@ESC + "[1;37m" => "HIW", # White
- # High Intensity Background Colors
- @@ESC + "[41;1m" => "HBRED", # Red
- @@ESC + "[42;1m" => "HBGRN", # Green
- @@ESC + "[43;1m" => "HBYEL", # Yellow
- @@ESC + "[44;1m" => "HBBLU", # Blue
- @@ESC + "[45;1m" => "HBBLU", # Magenta
- @@ESC + "[46;1m" => "HBCYN", # Cyan
- @@ESC + "[47;1m" => "HBWHT", # White
-
- # Background Colors
-
- @@ESC + "[40m" => "BBLK", # Black
- @@ESC + "[41m" => "BRED", # Red
- @@ESC + "[42m" => "BGRN", # Green
- @@ESC + "[43m" => "BYEL", # Yellow
- @@ESC + "[44m" => "BBLU", # Blue
- @@ESC + "[45m" => "BMAG", # Magenta
- @@ESC + "[46m" => "BCYN", # Cyan
- # @@ESC + "[47m" => "BWHT", # White
- @@ESC + "[2;37;0m" => "NOR", # Puts everything back to normal
- @@ESC + "[0m" => "",
- @@ESC + "[1m" => "HI",
- @@ESC + "[1A" + @@ESC + "[200D" + @@ESC + "[K" => "DES",
- @@ESC + "[1;5m" => "BNK", # pkuxkx
- @@ESC + "[7m" => "REV", #pkuxkx
- @@ESC + "[4m" => "U", #pkuxkx
- }
-
- # Class method
- def Ansi.colorprocess(str, colorful)
- if colorful then
- @@Maps.each { |k, v|
- str.gsub!(k, "$" +v.to_s + "$")
- }
- else
- @@Maps.each {|k,v|
- str.gsub!(k, "")
- }
- end
- return str
- end
- end
- =begin
- #颜色处理测试
- ansi = Ansi.new
- a = "\r\e[2;37;0m\e[1;37m【江湖】\e[36m杭州城\e[36m中有\e[37m毛头小贼\e[2;37;0m\e[1;37m\e[36m横行,据说此贼带有价值\e[1;33m尚可\e[2;37;0m\e[1;37m\e[36m的财宝!\e[2;37;0m\r\n"
- a = " \e[44;1m∷∷\e[37;0m\r\n \e[44;1m∷∷\e[37;0m 个素未谋面,可借此头的?我喜欢偶尔在这个世界里出现, \e[44;1m∷∷\e[37;0m\r\n \e[44;1m∷∷\e[37;0m 因为在这里,我们也许欢笑一堂,也许睚眦必报,但是我们 \e[44;1m∷∷\e[37;0m\r\n \e[44;1m∷∷\e[37;0m 都没有那么虚伪,我们活得不累。娱乐存在的意义,不在于 \e[44;1m∷∷\e[37;0m\r\n \e[44;1m∷∷\e[37;0m 它给我欢乐,而在于它给了我空间,尽管这空间是虚拟的, \e[44;1m∷"
- print a
- puts Ansi.colorprocess(a, true)
- a = "\r\e[2;37;0m\e[1;37m【江湖】\e[36m杭州城\e[36m中有\e[37m毛头小贼\e[2;37;0m\e[1;37m\e[36m横行,据说此贼带有价值\e[1;33m尚可\e[2;37;0m\e[1;37m\e[36m的财宝!\e[2;37;0m\r\n"
- print a
- puts Ansi.colorprocess(a, false)
- =end
复制代码 |
|