Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

License

NotificationsYou must be signed in to change notification settings

logrusorgru/aurora

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go.dev referenceUnlicenseBuild StatusCoverage StatusGoReportCard

Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc.

aurora logo

TOC

Installation

Version 1.x

Using gopkg.in.

go get -u gopkg.in/logrusorgru/aurora.v1
Version 2.x
go get -u github.com/logrusorgru/aurora
Go modules support, version v3+

Get

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

The v3 was introduced to supportgo.mod and leave previous import paths as is.Currently, there is no changes between them (excluding the importpath's /v3 tail).

The latest version
go get -u github.com/logrusorgru/aurora/v4

With hyperlinks.

Test

go test -cover -race github.com/logrusorgru/aurora/v4

Replace the import path with your, if it's different.

Usage

Simple

package mainimport ("fmt""github.com/logrusorgru/aurora/v4")funcmain() {fmt.Println("Hello,",aurora.Magenta("Aurora"))fmt.Println(aurora.Bold(aurora.Cyan("Cya!")))}

simple png

Printf

package mainimport ("fmt""github.com/logrusorgru/aurora/v4")funcmain() {fmt.Printf("Got it %d times\n",aurora.Green(1240))fmt.Printf("PI is %+1.2e\n",aurora.Cyan(3.14))}

printf png

aurora.Sprintf

package mainimport ("fmt""github.com/logrusorgru/aurora/v4")funcmain() {fmt.Println(aurora.Sprintf(aurora.Magenta("Got it %d times"),aurora.Green(1240)))}

sprintf png

Enable/Disable colors

package mainimport ("fmt""flag""github.com/logrusorgru/aurora/v4")// colorizervarau*aurora.Auroravarcolors=flag.Bool("colors",false,"enable or disable colors")funcinit() {flag.Parse()au=aurora.New(WithColors(*colors))}funcmain() {// use colorizerfmt.Println(au.Green("Hello"))}

Without flags:disable png

With-colors flag:enable png

Hyperlinks, default colorizer, and configurations

Hyperlinks feature description.

Add a red hyperlinks with text "Example" that is referencing tohttp://example.com.

package mainimport ("flag""fmt""github.com/logrusorgru/aurora/v4")funcmain() {varconf=aurora.NewConfig()conf.AddFlags(flag.CommandLine,"prefix.")flag.Parse()aurora.DefaultColorizer=aurora.New(conf.Options()...)// set globalfmt.Println(aurora.Red("Example").Hyperlink("http://example.com/"))}

Depending flags:depending flags pngdepending flags gif

Chains

The following samples are equal

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

The second is more readable

Colorize

There isColorize function that allows to choose some colors andformat from a side

funcgetColors()Color {// some stuff that returns appropriate colors and format}// [...]funcmain() {fmt.Println(aurora.Colorize("Greeting",getColors()))}

Less complicated example

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

Unlike other color functions and methods (such as Red/BgBlue etc)aColorize clears previous colors

x:=aurora.Red("x").Colorize(BgGreen)// will be with green background only

Grayscale

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

grayscale png

8-bit colors

MethodsIndex andBgIndex implements 8-bit colors.

Index/BgIndexMeaningForegroundBackground
0- 7standard colors30- 3740- 47
8- 15bright colors90- 97100-107
16-231216 colors38;5;n48;5;n
232-25524 grayscale38;5;n48;5;n

Example

package mainimport ("fmt""github.com/logrusorgru/aurora")funcmain() {fori:=uint8(16);i<=231;i++ {fmt.Println(i,aurora.Index(i,"pew-pew"),aurora.BgIndex(i,"pew-pew"))}}

Supported colors & formats

  • formats
    • bold (1)
    • faint (2)
    • doubly-underline (21)
    • fraktur (20)
    • italic (3)
    • underline (4)
    • slow blink (5)
    • rapid blink (6)
    • reverse video (7)
    • conceal (8)
    • crossed out (9)
    • framed (51)
    • encircled (52)
    • overlined (53)
  • background and foreground colors, including bright
    • black
    • red
    • green
    • yellow (brown)
    • blue
    • magenta
    • cyan
    • white
    • 24 grayscale colors
    • 216 8-bit colors

All colors

linux png
white png

Standard and bright colors

linux black standard pnglinux white standard png

Formats are likely supported

formats supported gif

Formats are likely unsupported

formats rarely supported png

Limitations

There is no way to represent%T and%p with colors usinga standard approach

package mainimport ("fmt""github.com/logrusorgru/aurora")funcmain() {var (r=aurora.Red("red")iint)fmt.Printf("%T %p\n",r,aurora.Green(&i))}

Output will be without colors

aurora.value %!p(aurora.value={0xc42000a310 768 0})

The obvious workaround isRed(fmt.Sprintf("%T", some))

Windows

The Aurora provides ANSI colors only, so there is no support for Windows. That said, there are workarounds available.Check out these comments to learn more:

TTY

The Aurora has no internal TTY detectors by design. Take a lookthis comment if you want turnon colors for a terminal only, and turn them off for a file.

Licensing

Copyright © 2016-2022 The Aurora Authors. This work is free.It comes without any warranty, to the extent permitted by applicablelaw. You can redistribute it and/or modify it under the terms of thethe Unlicense. See the LICENSE file for more details.

About

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp