{"id":15655039,"url":"https://github.com/chriskuehl/pygments-ansi-color","last_synced_at":"2025-10-03T20:54:36.097Z","repository":{"id":25122042,"uuid":"103224393","full_name":"chriskuehl/pygments-ansi-color","owner":"chriskuehl","description":"ANSI color-code highlighting for Pygments","archived":false,"fork":false,"pushed_at":"2025-05-28T04:34:41.000Z","size":125,"stargazers_count":28,"open_issues_count":5,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-28T05:30:01.504Z","etag":null,"topics":["pygments","python","syntax-highlighting"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chriskuehl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2017-09-12T05:01:21.000Z","updated_at":"2025-05-28T04:34:43.000Z","dependencies_parsed_at":"2024-03-27T22:29:11.147Z","dependency_job_id":"975c9f7a-8cff-455f-a59d-3c19bdeec9d5","html_url":"https://github.com/chriskuehl/pygments-ansi-color","commit_stats":{"total_commits":80,"total_committers":7,"mean_commits":"11.428571428571429","dds":0.525,"last_synced_commit":"6a856bd012f6350410ca3d793b9534d5ab7cc57c"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/chriskuehl/pygments-ansi-color","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chriskuehl%2Fpygments-ansi-color","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chriskuehl%2Fpygments-ansi-color/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chriskuehl%2Fpygments-ansi-color/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chriskuehl%2Fpygments-ansi-color/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chriskuehl","download_url":"https://codeload.github.com/chriskuehl/pygments-ansi-color/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chriskuehl%2Fpygments-ansi-color/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266026314,"owners_count":23866033,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["pygments","python","syntax-highlighting"],"created_at":"2024-10-03T12:55:38.723Z","updated_at":"2025-10-03T20:54:36.005Z","avatar_url":"https://github.com/chriskuehl.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"pygments-ansi-color\n-------------------\n\n[![build status](https://github.com/chriskuehl/pygments-ansi-color/actions/workflows/main.yml/badge.svg)](https://github.com/chriskuehl/pygments-ansi-color/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/chriskuehl/pygments-ansi-color/main.svg)](https://results.pre-commit.ci/latest/github/chriskuehl/pygments-ansi-color/main)\n[![PyPI version](https://badge.fury.io/py/pygments-ansi-color.svg)](https://pypi.python.org/pypi/pygments-ansi-color)\n\nAn ANSI color-code highlighting lexer for Pygments.\n\n![](https://i.fluffy.cc/nHPkL3gfBtj5Kt4H3RR51T9TJLh6rtv2.png)\n\n\n### Basic usage\n\n1. Install `pygments-ansi-color`:\n\n   ```shell-session\n   $ pip install pygments-ansi-color\n   ```\n\n2. `pygments-ansi-color` is not magic (yet?), so you need to [choose an existing\n   Pygments style](https://pygments.org/styles/), which will be used as a base\n   for your own style.\n\n   For example, let's choose `pygments.styles.xcode.XcodeStyle`, which looks\n   great to use. And then we will augment this reference style with\n   `pygments-ansi-color`'s color tokens thanks to the `color_tokens` function,\n   to make our final `MyStyle` custom style.\n\n   Here is how the code looks like:\n\n   ```python\n   from pygments_ansi_color import color_tokens\n\n   class MyStyle(pygments.styles.xcode.XcodeStyle):\n       styles = dict(pygments.styles.xcode.XcodeStyle.styles)\n       styles.update(color_tokens())\n   ```\n\n   That's all the custom code you need to integrate with `pygments-ansi-color`.\n\n3. Now you can highlight your content with the dedicated ANSI lexer and your\n   custom style, with the Pygments regular API:\n\n   ```python\n   import pygments\n   import pygments.formatters\n   import pygments.lexers\n\n   lexer = pygments.lexers.get_lexer_by_name('ansi-color')\n   formatter = pygments.formatters.HtmlFormatter(style=MyStyle)\n   print(pygments.highlight('your text', lexer, formatter))\n   ```\n\n### Design\n\nWe had to configure above a custom Pygments style with the appropriate color\ntokens. That's because existing Pygments lexers are built around contextual\ntokens (think `Comment` or `Punctuation`) rather than actual colors.\n\nIn the case of ANSI escape sequences, colors have no context beyond the color\nthemselves; we'd always want a `red` rendered as `red`, regardless of your\nparticular theme.\n\n\n### Custom theme\n\nBy default, `pygments-ansi-color` maps ANSI codes to its own set of colors.\nThey have been carefully crafted for readability, and are [loosely based on the\ncolor scheme used by iTerm2\n](https://github.com/chriskuehl/pygments-ansi-color/pull/27#discussion_r1113790011).\n\nDefault colors are hard-coded by the `pygments_ansi_color.DEFAULT_STYLE`\nconstant as such:\n- ![#000000](https://placehold.co/15/000000/000000) `Black`: `#000000`\n- ![#ef2929](https://placehold.co/15/ef2929/ef2929) `Red`: `#ef2929`\n- ![#8ae234](https://placehold.co/15/8ae234/8ae234) `Green`: `#8ae234`\n- ![#fce94f](https://placehold.co/15/fce94f/fce94f) `Yellow`: `#fce94f`\n- ![#3465a4](https://placehold.co/15/3465a4/3465a4) `Blue`: `#3465a4`\n- ![#c509c5](https://placehold.co/15/c509c5/c509c5) `Magenta`: `#c509c5`\n- ![#34e2e2](https://placehold.co/15/34e2e2/34e2e2) `Cyan`: `#34e2e2`\n- ![#f5f5f5](https://placehold.co/15/f5f5f5/f5f5f5) `White`: `#f5f5f5`\n- ![#676767](https://placehold.co/15/676767/676767) `BrightBlack`: `#676767`\n- ![#ff6d67](https://placehold.co/15/ff6d67/ff6d67) `BrightRed`: `#ff6d67`\n- ![#5ff967](https://placehold.co/15/5ff967/5ff967) `BrightGreen`: `#5ff967`\n- ![#fefb67](https://placehold.co/15/fefb67/fefb67) `BrightYellow`: `#fefb67`\n- ![#6871ff](https://placehold.co/15/6871ff/6871ff) `BrightBlue`: `#6871ff`\n- ![#ff76ff](https://placehold.co/15/ff76ff/ff76ff) `BrightMagenta`: `#ff76ff`\n- ![#5ffdff](https://placehold.co/15/5ffdff/5ffdff) `BrightCyan`: `#5ffdff`\n- ![#feffff](https://placehold.co/15/feffff/feffff) `BrightWhite`: `#feffff`\n\nStill, you may want to use your own colors, to tweak the rendering to your\nbackground color, or to match your own theme.\n\nFor that you can override each color individually, by passing them as\narguments to the `color_tokens` function:\n\n```python\nfrom pygments_ansi_color import color_tokens\n\nclass MyStyle(pygments.styles.xcode.XcodeStyle):\n   styles = dict(pygments.styles.xcode.XcodeStyle.styles)\n   styles.update(color_tokens(\n      fg_colors={'Cyan': '#00ffff', 'BrightCyan': '#00ffff'},\n      bg_colors={'BrightWhite': '#000000'},\n   ))\n```\n\n\n### Used by\n\nYou can see an example [on fluffy][fluffy-example], the project that this lexer\nwas originally developed for.\n\nThe colors are defined as part of your Pygments style and can be changed.\n\n\n### Optional: Enable \"256 color\" support\n\nThis library supports rendering terminal output using [256 color\n(8-bit)][256-color] ANSI color codes. However, because of limitations in\nPygments tokens, this is an opt-in feature which requires patching the\nformatter you're using.\n\nThe reason this requires patching the Pygments formatter is that Pygments does\nnot support multiple tokens on a single piece of text, requiring us to\n\"compose\" a single state (which is a tuple of `(bold enabled, fg color, bg\ncolor)`) into a single token like `Color.Bold.FGColor.BGColor`. We then need to\noutput the styles for this token in the CSS.\n\nIn the default mode where we only support the standard 8 colors (plus 1 for no\ncolor), we need 2 × 9 × 9 - 1 = 161 tokens, which is reasonable to contain in\none CSS file. With 256 colors (plus the standard 8, plus 1 for no color),\nthough, we'd need 2 × 265 × 265 - 1 = 140,449 tokens defined in CSS. This makes\nthe CSS too large to be practical.\n\nTo make 256-color support realistic, we patch Pygments' HTML formatter so that\nit places a class for each part of the state tuple independently. This means\nyou need only 1 + 265 + 265 = 531 CSS classes to support all possibilities.\n\nIf you'd like to enable 256-color support, you'll need to do two things:\n\n1. When calling `color_tokens`, pass `enable_256color=True`:\n\n   ```python\n   styles.update(color_tokens(enable_256color=True))\n   ```\n\n   This change is what causes your CSS to have the appropriate classes in it.\n\n2. When constructing your formatter, use the `ExtendedColorHtmlFormatterMixin`\n   mixin, like this:\n\n   ```python\n   from pygments.formatters import HtmlFormatter\n   from pygments_ansi_color import ExtendedColorHtmlFormatterMixin\n\n   ...\n\n   class MyFormatter(ExtendedColorHtmlFormatterMixin, HtmlFormatter):\n       pass\n\n   ...\n\n   formatter = pygments.formatter.HtmlFormatter(style=MyStyle)\n   ```\n\n   This change is what causes the rendered HTML to have the right class names.\n\nOnce these two changes have been made, you can use pygments-ansi-color as normal.\n\n\n[fluffy-example]: https://i.fluffy.cc/3Gq7Fg86mv3dX30Qx9LHMWcKMqsQLCtd.html\n[256-color]: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchriskuehl%2Fpygments-ansi-color","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchriskuehl%2Fpygments-ansi-color","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchriskuehl%2Fpygments-ansi-color/lists"}