Skip to content

Commit e1989c9

Browse files
committed
🎨🎨🎨color: puts the string colorful in shell use Ruby
1 parent 505ad2b commit e1989c9

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

colorize.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# gem colorized
2+
#encoding: utf-8
3+
require 'colorize'
4+
5+
puts "蓝色文字".colorize(:blue)
6+
puts "浅蓝色文字".colorize(:light_blue)
7+
puts "还是蓝色文字".colorize(:color => :blue)
8+
puts "浅蓝色文字红色背景".colorize(:color => :light_blue, :background => :red)
9+
puts "浅蓝色文字红色背景第二种写法".colorize(:light_blue).colorize(:background => :red)
10+
puts "浅蓝色文字红色背景第三种写法".light_blue.on_red
11+
puts "蓝色文字红色背景带下划线".blue.on_red.underline
12+
13+
puts "所有颜色-"
14+
puts String.colors
15+
16+
puts "所有效果-"
17+
puts String.modes
18+
19+
puts "显示颜色矩阵"
20+
puts String.color_matrix
21+
22+
puts String.color_matrix("FOO")

colorize_make.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def colorize(str, color_code = 31)
2+
"\e[#{color_code}m#{str}\e[0m"
3+
end
4+
5+
def blue(str, color_code = 34)
6+
colorize(str, color_code)
7+
end
8+
9+
def red(str, color_code = 31)
10+
colorize(str, color_code)
11+
end
12+
13+
puts blue("我是蓝色")
14+
puts red("我是红色")

colorize_make_different_color.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class String
2+
3+
def color_black
4+
"\033[30m#{self}\033[0m"
5+
end
6+
7+
def color_red
8+
"\033[31m#{self}\033[0m"
9+
end
10+
11+
def color_green
12+
"\033[32m#{self}\033[0m"
13+
end
14+
15+
def color_brown
16+
"\033[33m#{self}\033[0m"
17+
end
18+
19+
def color_blue
20+
"\033[34m#{self}\033[0m"
21+
end
22+
23+
def color_magenta
24+
"\033[35m#{self}\033[0m"
25+
end
26+
27+
def color_cyan
28+
"\033[36m#{self}\033[0m"
29+
end
30+
31+
def color_gray
32+
"\033[37m#{self}\033[0m"
33+
end
34+
35+
def bg_black
36+
"\033[40m#{self}\033[0m"
37+
end
38+
39+
def bg_red
40+
"\033[41m#{self}\033[0m"
41+
end
42+
43+
def bg_green
44+
"\033[42m#{self}\033[0m"
45+
end
46+
47+
def bg_brown
48+
"\033[43m#{self}\033[0m"
49+
end
50+
51+
def bg_blue
52+
"\033[44m#{self}\033[0m"
53+
end
54+
55+
def bg_magenta
56+
"\033[45m#{self}\033[0m"
57+
end
58+
59+
def bg_cyan
60+
"\033[46m#{self}\033[0m"
61+
end
62+
63+
def bg_gray
64+
"\033[47m#{self}\033[0m"
65+
end
66+
67+
def bold
68+
"\033[1m#{self}\033[0m"
69+
end
70+
71+
def reverse_color
72+
"\033[7m#{self}\033[0m"
73+
end
74+
end
75+
76+
puts "I am back green".bg_green
77+
78+
puts "I am red and back cyan".color_red.bg_cyan
79+
80+
puts "I am bold and green and background red".bold.color_green.bg_red
81+
82+
puts "I am blue".color_blue

0 commit comments

Comments
 (0)