博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang输出带颜色的信息 linux and windows
阅读量:3987 次
发布时间:2019-05-24

本文共 3647 字,大约阅读时间需要 12 分钟。

在输出颜色字体这块,linux 和 windows 有所不同

linux

颜色及模式编号

// 前景 背景 颜色// ---------------------------------------// 30  40  黑色// 31  41  红色// 32  42  绿色// 33  43  黄色// 34  44  蓝色// 35  45  紫红色// 36  46  青蓝色// 37  47  白色//// 模式代码 意义// -------------------------//  0  终端默认设置//  1  高亮显示//  4  使用下划线//  5  闪烁//  7  反白显示//  8  不可见

输出模板

// 其中0x1B是标记,[开始定义颜色,依次为:模式,背景色,前景色,0代表恢复默认颜色。func (c ColorOutput) Println(str interface{
}) {
fmt.Println(fmt.Sprintf("%c[%d;%d;%dm%s%c[0m", 0x1B, c.mode, c.backColor, c.frontColor, str, 0x1B))}
windows

在 cmd 下查看颜色编号

windows cmd下查看颜色编号: color /?Sets the default console foreground and background colors.COLOR [attr]  attr        Specifies color attribute of console outputColor attributes are specified by TWO hex digits -- the firstcorresponds to the background; the second the foreground.  Each digitcan be any of the following values:    0 = Black       8 = Gray    1 = Blue        9 = Light Blue    2 = Green       A = Light Green    3 = Aqua        B = Light Aqua    4 = Red         C = Light Red    5 = Purple      D = Light Purple    6 = Yellow      E = Light Yellow    7 = White       F = Bright WhiteIf no argument is given, this command restores the color to what it waswhen CMD.EXE started.  This value either comes from the current consolewindow, the /T command line switch or from the DefaultColor registryvalue.The COLOR command sets ERRORLEVEL to 1 if an attempt is made to executethe COLOR command with a foreground and background color that are thesame.Example: "COLOR fc" produces light red on bright white

设置字体颜色及背景色并输出

func SetCmdPrint(s interface{
}, i int) {
proc := kernel32.NewProc("SetConsoleTextAttribute") handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(i)) fmt.Println(s) handle, _, _ = proc.Call(uintptr(syscall.Stdout), uintptr(7)) CloseHandle := kernel32.NewProc("CloseHandle") CloseHandle.Call(handle)}

查看当前操作系统类型

// darwin, windows, linuxif runtime.GOOS == "windows" {
......} else {
......}

想法是通过判断操作系统类型来执行对应的输出方法,在 windows 下可以正常运行,但是在 linux 下编译都通不过,报错

./ColorOutput.go:139:15: undefined: syscall.LazyDLL./ColorOutput.go:187:13: undefined: syscall.NewLazyDLL

的确在Linux系统下 syscall.NewLazyDLL不存在。

那么现在的问题就是,如何告诉golang编译器基于是否是windows平台来选择性的编译或不编译某个文件呢?

实际上可以做到这一点,只需要在文件头部加上注解:

只在 windows 系统下编译此文件,则需要在文件头部加上// +build windows在非 windows 系统下编译此文件,则需要在文件头部加上// +build !windows

类似于这样:test.go

// +build windowspachage main....

解决了这个问题之后,整合代码,又发现一个问题,如果不编译 ColorOutput_windows.go 文件,那么在这个文件中定义的方法又会报不存在了,但实际上也不会被调用,于是我们定义方法类型,然后在 ColorOutput_windows.go 文件中来实现即可。到此这个功能已经初步完成。

包地址:github.com/phprao/ColorOutput

使用

ColorOutput.Colorful.WithFrontColor("green").WithBackColor("red").Println("ColorOutput test...")

windows 系统

在这里插入图片描述
linux 系统
在这里插入图片描述

补充知识:

1、关于 SetConsoleTextAttribute

通过调用windows操作系统API设置终端文本属性,包括:前景色,背景色,高亮。可同时设置多个属性,使用竖线 | 隔开。

DOC: https://docs.microsoft.com/zh-cn/windows/console/setconsoletextattribute

Usage: https://docs.microsoft.com/zh-cn/windows/console/using-the-high-level-input-and-output-functions
属性值: https://docs.microsoft.com/zh-cn/windows/console/console-screen-buffers#character-attributes

SetConsoleTextAttribute函数用于设置显示后续写入文本的颜色。在退出之前,程序会还原原始控制台输入模式和颜色属性。但是微软官方建议使用“虚拟终端”来实现终端控制,而且是跨平台的。

建议使用基于windows提供的“虚拟终端序列”来实现兼容多平台的终端控制,比如:https://github.com/gookit/color

https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences
https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences#samples

2、关于如何设置前景色和背景色

背景色 | 前景色

注意,简单的或操作是错误的,比如 4 | 2,实际是 6 即 黄色,和预期的红底绿字不一致。
应该构成1个8位的二进制,前四位是背景色,后四位是前景色,因此背景色需要左移4位。

当然这些都已经封装在ColorOutput里面了,不需要操心。

最后推荐一个很好的颜色包:https://github.com/gookit/color,使用的虚拟终端序列来实现的,功能强大,兼容性好。

转载地址:http://jcaui.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]168. Excel Sheet Column Title
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]171. Excel Sheet Column Number
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
Mac删除文件&文件夹
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
《Fluent Python》第三章Dictionaries and Sets
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
面试---刷牛客算法题
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>