Skip to content

๐Ÿ–๏ธ A lightweight Python parser library for ANSI escape code sequences

License

Notifications You must be signed in to change notification settings

getcuia/stransi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

232 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

PyPI Python package PyPI - License

stransi ๐Ÿ–๏ธ

stransi

I see a \033[1;31mred\033[;39m door, and I want it painted \033[1;30mblack\033[;39m.

stransi is a lightweight parser for ANSI escape code sequences. It implements a string-like type that is aware of its own ANSI escape sequences, and can be used to parse most of the common escape sequences used in terminal output manipulation.

Features

  • โœจ Good support of ANSI escape sequences
  • ๐ŸŽจ Focus on coloring and styling
  • ๐Ÿ›ก๏ธ Unsupported CSI escape sequences are emitted as tokens
  • ๐Ÿœ๏ธ Only one dependency: ochre
  • ๐Ÿ Python 3.8+

Installation

$ pip install stransi

Usage

In [1]: from stransi import Ansi

In [2]: text = Ansi(
   ...:     "I see a \033[1;31mred\033[;39m door, "
   ...:     "and I want it painted \033[1;30mblack\033[;39m"
   ...: )

In [3]: list(text.escapes())
Out[3]:
['I see a ',
 Escape('\x1b[1;31m'),
 'red',
 Escape('\x1b[;39m'),
 ' door, and I want it painted ',
 Escape('\x1b[1;30m'),
 'black',
 Escape('\x1b[;39m')]

In [4]: list(text.instructions())
Out[4]:
['I see a ',
 SetAttribute(attribute=<Attribute.BOLD: 1>),
 SetColor(role=<ColorRole.FOREGROUND: 30>, color=Ansi256(1)),
 'red',
 SetAttribute(attribute=<Attribute.NORMAL: 0>),
 SetColor(role=<ColorRole.FOREGROUND: 30>, color=None),
 ' door, and I want it painted ',
 SetAttribute(attribute=<Attribute.BOLD: 1>),
 SetColor(role=<ColorRole.FOREGROUND: 30>, color=Ansi256(0)),
 'black',
 SetAttribute(attribute=<Attribute.NORMAL: 0>),
 SetColor(role=<ColorRole.FOREGROUND: 30>, color=None)]

Credits

Photo by Tien Vu Ngoc on Unsplash.

About

๐Ÿ–๏ธ A lightweight Python parser library for ANSI escape code sequences

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages