Giter VIP home page Giter VIP logo

stransi's Introduction

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.

stransi's People

Contributors

dependabot[bot] avatar schneiderfelipe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kj7rrv

stransi's Issues

Incremental parsing?

It would be nice if I could create some sort of instance of a parser that would incrementally parse input with ANSI escape sequences as I feed it (from an I/O stream)? This means that it would have to allow a possibility of an unfinished escape sequence being fed to the parser and the parser would not decode it until another feed call feeds it in full. Terminal output is continuous so for long-running programs, it's not necessarily easy to figure out when it's safe to parse the text and you sometimes may want to modify and then propagate this output further, without much delay.

I believe that the built-in codecs.IncrementalDecoder is one example of such parsing/decoding in case this helps.

Fails on neofetch output

This is my code:

import subprocess
import stransi

neofetch_bytes = subprocess.run(['neofetch'], capture_output=True).stdout
neofetch_str = neofetch_bytes.decode('utf-8')
neofetch_ansi = stransi.Ansi(neofetch_str)

print('neofetch_bytes:')
print(neofetch_bytes)

print('----------')

print('neofetch_str:')
print(neofetch_str)

print('----------')

print('neofetch_ansi:')
print(neofetch_ansi)

print('----------')

print('neofetch_ansi.instructions():')
print(list(neofetch_ansi.instructions()))

