golang终端ANSI彩色输出格式化插件库aurora的使用

Golang终端ANSI彩色输出格式化插件库Aurora的使用

Aurora是一个为Golang提供终极ANSI颜色的库,支持Printf/Sprintf等格式化输出。

安装

版本1.x

使用gopkg.in安装:

go get -u gopkg.in/logrusorgru/aurora.v1

版本2.x

go get -u github.com/logrusorgru/aurora

Go模块支持(v3+)

go get -u github.com/logrusorgru/aurora/v3

最新版本(v4+)

go get -u github.com/logrusorgru/aurora/v4

使用示例

简单使用

package main

import (
	"fmt"
	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Println("Hello,", aurora.Magenta("Aurora"))
	fmt.Println(aurora.Bold(aurora.Cyan("Cya!")))
}

Printf格式化

package main

import (
	"fmt"
	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Printf("Got it %d times\n", aurora.Green(1240))
	fmt.Printf("PI is %+1.2e\n", aurora.Cyan(3.14))
}

aurora.Sprintf

package main

import (
	"fmt"
	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Println(aurora.Sprintf(aurora.Magenta("Got it %d times"), aurora.Green(1240)))
}

启用/禁用颜色

package main

import (
	"fmt"
	"flag"

	"github.com/logrusorgru/aurora/v4"
)

// 颜色器
var au *aurora.Aurora

var colors = flag.Bool("colors", false, "enable or disable colors")

func init() {
	flag.Parse()
	au = aurora.New(WithColors(*colors))
}

func main() {
	// 使用颜色器
	fmt.Println(au.Green("Hello"))
}

超链接、默认颜色器和配置

package main

import (
	"flag"
	"fmt"

	"github.com/logrusorgru/aurora/v4"
)

func main() {
	var conf = aurora.NewConfig()
	conf.AddFlags(flag.CommandLine, "prefix.")
	flag.Parse()

	aurora.DefaultColorizer = aurora.New(conf.Options()...) // 设置全局

	fmt.Println(aurora.Red("Example").Hyperlink("http://example.com/"))
}

链式调用

以下两种方式是等价的:

x := aurora.BgMagenta(aurora.Bold(aurora.Red("x")))
x := aurora.Red("x").Bold().BgMagenta()

第二种方式更易读。

Colorize函数

func getColors() Color {
	// 返回适当的颜色和格式
}

func main() {
	fmt.Println(aurora.Colorize("Greeting", getColors()))
}

更简单的例子:

x := aurora.Colorize("Greeting", GreenFg|GrayBg|BoldFm)

灰度颜色

fmt.Println("  ",
	aurora.Gray(1-1, " 00-23 ").BgGray(24-1),
	aurora.Gray(4-1, " 03-19 ").BgGray(20-1),
	aurora.Gray(8-1, " 07-15 ").BgGray(16-1),
	aurora.Gray(12-1, " 11-11 ").BgGray(12-1),
	aurora.Gray(16-1, " 15-07 ").BgGray(8-1),
	aurora.Gray(20-1, " 19-03 ").BgGray(4-1),
	aurora.Gray(24-1, " 23-00 ").BgGray(1-1),
)

8位颜色

package main

import (
	"fmt"
	"github.com/logrusorgru/aurora"
)

func main() {
	for i := uint8(16); i <= 231; i++ {
		fmt.Println(i, aurora.Index(i, "pew-pew"), aurora.BgIndex(i, "pew-pew"))
	}
}

支持的颜色和格式

格式

  • 粗体 (1)
  • 淡色 (2)
  • 双下划线 (21)
  • 哥特体 (20)
  • 斜体 (3)
  • 下划线 (4)
  • 慢闪烁 (5)
  • 快闪烁 (6)
  • 反色 (7)
  • 隐藏 (8)
  • 删除线 (9)
  • 加框 (51)
  • 加圈 (52)
  • 上划线 (53)

颜色

  • 前景色和背景色,包括亮色
    • 黑色
    • 红色
    • 绿色
    • 黄色(棕色)
    • 蓝色
    • 洋红色
    • 青色
    • 白色
    • 24级灰度
    • 216种8位颜色

限制

Windows

Aurora只提供ANSI颜色,不支持Windows。但有解决方案可用:

  • 使用go-colorable
  • 在Windows 10中使用注册表修改

TTY

Aurora没有内置TTY检测器。如果需要只为终端启用颜色,而为文件禁用颜色,可以参考相关解决方案。

许可

