dart_console 0.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

A Dart library for building console applications.

pub package Language

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:io library.
  • 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, and 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:

ExampleDescription
demo.dartSuite of test demos that showcase various capabilities of the package
main.dartBasic demo of how to get started with the package
keys.dartDemonstrates how control characters are processed by dart_console
readline.dartSample command-line interface
kilo.dartRudimentary 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.

0.3.1 #

  • Increased compatibility on Win32 consoles

0.3.0 #

  • Add support for Windows consoles that recognize ANSI escape sequences

0.2.0 #

  • Add readline function and escape character support

0.1.0 #

  • Initial version

example/main.dart

import 'package:dart_console/dart_console.dart';

main() {
  final console = Console();
  console.setBackgroundColor(ConsoleColor.blue);
  console.setForegroundColor(ConsoleColor.white);
  console.writeLine('Simple Demo', TextAlignment.center);
  console.resetColorAttributes();

  console.writeLine();

  console.writeLine('This console window has ${console.windowWidth} cols and '
      '${console.windowHeight} rows.');
  console.writeLine();

  console.writeLine('This text is left aligned.', TextAlignment.left);
  console.writeLine('This text is center aligned.', TextAlignment.center);
  console.writeLine('This text is right aligned.', TextAlignment.right);

  for (ConsoleColor color in ConsoleColor.values) {
    console.setForegroundColor(color);
    console.writeLine(color.toString().split('.').last);
  }
  console.resetColorAttributes();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_console: ^0.3.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_console/dart_console.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:dart_console/dart_console.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0-dev.1.0 <3.0.0
Dev dependencies
pedantic ^1.8.0+1
test ^1.6.11