From ccb140d1d19b2602c0160064fff350af4b055727 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Mon, 27 Jul 2020 22:44:26 +0800 Subject: [PATCH] feat : added textcolor --- tools/textcolor.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tools/textcolor.go diff --git a/tools/textcolor.go b/tools/textcolor.go new file mode 100644 index 00000000..d5862c4a --- /dev/null +++ b/tools/textcolor.go @@ -0,0 +1,72 @@ +package tools + +import ( + "fmt" +) + +// 前景 背景 颜色 +// --------------------------------------- +// 30 40 黑色 +// 31 41 红色 +// 32 42 绿色 +// 33 43 黄色 +// 34 44 蓝色 +// 35 45 紫红色 +// 36 46 青蓝色 +// 37 47 白色 +// +// 代码 意义 +// ------------------------- +// 0 终端默认设置 +// 1 高亮显示 +// 4 使用下划线 +// 5 闪烁 +// 7 反白显示 +// 8 不可见 + +const ( + TextBlack = iota + 30 + TextRed + TextGreen + TextYellow + TextBlue + TextMagenta + TextCyan + TextWhite +) + +func Black(msg string) string { + return SetColor(msg, 0, 0, TextBlack) +} + +func Red(msg string) string { + return SetColor(msg, 0, 0, TextRed) +} + +func Green(msg string) string { + return SetColor(msg, 0, 0, TextGreen) +} + +func Yellow(msg string) string { + return SetColor(msg, 0, 0, TextYellow) +} + +func Blue(msg string) string { + return SetColor(msg, 0, 0, TextBlue) +} + +func Magenta(msg string) string { + return SetColor(msg, 0, 0, TextMagenta) +} + +func Cyan(msg string) string { + return SetColor(msg, 0, 0, TextCyan) +} + +func White(msg string) string { + return SetColor(msg, 0, 0, TextWhite) +} + +func SetColor(msg string, conf, bg, text int) string { + return fmt.Sprintf("%c[%d;%d;%dm%s%c[0m", 0x1B, conf, bg, text, msg, 0x1B) +}