本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

Go和Python带颜色打印内容到终端

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


最近写运维工具、脚本比较多,就感觉普通的日志输出太苍白。。难看了。。就总结了一下然后写了个简单的带颜色输出的工具库。

Go 版本

主文件

package zzeutil

import (
	"fmt"
)

type FontColor int8

const (
	//黑色
	Black FontColor = iota
	//红色
	Red FontColor = iota
	//绿色
	Green FontColor = iota
	//黄色
	Yellow FontColor = iota
	//蓝色
	Blue FontColor = iota
	//紫色
	Purple FontColor = iota
	//深绿色
	DarkGreen FontColor = iota
	//白色
	White FontColor = iota
	//白底黑字
	WhiteReverse FontColor = iota
	//深绿底红字
	DarkGreenReverse FontColor = iota
	//紫底绿字
	PurpleReverse FontColor = iota
	//蓝底黄字
	BlueReverse FontColor = iota
	//黄底蓝字
	YellowReverse FontColor = iota
	//绿底紫字
	GreenReverse FontColor = iota
	//红底深绿字
	RedReverse FontColor = iota
	//黑底白字
	BlackReverse FontColor = iota
)

func ColorPrint(fontColor FontColor, format string, params ...interface{}) {
	msg := fmt.Sprintf(format, params...)

	colorFormat1 := "\x1b[%dm%%s\x1b[0m\n"
	colorFormat2 := "\x1b[%d;%dm%%s\x1b[0m\n"
	switch fontColor {
	case Black:
		format = fmt.Sprintf(colorFormat1, 30)
	case Red:
		format = fmt.Sprintf(colorFormat1, 31)
	case Green:
		format = fmt.Sprintf(colorFormat1, 32)
	case Yellow:
		format = fmt.Sprintf(colorFormat1, 33)
	case Blue:
		format = fmt.Sprintf(colorFormat1, 34)
	case Purple:
		format = fmt.Sprintf(colorFormat1, 35)
	case DarkGreen:
		format = fmt.Sprintf(colorFormat1, 36)
	case White:
		format = fmt.Sprintf(colorFormat1, 37)
	case WhiteReverse:
		format = fmt.Sprintf(colorFormat2, 47, 30)
	case DarkGreenReverse:
		format = fmt.Sprintf(colorFormat2, 46, 31)
	case PurpleReverse:
		format = fmt.Sprintf(colorFormat2, 45, 32)
	case BlueReverse:
		format = fmt.Sprintf(colorFormat2, 44, 33)
	case YellowReverse:
		format = fmt.Sprintf(colorFormat2, 43, 34)
	case GreenReverse:
		format = fmt.Sprintf(colorFormat2, 42, 35)
	case RedReverse:
		format = fmt.Sprintf(colorFormat2, 41, 36)
	case BlackReverse:
		format = fmt.Sprintf(colorFormat2, 40, 37)
	}
	fmt.Printf(format, msg)
}

//返回一个创造打印指定颜色内容到终端的闭包
func CreateColorPrinter(color FontColor) func(format string, params ...interface{}) {
	return func(format string, params ...interface{}) {
		ColorPrint(color, format, params...)
	}
}

使用

方法一

直接输出指定颜色的文字:

package main

import "zzeutil"

func main() {
	zzeutil.ColorPrint(zzeutil.Black, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.Red, "Hello zze")
	zzeutil.ColorPrint(zzeutil.Green, "Hello zze")
	zzeutil.ColorPrint(zzeutil.Yellow, "Hello zze")
	zzeutil.ColorPrint(zzeutil.Blue, "Hello zze")
	zzeutil.ColorPrint(zzeutil.Purple, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.DarkGreen, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.White, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.WhiteReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.DarkGreenReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.PurpleReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.BlueReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.YellowReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.GreenReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.RedReverse, "Hello %s", "zze")
	zzeutil.ColorPrint(zzeutil.BlackReverse, "Hello %s", "zze")
}

效果如下:
image.png

方法二

在实际使用中大部分情况我其实仅仅使用其中几个颜色,这时候每次要传入俩参数感觉有点不方便,所以就有了那个闭包函数,使用方法如下:

package main

import "zzeutil"

func main() {
	errorPrint := zzeutil.CreateColorPrinter(zzeutil.Red)
	successPrint := zzeutil.CreateColorPrinter(zzeutil.Green)
	warningPrint := zzeutil.CreateColorPrinter(zzeutil.Yellow)
	debugPrint := zzeutil.CreateColorPrinter(zzeutil.White)

	errorPrint("出现严重错误:%s", "MySQL 被删库了!!!")
	successPrint("上传文件成功~~~")
	warningPrint("内存已使用 86%!!!!")
	debugPrint("无关紧要的信息~~")
}

效果如下:
image.png

Python 版本

如下 Python 版本的使用和上面的 Go 版本使用差不太多,就不多说啦。。

from enum import Enum


class FontColor(Enum):
    '''
    颜色枚举
    '''
    # 黑色
    Black = 1
    # 红色
    Red = 2
    # 绿色
    Green = 3
    # 黄色
    Yellow = 4
    # 蓝色
    Blue = 5
    # 紫色
    Purple = 6
    # 深绿色
    DarkGreen = 7
    # 白色
    White = 8
    # 白底黑字
    WhiteReverse = 9
    # 深绿底红字
    DarkGreenReverse = 10
    # 紫底绿字
    PurpleReverse = 11
    # 蓝底黄字
    BlueReverse = 12
    # 黄底蓝字
    YellowReverse = 13
    # 绿底紫字
    GreenReverse = 14
    # 红底深绿字
    RedReverse = 15
    # 黑底白字
    BlackReverse = 16


def color_print(color, format, *args, **kwargs):
    msg = format.format(*args, **kwargs)
    color_format1 = "\x1b[%dm%%s\x1b[0m\n"
    color_format2 = "\x1b[%d;%dm%%s\x1b[0m\n"

    if color == FontColor.Black:
        format = color_format1 % 30
    elif color == FontColor.Red:
        format = color_format1 % 31
    elif color == FontColor.Green:
        format = color_format1 % 32
    elif color == FontColor.Yellow:
        format = color_format1 % 33
    elif color == FontColor.Blue:
        format = color_format1 % 34
    elif color == FontColor.Purple:
        format = color_format1 % 35
    elif color == FontColor.DarkGreen:
        format = color_format1 % 36
    elif color == FontColor.White:
        format = color_format1 % 37
    elif color == FontColor.WhiteReverse:
        format = color_format2 % (47, 30)
    elif color == FontColor.DarkGreenReverse:
        format = color_format2 % (46, 31)
    elif color == FontColor.PurpleReverse:
        format = color_format2 % (45, 32)
    elif color == FontColor.BlueReverse:
        format = color_format2 % (44, 33)
    elif color == FontColor.YellowReverse:
        format = color_format2 % (43, 34)
    elif color == FontColor.GreenReverse:
        format = color_format2 % (42, 35)
    elif color == FontColor.RedReverse:
        format = color_format2 % (41, 36)
    elif color == FontColor.BlackReverse:
        format = color_format2 % (40, 37)
    print(format % msg)


# 返回一个创造打印指定颜色内容到终端的闭包
def create_color_printer(color):
    def color_printer(format, *args, **kwargs):
        color_print(color,format,*args,**kwargs)

    return color_printer


if __name__ == '__main__':
    error_print = create_color_printer(FontColor.BlackReverse)
    error_print('数据库崩啦!!!!')


如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×