Skip to content

natebot13/dart-vtparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A library for generating a stream of virtual terminal events from a byte stream.

Usage

A simple usage example reading bytes from stdin:

import 'dart:async';
import 'dart:io';

import 'package:vtparse/vtparse.dart';

final q = 'q'.codeUnitAt(0);

void main() async {
  // Don't echo the things typed
  stdin.echoMode = false;
  // Accept char by char instead of line by line
  stdin.lineMode = false;

  // This could use stdin.expand(...) instead, but since you can't close stdin,
  // manually listening to it and passing events to a StreamController allows
  // us to close the StreamController when we're done with it.
  final _stdioByteController = StreamController<int>();
  final _stdioSubscription = stdin.listen((event) {
    for (final byte in event) {
      _stdioByteController.sink.add(byte);
    }
  });

  final parser = VTParse(_stdioByteController.stream);
  parser.events.listen((event) {
    print(event);
    // For example, this closes the stream if 'q' is typed. We wouldn't be able
    // to close the stream if we used stdin.expand above. Using our own stream
    // gives us the ability to close things properly if we're done taking user
    // input.
    if (event.action == VTAction.print && event.ch == q) {
      _stdioSubscription.cancel();
      _stdioByteController.close();
    }
  });
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages