commander_ui 2.2.2 copy "commander_ui: ^2.2.2" to clipboard
commander_ui: ^2.2.2 copied to clipboard

Commander is a Dart library for creating user interfaces within the terminal.

Commander #

Commander is a Dart library for creating user interfaces within the terminal.

It provides interactive components such as option selection and text input with advanced management of user input.

Installation #

To use Commander in your Dart project, add this to your pubspec.yaml file :

dependencies:
  commander_ui: ^2.0.0
copied to clipboard

Then run pub get to install the dependencies.

Usage #

Ask component #

A simple example of using Commander to create an ask component :

  • ✅ Secure
  • ✅ Validator with error message as callback
  • ✅ Default value
Future<void> main() async {
  final commander = Commander(level: Level.verbose);

  final value = await commander.ask('What is your name ?',
      defaultValue: 'John Doe',
      validate: (value) {
        return switch (value) {
          String(:final isEmpty) when isEmpty => 'Name cannot be empty',
          _ => null,
        };
      });

  print(value);
}
copied to clipboard

Select component #

A simple example of using Commander to create an option selection component :

  • ✅ Placeholder
  • ✅ Default selected
  • ✅ Searchable values
  • ✅ Display transformer
  • ✅ Max display count (default as 5)
Future<void> main() async {
  final commander = Commander(level: Level.verbose);

  final value = await commander.select('What is your name ?',
      onDisplay: (value) => value,
      placeholder: 'Type to search',
      defaultValue: 'Charlie',
      options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John']);

  print(value);
}
copied to clipboard

Swap component #

A simple example of using Commander to create a swap component :

  • ✅ Select value with directional arrows
Future<void> main() async {
  final commander = Commander(level: Level.verbose);

  final value = await commander.swap('Do you love cats', 
    defaultValue: true, 
    placeholder: '🐈'
  );
  
  final str = switch (value) {
    true => 'I love cats 😍',
    false => 'I prefer dogs 😕',
  };

  print(str);
}
copied to clipboard

Task component #

A simple example of using Commander to create a task component :

  • ✅ Multiple steps per task
  • ✅ Success, warn and error results
  • ✅ Sync and async action supports
Future<void> sleep() => Future.delayed(Duration(seconds: 1));

Future<String> sleepWithValue() =>
    Future.delayed(Duration(seconds: 1), () => 'Hello World !');

Future<void> main() async {
  final commander = Commander(level: Level.verbose);
  print('Hello World !');

  final successTask =
  await commander.task('I am an success task', colored: true);
  await successTask.step('Success step 1', callback: sleepWithValue);
  await successTask.step('Success step 2', callback: sleep);
  successTask.success('Success task data are available !');

  final warnTask = await commander.task('I am an warn task');
  await warnTask.step('Warn step 1', callback: sleepWithValue);
  await warnTask.step('Warn step 2', callback: sleep);
  await warnTask.step('Warn step 3', callback: sleep);
  warnTask.warn('Warn task !');

  final errorTask = await commander.task('I am an error task');
  await errorTask.step('Error step 1', callback: sleepWithValue);
  await errorTask.step('Error step 2', callback: sleep);
  await errorTask.step('Error step 3', callback: sleep);
  errorTask.error('Error task !');
}
copied to clipboard

Checkbox component #

A simple example of using Commander to create a checkbox component :

  • ✅ Placeholder
  • ✅ Default checked
  • ✅ Single or multiple selection
  • ✅ Display transforme
Future<void> main() async {
  final commander = Commander(level: Level.verbose);

  final value = await commander.checkbox(
    'What is your favorite pet ?',
    defaultValue: 'Charlie',
    options: ['cat', 'dog', 'bird'],
  );

  print(value);
}
copied to clipboard

Table component #

A simple example of using Commander to create a table component :

  • ✅ Without column and line borders
  • ✅ With column and line borders
  • ✅ With column borders and without line borders
  • ✅ With line borders and without column borders
Future<void> main() async {
  final commander = Commander(level: Level.verbose);
  commander.table(
    columns: ['Name', 'Age', 'Country', 'City'],
    lineSeparator: false,
    columnSeparator: false,
    data: [
      ['Alice', '20', 'USA', 'New York'],
      ['Bob', '25', 'Canada', 'Toronto'],
      ['Charlie', '30', 'France', 'Paris'],
      ['David', '35', 'Germany', 'Berlin'],
      ['Eve', '40', 'Italy', 'Rome'],
      ['Frank', '45', 'Japan', 'Tokyo'],
      ['John', '50', 'China', 'Beijing'],
    ],
  );
}
copied to clipboard

Alternative screen component #

A simple example of using Commander to create an alternative screen component :

  • ✅ Set title
  • ✅ Clear screen on start
  • ✅ Restore screen on stop
Future<void> main() async {
  final commander = Commander(level: Level.verbose);

  final screen = commander.screen(title: 'First screen');
  screen.enter();

  await sleep();
  print('Hello screen !');
  await sleep();

  screen.leave();

  print('Goodbye screen !');
}


Future<void> wait() =>
    Future.delayed(Duration(seconds: Random().nextInt(3) + 1));
copied to clipboard
4
likes
0
points
111
downloads

Publisher

verified publisherleadcode.fr

Weekly Downloads

2024.09.08 - 2025.03.23

Commander is a Dart library for creating user interfaces within the terminal.

Repository (GitHub)
View/report issues

Topics

#cli #tui

License

unknown (license)

Dependencies

collection, ffi, mansion, win32

More

Packages that depend on commander_ui