Skip to content

feat!: rewrite color engine with terminal detection and downsampling - #3

Draft
MarvinJWendt wants to merge 2 commits into
mainfrom
feat/new-pterm
Draft

MarvinJWendt wants to merge 2 commits into
mainfrom
feat/new-pterm

Conversation

@MarvinJWendt

Copy link
Copy Markdown
Member

Requested by Marvin 路 project thread

Before: color printed escape codes whatever the terminal could show. A pipe or a file got raw codes, an RGB color on a 16-color terminal came out wrong, and the package shipped its own Success/Error styles.

After: every print checks the writer it goes to. Piped output is plain, RGB colors are converted to the closest color a 256 or 16 color terminal can show, and the package only does color and text attributes. PTerm v0.13 builds on it.

How: the engine is PTerm's internal color code moved here. Detect(w) reads FORCE_COLOR, CLICOLOR_FORCE, NO_COLOR, CLICOLOR, checks whether w is a terminal, then reads COLORTERM, TERM and TERM_PROGRAM, and on Windows turns on ANSI processing in the console. Background(timeout) reads COLORFGBG or sends an OSC 11 query followed by DA1, so terminals that do not answer OSC 11 return at once instead of waiting for the timeout.

API

  • Color: one comparable value type. Red ... BrightWhite, Index(n), RGB(r, g, b), Hex(s), MustHex(s), Parse(s) (hex, names, 0-255, "none"). Methods RGB, Hex, String, Mix, Luminance, IsDark, Fg, Bg, Sprint.
  • Style{Foreground, Background, Attrs} with New(attrs...), Fg, Bg, Add, Bold and friends, Sequence(level), Render(level, text), Sprint/Fprint/Print variants. Render handles nesting and styles each line separately.
  • Level: LevelNone, LevelBasic, Level256, LevelTrueColor. Detect(w), DetectEnv(), IsTerminal(w), SetLevel(l), ResetLevel().
  • Strip, Hyperlink, SGR, Background.

Breaking changes

  • Mode, ANSIColor, ANSI256Color, RGBColor, the Color interface, NoColor, NewColorFromRGB, NewColorFromHex, NewStyle, Modifier and color.Writer are gone. Use Color, Index, RGB, Hex, None, Style and Attr.
  • The predefined styles (Success, Error, Green(...) and so on) are gone.
  • Go 1.27 is required. New dependency: golang.org/x/term.

Tests: go test -race ./... passes, golangci-lint reports 0 issues for linux and windows.

馃 Generated with Claude Code

https://claude.ai/code/session_011Dno7CsMx2fSzNx5ZTm7RL


Generated by Claude Code

Replaces the Mode/Style API with a single Color value type, a Style value
with attributes, per-writer level detection (TTY, FORCE_COLOR, NO_COLOR,
COLORTERM, TERM, Windows VT), automatic downsampling to 256 and 16 colors,
background color detection (COLORFGBG and OSC 11), Strip and Hyperlink.
Drops the predefined styles. Requires Go 1.27.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011Dno7CsMx2fSzNx5ZTm7RL
@MarvinJWendt MarvinJWendt self-assigned this Oct 10, 2026
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Welcome to Codecov 馃帀

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered 鈽傦笍

The v2.11.4 release binary is built with Go 1.26 and refuses to lint a
module that targets Go 1.27.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011Dno7CsMx2fSzNx5ZTm7RL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants