Skip to content

๐Ÿ› ๏ธ Commander is a Dart library for creating user command line interfaces within the terminal thank to tui components.

License

Notifications You must be signed in to change notification settings

LeadcodeDev/commander

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

35 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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: ^1.1.0

Then run pub get to install the dependencies.

Usage

Input component

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

  • โœ… Placeholder
  • โœ… Validator with error message as callback
  • โŒ Default value
StdinBuffer.initialize();

final input = Input(
  answer: 'Please give us your name',
  placeholder: 'firstname lastname',
  validate: (value) => switch(value) {
    String value when value.trim().isNotEmpty => Ok(null),
    _ => Err('Please provide a valid name')
  }
);

Select component

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

  • โœ… Placeholder
  • โœ… Searchable values
  • โœ… Selected line custom style
  • โœ… Unselected line custom style
  • โœ… Display transformer
  • โŒ Count limitation (actually defined as 5)
StdinBuffer.initialize();

final select = Select(
  answer: "Please select your best hello",
  options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
  placeholder: 'Type to filter',
  selectedLineStyle: (line) => '${AsciiColors.green('โฏ')} ${AsciiColors.lightCyan(line)}',
  unselectedLineStyle: (line) => '  $line',
  onDisplay: (item) => item.name
);

final selected = switch(await select.handle()) {
  Ok(:final value) => 'My value is ${value.value}',
  Err(:final error) => Exception('Error: $error'),
  _ => 'Unknown',
};

print(selected);

Switching component

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

StdinBuffer.initialize();

final switch = Switch(
  answer: 'Do you love cat ?',
  defaultValue: false,
);

final result = switch(await switch.handle()) {
  Ok(:final value) => value.value 
    ? 'I love cat ๐Ÿ˜' 
    : 'I hate cat ๐Ÿ˜•',
  Err(:final error) => Exception('Error: $error'),
  _ => 'Unknown',
};

print(result);

About

๐Ÿ› ๏ธ Commander is a Dart library for creating user command line interfaces within the terminal thank to tui components.

Topics

Resources

License

Stars

Watchers

Forks

Languages