{"id":26649047,"url":"https://github.com/mandelsoft/ttycolors","last_synced_at":"2026-05-14T20:31:03.383Z","repository":{"id":284131008,"uuid":"952651686","full_name":"mandelsoft/ttycolors","owner":"mandelsoft","description":"Color Support for ANSI Terminals","archived":false,"fork":false,"pushed_at":"2025-04-08T15:01:32.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-08T10:57:27.776Z","etag":null,"topics":["color","colors","console","golang","golang-library","golang-package","terminal","tty"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mandelsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-03-21T16:32:33.000Z","updated_at":"2025-04-08T15:01:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"180f3c81-56d7-431f-b243-1b2685fdb489","html_url":"https://github.com/mandelsoft/ttycolors","commit_stats":null,"previous_names":["mandelsoft/ttycolors"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mandelsoft/ttycolors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fttycolors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fttycolors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fttycolors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fttycolors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mandelsoft","download_url":"https://codeload.github.com/mandelsoft/ttycolors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fttycolors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33042060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-13T13:14:54.681Z","status":"online","status_checked_at":"2026-05-14T02:00:06.663Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["color","colors","console","golang","golang-library","golang-package","terminal","tty"],"created_at":"2025-03-25T00:47:50.999Z","updated_at":"2026-05-14T20:31:03.365Z","avatar_url":"https://github.com/mandelsoft.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Color Output for ANSI Terminals\n\nThis Go package provides support for colorized output\nin terms of [ANSI Escape Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors).\n\nThe library consists of three basic APIs:\n- [composition of output formats](#output-format-management)\n- [enable colored output on `io.Writer` and `os.StdOut`](#managing-colorized-output).\n- [compose colored strings](#managing-colorized-strings)\n\nIt supports the following ANSI output formats:\n- output modes:\n  - Bold\n  - Italic\n  - Underline\n  - Blinking\n  - Reverse\n  - StrikeThrough\n- ANSI standard colors like Blue and Green, etc.\n- ANSI color ids\n- ANSI RGB color settings\n- Foreground and Background Colors\n- Bright Colors\n\n\nThe APIs support cascaded output format management.\nForeground and background colors as well as the output modes can be enabled and disabled independently.\nIt is possible to insert format statements into surrounding ones.\n\nThe main package `github.com/mandelsoft/ttycolors`\ncontains the management of native and composed \noutput formats, the operation to colorize\nan output stream according to those formats and\nthe composition of colored strings supporting cascaded format handling.\n\nThe package `github.com/mandelsoft/ttycolors/ansi`\ndefines constants for the various ANSI escape codes.\n\n## Output Format Management\n\nThe package `github.com/mandelsoft/ttycolors`\nprovides constants for all native ANSI output formats:\n- output modes, for example `FmtBold`.\n- standard colors for foreground, background and bright colors, for example `FmtBlue`, `FmtBgBlue`, `FmtBrightBlue` or `FmtBgBrightBlue`.\n- color ids using `FmtIdColor(id)` and `FmtBgIdColor(id)`\n- RGB colors using `RGBColorId(r,g,b)` and `FmtRGBColorId(r,g,b)`\n\nAll those constants provide the composition method `String(...)` usable to compose colored strings (type String).\n\nWith the function `New(fmt...)` new\nformats can be composed based on any other format, for example `New(FmtBGWhite, FmtRed, FmtBold)`.\n\n### Examples\n\nPredefined standard ANSI formats can be composed to new formats:\n\n```golang\nRedOnWhite := ttycolors.New(ttycolors.FmtBgWhite, ttycolors.FmtRed)\n```\n\nThose composed formats as well as the standard formats can be used to\ncreate [colorized strings](#managing-colorized-strings)\n\n```golang\nstr := RedOnWhite.String(\"red on white\")\n```\nor to directly control an [output format](#managing-colorized-output):\n\n```golang\nttycolors.Set(RedOnWhite, ttycolors.FmtBold)\n```\n\n## Managing colorized Output\n\nThe package `github.com/mandelsoft/ttycolors`\nprovides operations to apply output formats \nto `io.Writer` streams.\n- `Set(fmt...) error` applies the format set to the standard output (`os.StdOut`)\n- `SetFor(w, fmt...)` applies the format set to a dedicated writer (which should write to an ANSI terminal)\n- UnSet(fmt...) error reverts the described settings for the standard output again. Output modes, foreground and background colors are handled separately.\n- UnSetFor(w, fmt...) error reverts the described settings again for the given writer.\n\nWith `NewFormatter(fmt...)` a formatter for the given format is created.\nIt provides the usual methods for printing:\n- Print\n- Printf\n- Sprint\n- Sprint\n- Fprint\n- Fprintf\n\n\n### Examples\n\n```golang\nttycolors.Set(ttycolors.FmtBgWhite, ttycolors.FmtRed)\nfmt.Printf(\"red on white\")\nttycolors.Set(ttycolors.FmtBlue)\nfmt.Printf(\"blue on white\")\nttycolors.UnSet(ttycolors.FmtBgWhite)\nfmt.Printf(\"blue\")\nttycolors.UnSet(ttycolors.FmtBlue)\nfmt.Printf(\"\\n\")\nfmt.Printf(\"back to normal\\n\")\n```\n\nThe formatter can be used to apply a format.\n\n```golang\nf := ttycolors.NewFormatter(ttycolors.FmtBlue, ttycolors.FmtBold)\nstr := f.Sprint(\"an \", ttycolors.Italic(\"italic\"), \" string.\")\n# This is basically identical to\nstr = ttycolors.New(ttycolors.FmtBlue, ttycolors.FmtBold).String(\"an \", ttycolors.Italic(\"italic\"), \" string.\").String()\n```\n\nAs can be seen, the two APIs for colorized output are combinable\n\n## Managing colorized Strings\n\nThe package `github.com/mandelsoft/ttycolors`\nlets you compose colored strings.\nThe base type for such a string is `String`.\nIt can be rendered to a Go string using the `String() string` method.\n\nThe package provides functions for all\nANSI output formats providing an appropriate formatted `String` object.\n\n- output modes, for example `Bold`.\n- standard colors for foreground, background and bright colors, for example `Blue`, `BgBlue`, `BrightBlue` or `BgBrightBlue`.\n- color ids using `IdColor(id)` and `BgIdColor(id)`\n- RGB colors using `RGBColor(r,g,b)` and `BgRGBColor(r,g,b)`\n\nForeground and Background color settings as wells as the output modes are handled separately. This way the Strings added\nto a formatting functions can locally use own\nmodes or colors independently of the surrounding settings.\n\nWith the method `Sequence(text...)` a sequence of Strings can be created.\nAll composition functions accept an arbitrary list of Golang strings or other\n`String` objects. All other types will be converted to a string using `fmt.Sprintf(\"%v\")`.\n\n### Examples\n\n```golang\nstr := ttycolors.Blue(\"this blue text contains a \", ttycolors.Italic(ttycolors.Red(\"red\"), \" italic\"), \" word\")\n```\n\nThis expression provides a `ttycolors.String` object. In between, there is an italic part with a red word. All the rest is still blue.\n\nA formal colorized string can be used as argument to all composition functions.\nFor example, with\n```golang\nstr=str.Append(\".\")\n```\nit can be extended with a sequence of one or more strings.\n\nTo render such an object to a Golang string, the `String()` method can be called:\n\n```golang\ns:=str.String()\n```\n\nIf colors are enabled, it contains the required ANSI escape sequences,\notherwise, the native text is returned.\n\nBecause of this method `String` objects can be directly passed to a\nfunction of the `fmt.Printf` family.\n\nFor example,\n\n```golang\nfmt.Printf(\"%s\\n\", str)\n```\n\noutputs the color string to the standard output.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"examples/strings/demo.gif\" alt=\"Cascaded Colorized String\" title=\"Cascaded Colorized String\" /\u003e\n\u003c/p\u003e\n\n\n\nA `String` object can be used to enable or disable coloring\nby using the `Enable(b...)` method. The default coloring\nis defined by the `NoColors` variable.\n\nThe state of top level `String` object determines the color enablement, regardless what sublevel objects define.\n\n## The Output Context\n\nThe package `github.com/mandelsoft/ttycolors` offers plain functions\nor format constants for the string composition. They are based\non the variable `NoColors`, which can be used to globally enable or\ndisable the color handling. It is defaulted to `false` if\n`os.StdOut` represents a terminal device.\n\nAdditionally, there is a `TTYContext` object offering the same functions as methods.\nIt can be enabled or disabled, also. The offered methods provide\nobjects reflecting this colorizing state.\nIf nothing selected during the context creation, it directly reflects\nthe state of the `NoColors` variable.\n\n### Examples\n\n```golang\n\nctx := ttycolors.NewContext(true) // provide a context always enabled\n\ns := ctx.Blue(\"a blue text with some \", ctx.Italic(\"italic\"), \" word\")\n```\n\nThe crucial element here is the first call. It decided, whether coloring\nis enabled ot not. Subsequent elements derive this information.\nTherefore, it could have been written as follows, also:\n\n```golang\ns := ctx.Blue(\"a blue text with some \", ttycolors.Italic(\"italic\"), \" word\")\n```\n\nor\n\n```golang\ns := ctx.String(ttycolors.Blue(\"a blue text with some \", ttycolors.Italic(\"italic\"), \" word\"))\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmandelsoft%2Fttycolors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmandelsoft%2Fttycolors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmandelsoft%2Fttycolors/lists"}