termilary 0.2.2 copy "termilary: ^0.2.2" to clipboard
termilary: ^0.2.2 copied to clipboard

ncurses like package for a terminal-style UI in a web canvas.

example/main.dart

import "package:termilary/termilary.dart";
import "package:utilary/utilary.dart" show rng, Vector;

const rows = 25;
const cols = 40;

final Font font = Font("Consolas", size: 14, w: 16, h: 16, x: 1, y: 13);

//Create a new 40 x 25 characters terminal using a Consolas font.
//Optional argument [canvas] isn't provided (will create a new canvas)
final CanvasTerminal terminal = CanvasTerminal(cols, rows, font);

String inputMapper(PointInput i) => i.toString();

final UserInterface<String> ui = UserInterface(inputMapper, terminal);

void main() {
  ui.keyPress.bind("up", KeyInput(KeyCodes.up));
  ui.keyPress.bind("right", KeyInput(KeyCodes.right));
  ui.keyPress.bind("down", KeyInput(KeyCodes.down));
  ui.keyPress.bind("left", KeyInput(KeyCodes.left));

  ui.push(ExampleScreen("HELLO, move me with the arrow keys"));

  //Start the UI
  ui.handlingInput = true;
  ui.running = true;
}

class ExampleScreen extends Screen<String> {
  int _x;
  int _y;

  int _ticks;

  Set<Vector> _points = Set();

  Color _textColor;
  final String _text;

  ExampleScreen(this._text, [this._textColor]) {
    _x = _y = 0;
    _ticks = 10;
    _textColor ??= Color.white;
  }

  @override
  bool handleInput(String input) {
    int xDir = 0, yDir = 0;

    switch (input) {
      case "up":
        yDir = -1;
        break;

      case "right":
        xDir = 1;
        break;

      case "down":
        yDir = 1;
        break;

      case "left":
        xDir = -1;
        break;
    }

    List<String> fields = input.split(",");
    if (fields.length > 2) {
      fields = fields.take(2).toList();
      fields = fields.map((String str) => str.split(":")[1]).toList();

      int x = int.parse(fields[0]);
      int y = int.parse(fields[1]);

      _points.add(Vector(x, y));
    }

    _move(xDir, yDir);
    dirty();

    return true;
  }

  void _move(int x, int y) {
    _x += x;
    _y += y;
  }

  @override
  void update() {
    _ticks--;

    if (_ticks <= 0) {
      _ticks = 10;
      _textColor = Color(rng.range(256), rng.range(256), rng.range(256));
      dirty();
    }
  }

  @override
  void render(Terminal t) {
    t.drawString(_x, _y, _text, _textColor);

    _points.forEach((Vector v) => t.setGlyph(v.x, v.y, Glyph('#', Color.white)));
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

ncurses like package for a terminal-style UI in a web canvas.

Repository (GitHub)
View/report issues

License

BSD-2-Clause (LICENSE)

Dependencies

utilary

More

Packages that depend on termilary