Skip to content

Commit 6c34878

Browse files
committed
feat: add examples
1 parent 444a930 commit 6c34878

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

example/input.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:commander_ui/commander_ui.dart';
2+
3+
final class Item {
4+
final String name;
5+
final int value;
6+
7+
Item(this.name, this.value);
8+
}
9+
10+
Future<void> main() async {
11+
StdinBuffer.initialize();
12+
13+
final input = Input(
14+
answer: 'Please give us your name',
15+
placeholder: 'firstname lastname',
16+
validate: (value) =>
17+
switch(value) {
18+
String value when value
19+
.trim()
20+
.isNotEmpty => Ok(null),
21+
_ => Err('Please provide a valid name')
22+
}
23+
);
24+
25+
final value = switch(await input.handle()) {
26+
Ok(:final value) => 'My value is $value',
27+
Err(:final error) => Exception('Error: $error'),
28+
_ => 'Unknown',
29+
};
30+
31+
print(value);
32+
}

example/select.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:commander_ui/commander_ui.dart';
2+
3+
final class Item {
4+
final String name;
5+
final int value;
6+
7+
Item(this.name, this.value);
8+
}
9+
10+
Future<void> main() async {
11+
StdinBuffer.initialize();
12+
13+
final select = Select(
14+
answer: "Please select your best hello",
15+
options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
16+
placeholder: 'Type to filter',
17+
selectedLineStyle: (line) => '${AsciiColors.green('❯')} ${AsciiColors.lightCyan(line)}',
18+
unselectedLineStyle: (line) => ' $line',
19+
onDisplay: (item) => item.name
20+
);
21+
22+
final selected = switch(await select.handle()) {
23+
Ok(:final value) => 'My value is ${value.value}',
24+
Err(:final error) => Exception('Error: $error'),
25+
_ => 'Unknown',
26+
};
27+
28+
print(selected);
29+
}

0 commit comments

Comments
 (0)