Skip to content
/ glint Public

High-performance, zero-allocation terminal color detection for Go. Thread-safe and cross-platform with advanced caching for optimal performance.

License

droqsic/glint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Glint

Go Reference License: MIT Go Workflow Go Report Card Latest Release Go Version

Glint is a lightweight, cross-platform Go library for detecting and enabling terminal color support. It combines performance, accuracy, and simplicity — with benchmarks showing it's up to 1000x faster than alternatives.

Features

  • High Performance: Advanced caching makes repeated checks nearly instantaneous
  • 🌐 Cross-Platform: Works on Windows, macOS, Linux, BSD, and more
  • 🧠 Zero Allocations: Efficient design ensures no heap allocations
  • 🔒 Thread-Safe: Safe for concurrent use from multiple goroutines
  • 🧼 Simple API: Easy to use and integrate
  • 📦 Minimal Dependencies: Only depends on the Go standard library, x/sys, and probe

Installation

go get github.com/droqsic/glint

Quick Start

package main

import (
	"fmt"

	"github.com/droqsic/glint"
)

func main() {
	// Check if terminal supports colors
	fmt.Println("Terminal supports colors:", glint.ColorSupport())

	// Get color support level
	fmt.Println("Color support level:", glint.ColorLevel())

	// Force color support
	glint.ForceColor(true)

    // Reset color support
	glint.ResetColor()
}

How It Works

Glint determines terminal color support through:

  • 🧾 Environment Variables: Inspects TERM, COLORTERM, NO_COLOR
  • 🧪 Terminal Detection: Uses the probe library to check if output is a terminal
  • 🪟 Windows Support: Enables virtual terminal sequences when necessary
  • 🌈 Color Levels: Distinguishes between None, 16, 256, and True Color

All results are cached to ensure ultra-fast subsequent checks.

Performance

Glint is engineered for speed. Here's what benchmarks reveal:

BenchmarkIsColorSupported-12                1000000000	         0.25 ns/op
BenchmarkIsColorSupportedLevel-12           1000000000	         0.89 ns/op
BenchmarkForceColorSupport-12               42567984	        30.15 ns/op
  • 🔁 Cached Checks: Almost all operations complete in under 1 ns
  • 🪄 Zero Allocations: No memory allocations for any operation
  • 💯 High Throughput: Ideal for performance-critical CLI tools

Color Support Levels

Glint can detect four levels of color support:

Level Colors Example Terminals
LevelNone 0 Non-interactive shells, logs
Level16 16 ANSI colors xterm, vt100, screen
Level256 256 colors xterm-256color, screen-256color
LevelTrue 16M true colors Terminals with COLORTERM=truecolor or 24bit

Thread Safety

Glint is built with concurrency in mind. It uses synchronization mechanisms such as sync.Once and sync.RWMutex to manage its internal cache, allowing multiple goroutines to access color support checks safely and efficiently. The design is optimized for read-heavy workloads, ensuring high throughput and low latency even under concurrent access. This makes Glint well-suited for use in modern, parallelized Go applications.

Contributing

We welcome contributions of all kinds! Bug fixes, new features, test improvements, and docs are all appreciated.

License

Glint is released under the MIT License. For the full license text, please see the LICENSE file.

Acknowledgements

This project is inspired by the need for high-performance terminal color detection in real-world Go applications. Special thanks to:

  • The Go team for their exceptional language and tooling
  • The maintainers of x/sys for low-level system access
  • The creators of Probe for the terminal detection library
  • All contributors who help make Glint better

About

High-performance, zero-allocation terminal color detection for Go. Thread-safe and cross-platform with advanced caching for optimal performance.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

Languages