GithubHelp home page GithubHelp logo

rasmus-toftdahl-olesen / libvt100 Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 5.0 239 KB

A purely .net/C# library for parsing a VT100/ANSI stream

License: Apache License 2.0

C# 100.00%
ansi-data parsing-library ansi-colors csharp-library dotnet

libvt100's Introduction

libvt100

A purely .net/C# library for parsing a VT100/ANSI stream

When writing anything that needs to communicate with a terminal in some way it is almost always speaking some dialect of VT100 or ANSI.

This library aims solely at parsing a stream of VT100/ANSI data and then letting the host application do the rendering. Many other project also parse VT100/ANSI data but their parser is always tangled up with the actual rendering of the data, making reuse in other projects problematic.

Projects Using libvt100:

  • winprint - Uses libvt100 to parse and print source code files that have been 'syntax highlighted' by Pygments and output using Pygment's terminal256 Formatter. The terminal256 formatter generates ANSI escapes for formatting. This code takes an input document:
_screen = new DynamicScreen(_minLineLen);
    IAnsiDecoder vt100 = new AnsiDecoder();
    vt100.Encoding = Encoding;
    vt100.Subscribe(_screen);
    var bytes = vt100.Encoding.GetBytes(Document);
    if (bytes != null && bytes.Length > 0) {
        vt100.Input(bytes);
    }

Then the winprint rendering engine simply enumerates over this (psuedocode):

    foreach (var line in _screen){
        SmartlyDrawLine(line);
    }

Real code is here:

https://github.com/tig/winprint/blob/eee9768506f08e2f5af75f90d9571dc94d8d42fa/src/WinPrint.Core/ContentTypeEngines/AnsiCte.cs#L196

A cool thing about this is features like line-wrap and /t expansion are handled by libvt100.

Hopefully other projects will start using libvt100 and we can stop reinventing the wheel in each project.

libvt100's People

Contributors

rasmus-toftdahl-olesen avatar tig avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

libvt100's Issues

Tabs

Posting this in case there was any thought applied previously before I dive in.

Right now, there's no evidence that libvt100 has any support for tabs (\t). IIRC, classic consoles like vt100 defined tabs in terms of tab-stops.

E.g. `\t' moves cursor to the next tab stop, or to the right margin if there are no more tab stops.

This is also how modern code editors work. Screenshot from VS Code:
image

Here's how winprint currently renders this, showing the problem:
image

Approach I'm considering:

On Input convert \t in the input stream into the right number of spaces to ensure text that follows starts at the next tab stop.

The screen buffer will have lost the fact that the tabs were there. No different than how \n is handled now though.

To address this, an enhancement could be added such that on Input we keep track of where \t were and force renderers to decode and and place correctly (e.g. by extending Run to have a startsWithTab field).

How to deal with unrecognized command sequences

Is there a way with this library to handle command sequences which aren't 
already built into the library itself?

I'm connecting to a very old system, which seems to break or show characters 
that are unexpected.

Original issue reported on code.google.com by [email protected] on 17 Jul 2012 at 7:08

Recursion with ProcessCommandBuffer causes stack overflow

Recursion with ProcessCommandBuffer in EscapeCharacterDecoder.cs causes stack overflow.

I didn't notice this until I started testing with larger files. But once I get beyond a certain size (it's also dependent on how many ansi sequences there are) it happens.

Gonna either need to find another library or figure out how to refactor ProcessCommandBuffer to not use recursion. Rats.

Note I tested this with Screen as well as my new DynamicScreen and while mine fails sooner (because I'm using larger constructs to track ansi runs), Screen still stack overflows with even moderate sized files (100k).

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.