https://github.com/droqsic/glint
High-performance, zero-allocation terminal color detection for Go. Thread-safe and cross-platform with advanced caching for optimal performance.
https://github.com/droqsic/glint
cli color colors console cross-platform cygwin go golang msys2 terminal thread-safe tty zero-allocation
Last synced: 9 months ago
JSON representation
High-performance, zero-allocation terminal color detection for Go. Thread-safe and cross-platform with advanced caching for optimal performance.
- Host: GitHub
- URL: https://github.com/droqsic/glint
- Owner: droqsic
- License: other
- Created: 2025-05-13T08:37:49.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-05-23T07:45:59.000Z (11 months ago)
- Last Synced: 2025-05-23T09:05:47.473Z (11 months ago)
- Topics: cli, color, colors, console, cross-platform, cygwin, go, golang, msys2, terminal, thread-safe, tty, zero-allocation
- Language: Go
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: docs/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: docs/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Glint
[](https://pkg.go.dev/github.com/droqsic/glint)
[](https://opensource.org/licenses/MIT)
[](https://github.com/droqsic/glint/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/droqsic/glint)
[](https://github.com/droqsic/glint/releases)
[](https://golang.org/)
**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
```bash
go get github.com/droqsic/glint
```
## Quick Start
```go
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.
- Read the [Contributing Guide](docs/CONTRIBUTING.md) to get started
- Please follow the [Code of Conduct](docs/CODE_OF_CONDUCT.md)
## License
Glint is released under the MIT License. For the full license text, please see the [LICENSE](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](https://github.com/droqsic/probe) for the terminal detection library
- All contributors who help make Glint better