Skip to content

Commit 5f7dce6

Browse files
committed
feat(select): add display handler
1 parent 0acad55 commit 5f7dce6

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

example/select.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import 'package:commander_ui/src/commander.dart';
22
import 'package:commander_ui/src/level.dart';
33

4+
enum Person {
5+
alice('Alice'),
6+
bob('Bob'),
7+
charlie('Charlie'),
8+
david('David'),
9+
eve('Eve'),
10+
frank('Frank'),
11+
john('John');
12+
13+
final String value;
14+
const Person(this.value);
15+
}
16+
417
Future<void> main() async {
518
final commander = Commander(level: Level.verbose);
619
print('Hello World !');
720

8-
final value = await commander.select('What is your name ?',
9-
defaultValue: 'Charlie',
10-
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John']);
21+
final value = await commander.select<Person>('What is your name ?',
22+
defaultValue: Person.bob,
23+
onDisplay: (person) => person.value,
24+
options: Person.values);
1125

1226
print(value);
1327
}

lib/src/application/components/select.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ final class Select<T> with TerminalTools implements Component<Future<T>> {
134134

135135
buffer.writeAnsiAll([
136136
SetStyles.reset,
137-
Print(' $choice'),
137+
Print(' ${_onDisplay?.call(choice) ?? choice.toString()}'),
138138
AsciiControl.lineFeed,
139139
]);
140140
}
@@ -144,6 +144,7 @@ final class Select<T> with TerminalTools implements Component<Future<T>> {
144144
SetStyles(Style.foreground(Color.brightBlack)),
145145
Print('(Type to filter, press ↑/↓ to navigate, enter to select)'),
146146
SetStyles.reset,
147+
AsciiControl.lineFeed,
147148
]);
148149

149150
if (isInitialRender) {
@@ -169,7 +170,7 @@ final class Select<T> with TerminalTools implements Component<Future<T>> {
169170
SetStyles.reset,
170171
Print(' $_message '),
171172
SetStyles(Style.foreground(Color.brightBlack)),
172-
Print(_selectedOption as String),
173+
Print(_onDisplay?.call(_selectedOption as T) ?? _selectedOption.toString()),
173174
SetStyles.reset,
174175
AsciiControl.lineFeed,
175176
]);

0 commit comments

Comments
 (0)