Guides/Installation

Installation

dart_tui is a pure-Dart package on pub.dev with no native build step. Add it, import it, and run a Program.

Add the dependency

Add dart_tui to your pubspec.yaml:

yaml
dependencies:
  dart_tui: ^1.4.0

Then fetch it:

bash
dart pub get

Or add it in one command:

bash
dart pub add dart_tui

Requirements

dart_tui targets the Dart SDK ≥ 3.5. It runs on macOS, Linux and Windows terminals (Windows console mode is handled via kernel32).

Quick start

Every app is a Program running a Model. Here's a self-contained counter that ticks once a second and quits at 5:

dart
import 'package:dart_tui/dart_tui.dart';

void main() async {
  await Program(
    options: const ProgramOptions(altScreen: true),
  ).run(CounterModel());
}

final class CounterModel extends TeaModel {
  CounterModel({this.count = 0});
  final int count;

  @override
  Cmd? init() => tick(const Duration(seconds: 1), (_) => _TickMsg());

  @override
  (Model, Cmd?) update(Msg msg) {
    if (msg is _TickMsg) {
      if (count >= 5) return (this, () => quit());
      return (CounterModel(count: count + 1),
          tick(const Duration(seconds: 1), (_) => _TickMsg()));
    }
    if (msg is KeyMsg && (msg.key == 'q' || msg.key == 'ctrl+c')) {
      return (this, () => quit());
    }
    return (this, null);
  }

  @override
  View view() => newView('Count: $count\n\nPress q to quit.');
}

final class _TickMsg extends Msg {}
Next: learn how Model, update and view fit together in the Model–Update–View guide.

Related