fatih/color là một trong những repo phía máy chủ mà TopGit theo dõi, hiện có 8.0k sao, viết chủ yếu bằng Go. Color package for Go (golang)
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Color lets you use colorized outputs in terms of ANSI Escape
Codes in Go (Golang). It
has support for Windows too! The API can be used in several ways, pick one that
suits you.
Install
go get github.com/fatih/color
Examples
Standard colors
// Print with default helper functions
color.Cyan("Prints text in cyan.")
// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")
// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")
RGB colors
If your terminal supports 24-bit colors, you can use RGB color codes.
// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")
// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")
// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)
boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")
whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")
// Mix with RGB color codes
color.RGB(255, 128, 0).AddBgRGB(0, 0, 0).Println("orange with black background")
color.BgRGB(255, 128, 0).AddRGB(255, 255, 255).Println("orange background with white foreground")
Use your own output (io.Writer)
// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")
Custom print functions (PrintFunc)
// Create a custom print function for convenience
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)
// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")
Custom fprint functions (FprintFunc)
blue := color.New(color.FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)
// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, "Don't forget this...")
Insert into noncolor strings (SprintFunc)
// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))
// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
Plug into existing code
// Use handy standard colors
color.Set(color.FgYellow)
fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")
color.Unset() // Don't forget to unset
// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your function
fmt.Println("All text will now be bold magenta.")
Disable/Enable color
There might be a case where you want to explicitly disable/enable color output. the
go-isatty package will automatically disable color output for non-tty output streams
(for example if the output were piped directly to less).
The color package also disables color output if the NO_COLOR environment
variable is set to a non-empty string.
Color has support to disable/enable colors programmatically both globally and
for single color definitions. For example suppose you have a CLI app and a
-no-color bool flag. You can easily disable the color output with:
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
if *flagNoColor {
color.NoColor = true // disables colorized output
}
It also has support for single color definitions (local). You can
disable/enable color output on the fly:
c := color.New(color.FgCyan)
c.Println("Prints cyan text")
c.DisableColor()
c.Println("This is printed without any color")
c.EnableColor()
c.Println("This prints again cyan...")
GitHub Actions
To output color in GitHub Actions (or other CI systems that support ANSI colors), make sure to set color.NoColor = false so that it bypasses the check for non-tty output streams.
Credits
Fatih Arslan
Windows support via @mattn: colorable
License
The MIT License (MIT) - see LICENSE.md for more details
fatih/color có 8.0k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/fatih/color. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
fatih/color có những chủ đề gì?
GitHub topics của fatih/color: "ansi", "color", "coloring", "go", "golang". TopGit xếp repo vào nhóm Backend.
fatih/color còn đang phát triển không?
Commit gần nhất trên fatih/color là 3 tháng trước (theo timestamp GitHub). Repo có 648 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
fatih/color là gì?
fatih/color (fatih/color) là dự án Go trên GitHub. Theo mô tả gốc: Color package for Go (golang)
fatih/color so với các dự án Backend khác thế nào?
fatih/color được TopGit xếp vào nhóm Backend, với 8.0k sao GitHub và viết bằng Go. Xem trang chủ đề Backend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
fatih/color viết bằng ngôn ngữ gì?
fatih/color chủ yếu viết bằng Go. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao fatih/color được xếp vào nhóm Backend?
TopGit xếp fatih/color vào nhóm Backend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "ansi", "color", "coloring"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc color có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về color.