dart_console 0.4.0
dart_console: ^0.4.0 copied to clipboard
A helper library for command-line applications that need more control over input/output than the standard library provides.
A Dart library for building console applications.
This library contains a variety of useful functions for console application development, including:
- Reading the current window dimensions (height, width)
- Reading and setting the cursor location
- Setting foreground and background colors
- Manipulating the console into "raw mode", which allows more advanced
keyboard input processing than the default
dart:iolibrary. - Reading keys and control sequences from the keyboard
- Writing aligned text to the screen
The library has been used to implement a Dart version of the
Kilo text editor; when compiled with dart2native that
results in a self-contained kilo executable. The library is sufficient for a
reasonably complete set of usage, including readline-style CLI and basic text
games.
The library assumes a terminal that recognizes and implements common ANSI escape
sequences. The package has been tested on macOS, Linux and
Windows 10 version 1903. Windows support is preliminary - the
library is tested on the Windows Terminal and cmd, but some
functions will not work on older versions of Windows that do not support ANSI
escape sequences.
As an aside, this library demonstrates usage of the dart:ffi library
for calling system APIs and separating Win32 and UNIX-style implementations.
Usage #
A simple example for the dart_console package:
import 'package:dart_console/dart_console.dart';
main() {
final console = Console();
console.clearScreen();
console.resetCursorPosition();
console.writeAligned(
'Console size is ${console.windowWidth} cols and ${console.windowHeight} rows.',
TextAlignment.Center);
console.writeLine();
return 0;
}
More comprehensive demos of the Console class are provided, as follows:
| Example | Description |
|---|---|
demo.dart |
Suite of test demos that showcase various capabilities of the package |
main.dart |
Basic demo of how to get started with the package |
keys.dart |
Demonstrates how control characters are processed by dart_console |
readline.dart |
Sample command-line interface |
kilo.dart |
Rudimentary text editor |
Acknowledgements #
Special thanks to Matt Sullivan (@mjohnsullivan) and Samir Jindel (@sjindel-google) for their help understanding the vagaries of FFI in its early state.
Features and bugs #
Please file feature requests and bugs at the issue tracker.