Copyright © 2016-2022 The Aurora Authors. 本作品是免费的。 它在适用法律允许的范围内不提供任何担保。您可以根据Unlicense的条款重新分发和/或修改它。


更多关于golang终端ANSI彩色输出格式化插件库aurora的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang终端ANSI彩色输出格式化插件库aurora的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Aurora - Go语言ANSI彩色终端输出库

Aurora是一个轻量级的Go语言库,用于在终端中输出彩色文本和格式化内容。它支持ANSI颜色代码,可以方便地为终端输出添加颜色、背景色和各种文本样式。

安装

go get github.com/logrusorgru/aurora

基本用法

package main

import (
	"fmt"
	
	"github.com/logrusorgru/aurora"
)

func main() {
	// 创建Aurora实例
	au := aurora.NewAurora(true) // 参数true表示启用颜色,false则禁用

	// 基本颜色输出
	fmt.Println(au.Red("红色文本"))
	fmt.Println(au.Green("绿色文本"))
	fmt.Println(au.Blue("蓝色文本"))
	fmt.Println(au.Yellow("黄色文本"))
	fmt.Println(au.Magenta("品红色文本"))
	fmt.Println(au.Cyan("青色文本"))
	
	// 组合样式
	fmt.Println(au.Bold(au.Red("加粗的红色文本")))
	fmt.Println(au.Inverse(au.Green("反色的绿色文本")))
	
	// 背景色
	fmt.Println(au.BgRed("红色背景"))
	fmt.Println(au.BgGreen("绿色背景"))
	fmt.Println(au.BgBlue("蓝色背景"))
	
	// 混合颜色和样式
	fmt.Println(au.Bold(au.BgYellow(au.Blue("蓝色文字黄色背景加粗"))))
	
	// 格式化输出
	fmt.Printf("%s %s\n", au.Green("成功:"), "操作已完成")
	fmt.Printf("%s: %d\n", au.Red("错误代码"), 404)
	
	// 链式调用
	msg := au.Bold(au.Underline(au.BrightCyan("链式样式调用")))
	fmt.Println(msg)
}

高级特性

1. 颜色强度控制

func main() {
	au := aurora.NewAurora(true)
	
	// 明亮色
	fmt.Println(au.BrightRed("明亮的红色"))
	fmt.Println(au.BrightGreen("明亮的绿色"))
	
	// 暗淡色
	fmt.Println(au.Gray(10, "灰色文本")) // 0-23表示不同深浅的灰色
}

2. 条件性着色

func main() {
	au := aurora.NewAurora(true)
	
	// 根据条件决定是否着色
	colorEnabled := true
	auCond := aurora.NewAurora(colorEnabled)
	
	fmt.Println(auCond.Red("根据条件显示颜色"))
}

3. 自定义颜色

func main() {
	au := aurora.NewAurora(true)
	
	// 使用ANSI颜色代码
	customColor := aurora.Index(45, "自定义颜色") // 45是ANSI颜色代码
	fmt.Println(customColor)
	
	// RGB颜色 (部分终端支持)
	rgbColor := aurora.RGB(255, 0, 128, "RGB颜色")
	fmt.Println(rgbColor)
}

4. 表格输出示例

func main() {
	au := aurora.NewAurora(true)
	
	headers := []interface{}{
		au.Bold(au.Cyan("ID")),
		au.Bold(au.Cyan("名称")),
		au.Bold(au.Cyan("状态")),
	}
	
	data := [][]interface{}{
		{1, "项目A", au.Green("正常")},
		{2, "项目B", au.Yellow("警告")},
		{3, "项目C", au.Red("错误")},
	}
	
	// 打印表头
	for _, h := range headers {
		fmt.Printf("%-10s", h)
	}
	fmt.Println()
	
	// 打印数据行
	for _, row := range data {
		for _, col := range row {
			fmt.Printf("%-10v", col)
		}
		fmt.Println()
	}
}

注意事项

  1. 不是所有终端都支持完整的ANSI颜色代码,特别是Windows的cmd终端支持有限
  2. 在使用前最好检查终端是否支持颜色输出
  3. 对于生产环境,建议提供禁用颜色的选项
  4. 过多的颜色可能会降低可读性,应适度使用

替代方案

如果你需要更复杂的终端UI功能,可以考虑以下库:

  • termui - 终端仪表盘
  • tview - 终端应用框架
  • chalk - 类似的颜色库

Aurora以其简单易用和轻量级的特点,非常适合只需要基本颜色输出的场景。

回到顶部