Package: hl-vt100
vt100 emulatorvt100-emulatoris a headlessvt100emulator, a bit like any
terminal you may use daily (like urxvt, xterm, ...) but those you're
using are NOT headless, they have a graphical interface to interact
with you, human). Here,vt100-emulatoris only the underlying aCandPythonAPI to an actual emulator, so you can do everything you
want with it, like interfacing over TCP, HTTP, automatically testing
your implementationmallocagainsttopwhile runningtopin the
headless terminal, whatever pleases you.For copyright information, please see the file LICENSE in this
directory or in the files of the source tree.INSTALLPython modulepip install hl-vt100Python module from sourceThe simpliest way is just to runpip install .from within the repo,
but if you want build artifacts, you can build one in an isolated
environment using:pip install build
python -m buildOr just create ansdistthe quick way:
n
python setup.py sdistIn both case it will provide a build artifact in thedist/directory
that you can alsopip install.Usage using the Python wrapper (same methods in C)importhl_vt100defdump(vt100):print("╭"+"─"*vt100.width+"╮")forlineinvt100.getlines():print(f"│{line:{vt100.width}}│")print("╰"+"─"*vt100.width+"╯")defmain():vt100=hl_vt100.vt100_headless()vt100.changed_callback=lambda:dump(vt100)vt100.fork('top',['top'])vt100.main_loop()if__name__=='__main__':main()Usage using the C library#include<stdio.h>#include<stdlib.h>#include"hl_vt100.h"voidchanged(structvt100_headless*vt100){constchar**lines;lines=vt100_headless_getlines(vt100);for(unsignedinty=0;y<vt100->term->height;++y){write(1,"|",1);write(1,lines[y],vt100->term->width);write(1,"|\n",2);}write(1,"\n",1);}intmain(intac,char**av){structvt100_headless*vt100;char*argv[]={"top",NULL};vt100=new_vt100_headless();vt100_headless_fork(vt100,argv[0],argv);vt100->changed=changed;vt100_headless_main_loop(vt100);returnEXIT_SUCCESS;}Code overviewlw_terminal_parser, lw_terminal_vt100, and hl_vt100 are three modules used to emulate a vt100 terminal:-------------
                                  |           |
                                  | Your Code |
                                  |           |
                                  -------------
                                    |      ^
 vt100 = vt100_headless_init()      |      |
 vt100->changed = changed;          |      | hl_vt100 raises 'changed'
 vt100_headless_fork(vt100, ...     |      | when the screen has changed.
                                    |      | You get the content of the screen
                                    |      | calling vt100_headless_getlines.
                                    V      |
                                  -------------              -------------
 Read from PTY master and write | |           |     PTY      |           |
 to lw_terminal_vt100_read_str  | |  hl_vt100 |<------------>|  Program  |
                                V |           |Master   Slave|           |
                                  -------------              -------------
                                   |        |^ hl_vt100 gets lw_terminal_vt100's
                                   |        || lines by calling
                                   |        || lw_terminal_vt100_getlines
                                   |        ||
                                   |        ||
                                   V        V|
                              ----------------------
 Got data from              | |                    | Recieve data from callbacks
 lw_terminal_vt100_read_str | | lw_terminal_vt100  | And store an in-memory
 give it to                 | |                    | state of the vt100 terminal
 lw_terminal_parser_read_strV ----------------------
                                 |              ^
                                 |              |
                                 |              |
                                 |              |
                                 |              |
                                 |              | Callbacks
                                 |              |
                                 |              |
                                 |              |
                                 |              |
                                 |              |
                                 V              |
                              ----------------------
 Got data from                |                    |
 lw_terminal_pasrser_read_str | lw_terminal_parser |
 parses, and call callbacks   |                    |
                              ----------------------lw_terminal_parserlw_terminal_parserparses terminal escape sequences, calling callbacks
when a sequence is sucessfully parsed, readexample/parse.c.Provides:struct lw_terminal *lw_terminal_parser_init(void);void lw_terminal_parser_destroy(struct lw_terminal* this);void lw_terminal_parser_default_unimplemented(struct lw_terminal* this, char *seq, char chr);void lw_terminal_parser_read(struct lw_terminal *this, char c);void lw_terminal_parser_read_str(struct lw_terminal *this, char *c);lw_terminal_vt100Hooks into alw_terminal_parserand keep an in-memory state of the
screen of a vt100.Provides:struct lw_terminal_vt100 *lw_terminal_vt100_init(void *user_data, void (*unimplemented)(struct lw_terminal* term_emul, char *seq, char chr));char lw_terminal_vt100_get(struct lw_terminal_vt100 *vt100, unsigned int x, unsigned int y);const char **lw_terminal_vt100_getlines(struct lw_terminal_vt100 *vt100);void lw_terminal_vt100_destroy(struct lw_terminal_vt100 *this);void lw_terminal_vt100_read_str(struct lw_terminal_vt100 *this, char *buffer);hl_vt100Forks a program, plug its io to a pseudo terminal and emulate a vt100
usinglw_terminal_vt100.Provides:void vt100_headless_fork(struct vt100_headless *this, const char *progname, char *const argv[]);struct vt100_headless *vt100_headless_init(void);const char **vt100_headless_getlines(struct vt100_headless *this);