@@ -10,144 +10,139 @@ user input.
1010To use Commander in your Dart project, add this to your ` pubspec.yaml ` file :
1111``` yaml
1212dependencies :
13- commander_ui : ^1.8 .0
13+ commander_ui : ^2.0 .0
1414` ` `
1515
1616Then run ` pub get` to install the dependencies.
1717
1818# # Usage
1919
20- # ## Input component
20+ # ## Ask component
2121
22- A simple example of using Commander to create an input component :
22+ A simple example of using Commander to create an ask component :
2323
24- - ✅ Placeholder
24+ - ✅ Secure
2525- ✅ Validator with error message as callback
2626- ✅ Default value
2727
2828` ` ` dart
2929Future<void> main() async {
30- final input = Input(
31- answer: 'Please give us your name',
32- placeholder: 'firstname lastname',
33- validate: (value) =>
34- switch(value) {
35- String value when value
36- .trim()
37- .isNotEmpty => Ok(null),
38- _ => Err('Please provide a valid name')
39- }
40- );
41-
42- print(await input.handle());
30+ final commander = Commander(level: Level.verbose);
31+
32+ final value = await commander.ask('What is your name ?',
33+ defaultValue: 'John Doe',
34+ validate: (value) {
35+ return switch (value) {
36+ String(:final isEmpty) when isEmpty => 'Name cannot be empty',
37+ _ => null,
38+ };
39+ });
40+
41+ print(value);
4342}
4443` ` `
4544
4645# ## Select component
4746A simple example of using Commander to create an option selection component :
4847
4948- ✅ Placeholder
49+ - ✅ Default selected
5050- ✅ Searchable values
51- - ✅ Selected line custom style
52- - ✅ Unselected line custom style
5351- ✅ Display transformer
5452- ✅ Max display count (default as 5)
5553
5654` ` ` dart
5755Future<void> main() async {
58- final select = Select(
59- answer: "Please select your best hello",
60- options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
61- placeholder: 'Type to filter',
62- selectedLineStyle: (line) => '${AsciiColors.green('❯')} ${AsciiColors.lightCyan(line)}',
63- unselectedLineStyle: (line) => ' $line',
64- onDisplay: (item) => item.name,
65- displayCount: 4
66- );
56+ final commander = Commander(level: Level.verbose);
6757
68- final selected = switch( await select.handle()) {
69- Ok(:final value) => 'My value is ${value.value}' ,
70- Err(:final error) => Exception('Error: $error') ,
71- _ => 'Unknown ',
72- } ;
58+ final value = await commander.select('What is your name ?',
59+ onDisplay: ( value) => value,
60+ placeholder: 'Type to search' ,
61+ defaultValue: 'Charlie ',
62+ options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John']) ;
7363
74- print(selected );
64+ print(value );
7565}
7666` ` `
7767
78- # ## Switching component
79- A simple example of using Commander to create a switch component :
68+ # ## Swap component
69+ A simple example of using Commander to create a swap component :
70+
71+ - ✅ Select value with directional arrows
8072
8173` ` ` dart
8274Future<void> main() async {
83- final component = Switch(
84- answer: 'Do you love cat ?',
85- defaultValue: false,
75+ final commander = Commander(level: Level.verbose);
76+
77+ final value = await commander.swap('Do you love cats',
78+ defaultValue: true,
79+ placeholder: '🐈'
8680 );
8781
88- final value = await component.handle();
89-
90- final result = switch(value) {
91- Ok(:final value) => value.value
92- ? 'I love cat 😍'
93- : 'I hate cat 😕',
94- Err(:final error) => Exception('Error: $error'),
95- _ => 'Unknown',
82+ final str = switch (value) {
83+ true => 'I love cats 😍',
84+ false => 'I prefer dogs 😕',
9685 };
86+
87+ print(str);
9788}
9889` ` `
99- # ## Delayed component
100- A simple example of using Commander to create a delayed component :
90+
91+ # ## Task component
92+ A simple example of using Commander to create a task component :
93+
94+ - ✅ Multiple steps per task
95+ - ✅ Success, warn and error results
96+ - ✅ Sync and async action supports
10197
10298` ` ` dart
103- Future<void> main() async {
104- final delayed = Delayed();
105-
106- delayed.step('Fetching data from remote api...');
107- await wait();
108- delayed.step('Find remote location...');
109- await wait();
110- delayed.step('Extract data...');
111- await wait();
112- delayed.success('Data are available !');
113- }
99+ Future<void> sleep() => Future.delayed(Duration(seconds: 1));
114100
115- Future<void> wait() =>
116- Future.delayed(Duration(seconds: Random().nextInt(3) + 1));
117- ` ` `
101+ Future<String> sleepWithValue() =>
102+ Future.delayed(Duration(seconds: 1), () => 'Hello World !');
118103
119- # ## Progress component
120- A simple example of using Commander to create a progress component :
104+ Future<void> main() async {
105+ final commander = Commander(level: Level.verbose);
106+ print('Hello World !');
121107
122- ` ` ` dart
123- void main() async {
124- final progress = Progress(max: 50);
125-
126- for (int i = 0; i < 50; i++) {
127- progress.next(message: [Print('Downloading file ${i + 1}/50...')]);
128- await Future.delayed(Duration(milliseconds: 50));
129- }
130-
131- progress.done(message: [
132- SetStyles(Style.foreground(Color.green)),
133- Print('✔'),
134- SetStyles.reset,
135- Print(' Download complete!')
136- ]);
108+ final successTask =
109+ await commander.task('I am an success task', colored: true);
110+ await successTask.step('Success step 1', callback: sleepWithValue);
111+ await successTask.step('Success step 2', callback: sleep);
112+ successTask.success('Success task data are available !');
113+
114+ final warnTask = await commander.task('I am an warn task');
115+ await warnTask.step('Warn step 1', callback: sleepWithValue);
116+ await warnTask.step('Warn step 2', callback: sleep);
117+ await warnTask.step('Warn step 3', callback: sleep);
118+ warnTask.warn('Warn task !');
119+
120+ final errorTask = await commander.task('I am an error task');
121+ await errorTask.step('Error step 1', callback: sleepWithValue);
122+ await errorTask.step('Error step 2', callback: sleep);
123+ await errorTask.step('Error step 3', callback: sleep);
124+ errorTask.error('Error task !');
137125}
138126` ` `
139127
140128# ## Checkbox component
141129A simple example of using Commander to create a checkbox component :
142130
131+ - ✅ Placeholder
132+ - ✅ Default checked
133+ - ✅ Single or multiple selection
134+ - ✅ Display transforme
135+
143136` ` ` dart
144137Future<void> main() async {
145- final checkbox = Checkbox(
146- answer: 'What is your favorite pet ?',
138+ final commander = Commander(level: Level.verbose);
139+
140+ final value = await commander.checkbox(
141+ 'What is your favorite pet ?',
142+ defaultValue: 'Charlie',
147143 options: ['cat', 'dog', 'bird'],
148144 );
149145
150- final value = await checkbox.handle();
151146 print(value);
152147}
153148` ` `
@@ -162,7 +157,8 @@ A simple example of using Commander to create a table component :
162157
163158` ` ` dart
164159Future<void> main() async {
165- Table(
160+ final commander = Commander(level: Level.verbose);
161+ commander.table(
166162 columns: ['Name', 'Age', 'Country', 'City'],
167163 lineSeparator: false,
168164 columnSeparator: false,
@@ -188,13 +184,18 @@ A simple example of using Commander to create an alternative screen component :
188184
189185` ` ` dart
190186Future<void> main() async {
191- final screen = AlternateScreen(title: 'Hello World !');
192- screen.start();
187+ final commander = Commander(level: Level.verbose);
193188
194- print('Hello World !');
189+ final screen = commander.screen(title: 'First screen');
190+ screen.enter();
191+
192+ await sleep();
193+ print('Hello screen !');
194+ await sleep();
195+
196+ screen.leave();
195197
196- await wait();
197- screen.stop();
198+ print('Goodbye screen !');
198199}
199200
200201
0 commit comments