Skip to content

Navigation Menu

Sign in
Appearance settings

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

Appearance settings

yanun0323/colorize

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Colorize

A lightweight Go library for terminal text colorization using ANSI escape codes.

Features

  • Simple and intuitive API
  • Support for multiple color schemes (basic, bright, background colors)
  • Memory efficient with sync.Pool buffer management
  • High-performance operations with zero external dependencies
  • Compatible with any io.Writer interface

Installation

go get github.com/yanun0323/colorize

Quick Start

package main

import (
    "fmt"
    "strings"

    "github.com/yanun0323/colorize"
)

func main() {
    // Basic string colorization
    fmt.Println(colorize.String(colorize.ColorRed, "Error message"))
    fmt.Println(colorize.Sprint(colorize.ColorGreen, "Success:", " operation completed"))
    fmt.Println(colorize.Sprintf(colorize.ColorBlue, "User %s logged in", "john"))

    // Write to buffer
    var buf strings.Builder
    colorize.Fprint(&buf, colorize.ColorYellow, "Warning:", " low disk space")
    colorize.Fprintf(&buf, colorize.ColorCyan, " Available: %d%%", 15)
    fmt.Print(buf.String())

    // Remove ANSI codes
    coloredText := colorize.String(colorize.ColorRed, "Error")
    plainText := colorize.Reset(coloredText)
    fmt.Println(plainText) // Output: "Error" (without colors)
}

Performance Tip

For maximum performance, you can build the colored output by appending to a pre-allocated byte slice:

buf := make([]byte, 0, 4<<10) // allocate enough size
buf = append(buf, colorize.ColorRed...)
buf = append(buf, "Hello, World"...)
buf = append(buf, colorize.ColorReset...)

API

Core Functions

  • String(c Color, str ...string) string - Colorize multiple strings
  • Sprint(c Color, args ...any) string - Colorize with fmt.Sprint behavior
  • Sprintf(c Color, format string, args ...any) string - Colorize with formatting
  • Fprint(w io.Writer, c Color, args ...any) (int, error) - Write colorized text to writer
  • Fprintf(w io.Writer, c Color, format string, args ...any) (int, error) - Write formatted colorized text
  • Reset(s string) string - Remove ANSI escape codes from string

Available Colors

colorize

Benchmarks

Benchmarks (darwin/arm64, Apple M2).

BenchmarkColorizeString-8       39335316                30.01 ns/op           24 B/op          1 allocs/op
BenchmarkColorizeReset-8        25729447                45.86 ns/op           16 B/op          1 allocs/op
BenchmarkColorizeSprint-8       14059197                80.54 ns/op          136 B/op          3 allocs/op
BenchmarkColorizeSprintf-8      14723767                81.97 ns/op          136 B/op          3 allocs/op
BenchmarkColorizeFprint-8       36809578                33.07 ns/op            0 B/op          0 allocs/op
BenchmarkColorizeFprintf-8      34705401                35.12 ns/op            0 B/op          0 allocs/op
BenchmarkColorizeAppend-8      634096282                 1.89 ns/op            0 B/op          0 allocs/op

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A simple, lightweight Go library for terminal text colorization using ANSI escape codes.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.