@@ -10,144 +10,139 @@ user input.
10
10
To use Commander in your Dart project, add this to your ` pubspec.yaml ` file :
11
11
``` yaml
12
12
dependencies :
13
- commander_ui : ^1.8 .0
13
+ commander_ui : ^2.0 .0
14
14
` ` `
15
15
16
16
Then run ` pub get` to install the dependencies.
17
17
18
18
# # Usage
19
19
20
- # ## Input component
20
+ # ## Ask component
21
21
22
- A simple example of using Commander to create an input component :
22
+ A simple example of using Commander to create an ask component :
23
23
24
- - ✅ Placeholder
24
+ - ✅ Secure
25
25
- ✅ Validator with error message as callback
26
26
- ✅ Default value
27
27
28
28
` ` ` dart
29
29
Future<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);
43
42
}
44
43
` ` `
45
44
46
45
# ## Select component
47
46
A simple example of using Commander to create an option selection component :
48
47
49
48
- ✅ Placeholder
49
+ - ✅ Default selected
50
50
- ✅ Searchable values
51
- - ✅ Selected line custom style
52
- - ✅ Unselected line custom style
53
51
- ✅ Display transformer
54
52
- ✅ Max display count (default as 5)
55
53
56
54
` ` ` dart
57
55
Future<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);
67
57
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']) ;
73
63
74
- print(selected );
64
+ print(value );
75
65
}
76
66
` ` `
77
67
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
80
72
81
73
` ` ` dart
82
74
Future<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: '🐈'
86
80
);
87
81
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 😕',
96
85
};
86
+
87
+ print(str);
97
88
}
98
89
` ` `
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
101
97
102
98
` ` ` 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));
114
100
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 !');
118
103
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 !');
121
107
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 !');
137
125
}
138
126
` ` `
139
127
140
128
# ## Checkbox component
141
129
A simple example of using Commander to create a checkbox component :
142
130
131
+ - ✅ Placeholder
132
+ - ✅ Default checked
133
+ - ✅ Single or multiple selection
134
+ - ✅ Display transforme
135
+
143
136
` ` ` dart
144
137
Future<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',
147
143
options: ['cat', 'dog', 'bird'],
148
144
);
149
145
150
- final value = await checkbox.handle();
151
146
print(value);
152
147
}
153
148
` ` `
@@ -162,7 +157,8 @@ A simple example of using Commander to create a table component :
162
157
163
158
` ` ` dart
164
159
Future<void> main() async {
165
- Table(
160
+ final commander = Commander(level: Level.verbose);
161
+ commander.table(
166
162
columns: ['Name', 'Age', 'Country', 'City'],
167
163
lineSeparator: false,
168
164
columnSeparator: false,
@@ -188,13 +184,18 @@ A simple example of using Commander to create an alternative screen component :
188
184
189
185
` ` ` dart
190
186
Future<void> main() async {
191
- final screen = AlternateScreen(title: 'Hello World !');
192
- screen.start();
187
+ final commander = Commander(level: Level.verbose);
193
188
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();
195
197
196
- await wait();
197
- screen.stop();
198
+ print('Goodbye screen !');
198
199
}
199
200
200
201
0 commit comments