termcolor

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

README

TermColor

termcolor tells you what colors your terminal can display and at what level or depth

It answers questions like

  • Does my terminal supports colors at all?
  • Can it show 256 colors?
  • Can it show truecolor

Features

  • Detects terminal color capability (dumb, 16, 256, truecolor)
  • Automatically parses --color and --no-color flags in both GNU and POSIX styles.
  • Checks if color has being explicitly enabled or disabled via flags or environment variables.
  • Respects NO_COLOR and FORCE_COLOR environment variables.
  • Falls back to auto-detection when no color flags are provided
  • Detects if output is a TTY.
  • Automatically handles when user uses --color=auto or --color=tty or --color tty

Installation

go get -u github.com/phamio/termcolor

Usage

Check color support
package main
import (
    "fmt"
    "github.com/phamio/termcolor"
)

func main(){
     //Check if terminal supports color
    cap , status := termcolor.Capability()
    if status == termcolor.Detected && cap >= termcolor.Color16 {
        fmt.Println("Hurray! Terminal Understands colors")
    } else if status == termcolor.NotTTy {
        fmt.Println("Output is piped(color disabled)")
    } else if status ==termcolor.ExplicitDisabled {
        fmt.Println("Colors explicitly disabled by user")
    } else if status == termcolor.Uncertain {
        fmt.Println("Could not detect - try setting FORCE_COLOR=1")
    }
}

Check capability level
package main
import (
    "fmt"
    "github.com/phamio/termcolor"
)

func main(){
    cap, ok := termcolor.Capability()

    ///Only trust capability if detection was successful
    if !ok {
        fmt.Println("Could not reliably detect color support")
        return
    }

    switch cap {
    case termcolor.ColorTrue:
        //16 million colors
        fmt.Println("Terminal supports true color")
    case termcolor.Color256:
        //256 color palette
        fmt.Println("Terminal supports 256-color palette")
    case termcolor.Color16:
       //Basic ansi colors
        fmt.Println("Terminal supports 16 colors")
    case termcolor.ColorNone:
        //No color, plain output
        fmt.Println("Terminal supports no color (dumb terminal)")
    }
}

License

Apache License 2.0 — see LICENSE file for details.

About

termcolor was originally built as the color detection and color flag parsing foundation for Inkstamp

Documentation

Overview

Package termcolor detects terminal color capabilities for Go applications.

# Overview

termcolor tells you what colors your terminal can display and at what level or depth.
It answers like :
  - Does my terminal support colors at all?
  - Can it show 256 colors?
  - Can it show truecolor?
  - Did the user explicitly enable or disable color?

termcolor was originally built as a color detection and color flag parsing foundation
for Inkstamp (https://github.com/inkstamp/inkstamp)

# Key Features

  - Detects terminal color capability (none, 16, 256, truecolor)
  - Automatically parses --color and --no-color flag in GNU and POSIX styles
  - Respects NO_COLOR and FORCE_COLOR environment variables
  - Handles --color=auto, --color=tty automatically
  - CI environment detection (Github Actions, Travis, Gitlab, CircleCI)

# Quick Start

package main

import (
    "fmt"
    "github.com/phamio/termcolor"
)

func main() {
    cap, ok := termcolor.Capability()
    if ok {
        switch cap {
        case termcolor.ColorTrue:
            fmt.Println("Terminal supports truecolor")
        case termcolor.Color256:
            fmt.Println("Terminal supports 256 colors")
        case termcolor.Color16:
            fmt.Println("Terminal supports 16 colors")
        case termcolor.ColorNone:
            fmt.Println("Terminal supports no color")
        }
    } else {
          fmt.Println("Could not detect color support")
    }
}

# Color Capability Levels

ColorCap represents the color depth supported by the terminal.
Values are the actual color counts for easy comparison:

ColorNone = 0          // No color support (dumb terminal)
Color16   = 16         // Basic ANSI 16 colors
Color256  = 256        // 256 color palette
ColorTrue = 16777216   // Truecolor (16 million colors)

Capabilities can be compared directly:

cap, ok := termcolor.Capability()
if ok && cap >= termColor.Color256 {
    // terminal supports at least 256 colors
}

# Detection Chain

Capability runs through the following checks in priority order,
returning as soon as a conclusive result is found:

  1. FORCE_COLOR environment variable
  2. NO_COLOR environment variable
  3. TTY check — is output a terminal at all
  4. Flag sniffing — --color, --no-color in GNU and POSIX styles
  5. CI systems — Github Actions, Travis, Gitlab,

# Options
Capability accepts options to customize detections behaviour:

// Check stderr instead of stdout
cap, ok := termcolor.Capability(termcolor.Stream(os.Stderr))

// Disable flag sniffing
cap, ok := termcolor.Capability(termcolor.SniffFlagToggle(false),)

// Combined
cap, ok := termcolor.Capability(termcolor.Stream(os.Stderr), termcolor.SniffFlagToggle(false),)

Any bug fixes or issues see => https://github.com/inkstamp/inkstamp

License

Apache License 2.0 — see LICENSE file for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorCap

type ColorCap int
const (
	ColorNone ColorCap = 0
	Color16   ColorCap = 16
	Color256  ColorCap = 256
	ColorTrue ColorCap = 16777216
)

func Capability

func Capability(opts ...Option) ColorCap

type Option

type Option func(*config)

func FlagToggle

func FlagToggle(sniff bool) Option

func Stream

func Stream(s *os.File) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL