Go language library series of the aurora

Background

Today one can recommend to the output terminal of coloring tools --aurora with you.

file

Speed ​​to get started

Ready to work

Initialize the project

go mod init aurora

Demonstration project structure

.
├── go.mod
├── go.sum
└── main.go

Aurora installation package

go get -u github.com/logrusorgru/aurora

Code demonstrates

First introduced aurora library

import . "github.com/logrusorgru/aurora"

The content output a magenta color, Magenta is the color naming method

fmt.Println("Hello,", Magenta("Aurora"))

And then output a bold blue name

fmt.Println(Bold(Cyan("Cya!")))

The complete code is as follows

package main

import (
	"fmt"

	. "github.com/logrusorgru/aurora"
)

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

After running output follows

file

More Play

Support formatted output function

In addition to the outer wrap output functions, aurora supports formatted output function

msg := fmt.Sprintf("My name is %s", Green("pingyeaa"))
fmt.Println(msg)

file

Chained calls

We can nest calls to a bold red green background

fmt.Println(BgGreen(Bold(Red("pingyeaa"))))

file

Can also be chained calls can also achieve the same effect, higher readability of this approach

fmt.Println(Red("pingyeaa").Bold().BgGreen())

Easier wording

In addition to chain calls, there is an easier wording is achieved by bitwise OR operator

fmt.Println(Colorize("Greeting", GreenFg|RedBg|BoldFm))

The official definition of 10 kinds of constants, interested students can study the source code itself

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
)

The same can also be chained calls with the use of

fmt.Println(Red("x").Colorize(GreenFg))

Grayscale support

The so-called gray scale, is the change in luminance between the lightest and the darkest areas divided into several parts

file

Digital methods of gray represents the depth value, support for background and text color

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),
)

file

Support blinking

fmt.Println(Blink("Blink"))

limit

Formatted output functions in %Tand %pis no way colored

r := Red("red")
var i int
fmt.Printf("%T %p\n", r, Green(&i))
aurora.value %!p(aurora.value={0xc42000a310 768 0})

However, the color can be solved by nesting in the outer layer

fmt.Println(Red(fmt.Sprintf("%T %p\n", r, Green(&i))))

Go language library sample code, welcomed the Star
https://github.com/pingyeaa/golang-examples

Thank you for watching, if that article to help you, welcome attention to the public number "level is also" focusing Go language and technical principles.
follow me

Guess you like

Origin www.cnblogs.com/pingyeaa/p/12637271.html