{"id":5566,"library":"ansiwrap","title":"ansiwrap","description":"ansiwrap provides text wrapping capabilities similar to Python's standard `textwrap` module, but it is specifically designed to correctly handle strings that contain ANSI control sequences for colors and styles. It correctly calculates the effective length of a string by ignoring these non-printable control codes. The latest release is version 0.8.4, published in January 2019, indicating a maintenance-focused or slow release cadence.","status":"maintenance","version":"0.8.4","language":"en","source_language":"en","source_url":"https://github.com/jonathaneunice/ansiwrap","tags":["text processing","ansi","colors","textwrap","terminal"],"install":[{"cmd":"pip install ansiwrap","lang":"bash","label":"Install ansiwrap"}],"dependencies":[{"reason":"Used in examples to generate ANSI colored strings that ansiwrap can process. Not a strict runtime dependency for ansiwrap itself, but essential for using its core feature with colored text.","package":"ansicolors","optional":true}],"imports":[{"note":"Primary function for wrapping text.","symbol":"fill","correct":"from ansiwrap import fill"},{"note":"Primary function for truncating text.","symbol":"shorten","correct":"from ansiwrap import shorten"},{"note":"Calculates the printable length of a string, ignoring ANSI codes.","symbol":"ansilen","correct":"from ansiwrap import ansilen"},{"note":"Removes all ANSI escape codes from a string.","symbol":"strip_color","correct":"from ansiwrap import strip_color"}],"quickstart":{"code":"from ansiwrap import fill, shorten, ansilen, strip_color\nfrom colors import red, blue, green, color # Requires 'ansicolors' package\n\ns = ' '.join([\n    red('This string'),\n    blue('is going on a bit long'),\n    green('and may need to be'),\n    color('shortened a bit', fg='purple')\n])\n\nprint('-- Original string --')\nprint(s)\nprint(f'Original length (Python): {len(s)}')\nprint(f'Printable length (ansiwrap): {ansilen(s)}')\n\nprint('\\n-- Now filled to 20 columns --')\nprint(fill(s, 20))\n\nprint('\\n-- Now shortened / truncated to 20 columns --')\nprint(shorten(s, 20, placeholder='...'))\n\nprint('\\n-- Strip colors --')\nprint(strip_color(s))\nprint(f'Stripped length: {len(strip_color(s))}')","lang":"python","description":"This example demonstrates how to use `ansiwrap` to fill and shorten a string containing ANSI color codes, calculate its effective display length, and strip the colors. It uses the `ansicolors` library to generate the colored text."},"warnings":[{"fix":"As of April 2026, there is an unmerged pull request on GitHub to address Python 3.12 compatibility. Users may need to apply a patch manually or await an official release. Consider using alternative libraries if Python 3.12+ compatibility is critical.","message":"ansiwrap version 0.8.4 is not directly compatible with Python 3.12 due to the removal of the `imp` module, leading to `ModuleNotFoundError` during import.","severity":"breaking","affected_versions":"0.8.4 on Python 3.12+"},{"fix":"Be aware of the project's maintenance status. For critical applications, thorough testing against your Python environment is recommended, and consider contributing fixes or exploring more actively maintained alternatives if long-term support is a concern.","message":"The library has not had a new release since January 2019. This suggests it is in maintenance mode or no longer actively developed, which might lead to limited support for newer Python features, potential unaddressed bugs, or future compatibility issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `ansicolors` (or your preferred ANSI styling library) is installed if you intend to use `ansiwrap` with colored text, e.g., `pip install ansicolors`.","message":"The example code often relies on an external library like `ansicolors` (or `colors`) to generate ANSI-colored strings. This dependency is not automatically installed with `ansiwrap` and must be installed separately for the quickstart examples to run as shown.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}