内容简介:今天跟大家推荐一款可以给终端输出上色的工具--aurora。初始化项目
背景介绍
今天跟大家推荐一款可以给终端输出上色的工具--aurora。
极速上手
准备工作
初始化项目
go mod init aurora
演示项目结构
. ├── go.mod ├── go.sum └── main.go
安装aurora包
go get -u github.com/logrusorgru/aurora
代码演示
首先引入aurora库
import . "github.com/logrusorgru/aurora"
输出一个品红颜色的内容,Magenta是以颜色命名的方法
fmt.Println("Hello,", Magenta("Aurora"))
再输出一个加粗的青色的名称
fmt.Println(Bold(Cyan("Cya!")))
完整代码如下
package main import ( "fmt" . "github.com/logrusorgru/aurora" ) func main() { fmt.Println("Hello,", Magenta("Aurora")) fmt.Println(Bold(Cyan("Cya!"))) }
运行后输出内容如下
更多玩法
支持格式化输出函数
除了换行输出函数外,aurora还支持格式化输出函数
msg := fmt.Sprintf("My name is %s", Green("pingyeaa")) fmt.Println(msg)
链式调用
我们可以嵌套调用,来个绿底加粗红字
fmt.Println(BgGreen(Bold(Red("pingyeaa"))))
还可以进行链式调用,同样可以达到相同效果,这种方式的可读性更高一些
fmt.Println(Red("pingyeaa").Bold().BgGreen())
更简便的写法
除了链式调用外,还有一种更简便的写法,就是通过位或运算符来实现
fmt.Println(Colorize("Greeting", GreenFg|RedBg|BoldFm))
官方定义了10种常量,感兴趣的同学可以自行研究源码
const ( BlackBg Color = (iota << shiftBg) | flagBg // 40, 100 RedBg // 41, 101 GreenBg // 42, 102 YellowBg // 43, 103 BlueBg // 44, 104 MagentaBg // 45, 105 CyanBg // 46, 106 WhiteBg // 47, 107 BrightBg Color = ((1 << 3) << shiftBg) | flagBg // -> 100 BrownBg = YellowBg maskBg = (0xff << shiftBg) | flagBg )
同样也可以搭配链式调用使用
fmt.Println(Red("x").Colorize(GreenFg))
支持灰阶
所谓灰阶,是将最亮与最暗之间的亮度变化,区分为若干份
方法中的数字代表灰色深度的数值,支持背景和文字上色
fmt.Println(" ", Gray(1-1, " 00-23 ").BgGray(24-1), Gray(4-1, " 03-19 ").BgGray(20-1), Gray(8-1, " 07-15 ").BgGray(16-1), Gray(12-1, " 11-11 ").BgGray(12-1), Gray(16-1, " 15-07 ").BgGray(8-1), Gray(20-1, " 19-03 ").BgGray(4-1), Gray(24-1, " 23-00 ").BgGray(1-1), )
支持闪烁
fmt.Println(Blink("Blink"))
限制
格式化输出函数中的 %T
与 %p
是没办法上色的
r := Red("red") var i int fmt.Printf("%T %p\n", r, Green(&i))
aurora.value %!p(aurora.value={0xc42000a310 768 0})
但是可以通过在外层嵌套颜色来解决
fmt.Println(Red(fmt.Sprintf("%T %p\n", r, Green(&i))))
Go语言库代码示例,欢迎star
https://github.com/pingyeaa/g...
感谢大家的观看,如果觉得文章对你有所帮助,欢迎关注公众号「平也」,聚焦 Go 语言与技术原理。
以上所述就是小编给大家介绍的《Go语言库系列之aurora》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 编译型语言、解释型语言、静态类型语言、动态类型语言概念与区别
- 计算机语言发展的三个阶段:机器语言、汇编语言与高级语言
- 凹 (“Wa”) 语言:可以嵌入 Go 语言环境的脚本语言
- Rust语言恰巧是一门解决了Go语言所有问题的语言
- 获取系统语言/当前 App支持语言
- 【Go 语言教程】Go 语言简介
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Security Testing Cookbook
Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99
Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!