This is the output (to see it as it appears in my terminal, including colors, run curl https://termbin.com/abyk):

neofetch_bytes:
b'\x1b[?25l\x1b[?7l\x1b[0m\x1b[36m\x1b[1m                   -`\n                  .o+`\n                 `ooo/\n                `+oooo:\n               `+oooooo:\n               -+oooooo+:\n             `/:-:++oooo+:\n            `/++++/+++++++:\n           `/++++++++++++++:\n          `/+++o\x1b[0m\x1b[36m\x1b[1moooooooo\x1b[0m\x1b[36m\x1b[1moooo/`\n\x1b[0m\x1b[36m\x1b[1m         \x1b[0m\x1b[36m\x1b[1m./\x1b[0m\x1b[36m\x1b[1mooosssso++osssssso\x1b[0m\x1b[36m\x1b[1m+`\n\x1b[0m\x1b[36m\x1b[1m        .oossssso-````/ossssss+`\n       -osssssso.      :ssssssso.\n      :osssssss/        osssso+++.\n     /ossssssss/        +ssssooo/-\n   `/ossssso+/:-        -:/+osssso+-\n  `+sso+:-`                 `.-/+oso:\n `++:.                           `-/+/\n .`                                 `/\x1b[0m\n\x1b[19A\x1b[9999999D\x1b[41C\x1b[0m\x1b[1m\x1b[36m\x1b[1mkj7rrv\x1b[0m@\x1b[36m\x1b[1marchlinux\x1b[0m \n\x1b[41C\x1b[0m----------------\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mOS\x1b[0m\x1b[0m:\x1b[0m Arch Linux x86_64\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mHost\x1b[0m\x1b[0m:\x1b[0m Inspiron 3521 A05\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mKernel\x1b[0m\x1b[0m:\x1b[0m 5.18.7-zen1-1-zen\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mUptime\x1b[0m\x1b[0m:\x1b[0m 23 hours, 10 mins\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mPackages\x1b[0m\x1b[0m:\x1b[0m 1412 (pacman)\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mShell\x1b[0m\x1b[0m:\x1b[0m zsh 5.9\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mResolution\x1b[0m\x1b[0m:\x1b[0m 1366x768\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mDE\x1b[0m\x1b[0m:\x1b[0m GNOME 42.3.1\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mWM\x1b[0m\x1b[0m:\x1b[0m Mutter\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mWM Theme\x1b[0m\x1b[0m:\x1b[0m Adwaita\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mTheme\x1b[0m\x1b[0m:\x1b[0m Adwaita [GTK2/3]\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mIcons\x1b[0m\x1b[0m:\x1b[0m breeze [GTK2/3]\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mTerminal\x1b[0m\x1b[0m:\x1b[0m mate-terminal\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mTerminal Font\x1b[0m\x1b[0m:\x1b[0m Monospace 10\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mCPU\x1b[0m\x1b[0m:\x1b[0m Intel Pentium 2117U (2) @ 1.800GHz\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mGPU\x1b[0m\x1b[0m:\x1b[0m Intel 3rd Gen Core processor Graphics Controller\x1b[0m \n\x1b[41C\x1b[0m\x1b[36m\x1b[1mMemory\x1b[0m\x1b[0m:\x1b[0m 2737MiB / 3797MiB\x1b[0m \n\n\x1b[41C\x1b[30m\x1b[40m   \x1b[31m\x1b[41m   \x1b[32m\x1b[42m   \x1b[33m\x1b[43m   \x1b[34m\x1b[44m   \x1b[35m\x1b[45m   \x1b[36m\x1b[46m   \x1b[37m\x1b[47m   \x1b[m\n\x1b[41C\x1b[38;5;8m\x1b[48;5;8m   \x1b[38;5;9m\x1b[48;5;9m   \x1b[38;5;10m\x1b[48;5;10m   \x1b[38;5;11m\x1b[48;5;11m   \x1b[38;5;12m\x1b[48;5;12m   \x1b[38;5;13m\x1b[48;5;13m   \x1b[38;5;14m\x1b[48;5;14m   \x1b[38;5;15m\x1b[48;5;15m   \x1b[m\n\n\n\x1b[?25h\x1b[?7h'
----------
neofetch_str: [in the terminal, this is in color]
                   -`                    kj7rrv@archlinux 
                  .o+`                   ---------------- 
                 `ooo/                   OS: Arch Linux x86_64 
                `+oooo:                  Host: Inspiron 3521 A05 
               `+oooooo:                 Kernel: 5.18.7-zen1-1-zen 
               -+oooooo+:                Uptime: 23 hours, 10 mins 
             `/:-:++oooo+:               Packages: 1412 (pacman) 
            `/++++/+++++++:              Shell: zsh 5.9 
           `/++++++++++++++:             Resolution: 1366x768 
          `/+++ooooooooooooo/`           DE: GNOME 42.3.1 
         ./ooosssso++osssssso+`          WM: Mutter 
        .oossssso-````/ossssss+`         WM Theme: Adwaita 
       -osssssso.      :ssssssso.        Theme: Adwaita [GTK2/3] 
      :osssssss/        osssso+++.       Icons: breeze [GTK2/3] 
     /ossssssss/        +ssssooo/-       Terminal: mate-terminal 
   `/ossssso+/:-        -:/+osssso+-     Terminal Font: Monospace 10 
  `+sso+:-`                 `.-/+oso:    CPU: Intel Pentium 2117U (2) @ 1.800GHz 
 `++:.                           `-/+/   GPU: Intel 3rd Gen Core processor Graphics Controller 
 .`                                 `/   Memory: 2737MiB / 3797MiB 

                                                                 
                                                                 



----------
neofetch_ansi: [in the terminal, this is in color]
                   -`                    kj7rrv@archlinux 
                  .o+`                   ---------------- 
                 `ooo/                   OS: Arch Linux x86_64 
                `+oooo:                  Host: Inspiron 3521 A05 
               `+oooooo:                 Kernel: 5.18.7-zen1-1-zen 
               -+oooooo+:                Uptime: 23 hours, 10 mins 
             `/:-:++oooo+:               Packages: 1412 (pacman) 
            `/++++/+++++++:              Shell: zsh 5.9 
           `/++++++++++++++:             Resolution: 1366x768 
          `/+++ooooooooooooo/`           DE: GNOME 42.3.1 
         ./ooosssso++osssssso+`          WM: Mutter 
        .oossssso-````/ossssss+`         WM Theme: Adwaita 
       -osssssso.      :ssssssso.        Theme: Adwaita [GTK2/3] 
      :osssssss/        osssso+++.       Icons: breeze [GTK2/3] 
     /ossssssss/        +ssssooo/-       Terminal: mate-terminal 
   `/ossssso+/:-        -:/+osssso+-     Terminal Font: Monospace 10 
  `+sso+:-`                 `.-/+oso:    CPU: Intel Pentium 2117U (2) @ 1.800GHz 
 `++:.                           `-/+/   GPU: Intel 3rd Gen Core processor Graphics Controller 
 .`                                 `/   Memory: 2737MiB / 3797MiB 

                                                                 
                                                                 



----------
neofetch_ansi.instructions():
Traceback (most recent call last):
  File "/home/kj7rrv/projects/neoshare/neoshare.py", line 24, in <module>
    print(list(neofetch_ansi.instructions()))
  File "/home/kj7rrv/.local/lib/python3.10/site-packages/stransi/ansi.py", line 50, in instructions
    yield from escape.instructions()
  File "/home/kj7rrv/.local/lib/python3.10/site-packages/stransi/escape.py", line 58, in instructions
    while token := next(tokens, None):
  File "/home/kj7rrv/.local/lib/python3.10/site-packages/stransi/escape.py", line 42, in tokens
    yield Token(kind=kind, data=int(param))
ValueError: invalid literal for int() with base 10: '?25l\x1b[?7'

Release tag

  • Update README.md with install, badges, example
  • Release first version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.