@@ -10,7 +10,7 @@ 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.1 .0
13
+ commander_ui : ^1.3 .0
14
14
` ` `
15
15
16
16
Then run ` pub get` to install the dependencies.
@@ -26,16 +26,21 @@ A simple example of using Commander to create an input component :
26
26
- ❌ Default value
27
27
28
28
` ` ` dart
29
- StdinBuffer.initialize();
30
-
31
- final input = Input(
32
- answer: 'Please give us your name',
33
- placeholder: 'firstname lastname',
34
- validate: (value) => switch(value) {
35
- String value when value.trim().isNotEmpty => Ok(null),
36
- _ => Err('Please provide a valid name')
37
- }
38
- );
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());
43
+ }
39
44
` ` `
40
45
41
46
# ## Select component
@@ -46,47 +51,88 @@ A simple example of using Commander to create an option selection component :
46
51
- ✅ Selected line custom style
47
52
- ✅ Unselected line custom style
48
53
- ✅ Display transformer
49
- - ❌ Count limitation (actually defined as 5)
54
+ - ✅ Max display count (default as 5)
50
55
51
56
` ` ` dart
52
- StdinBuffer.initialize();
53
-
54
- final select = Select(
55
- answer: "Please select your best hello",
56
- options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
57
- placeholder: 'Type to filter',
58
- selectedLineStyle: (line) => '${AsciiColors.green('❯')} ${AsciiColors.lightCyan(line)}',
59
- unselectedLineStyle: (line) => ' $line',
60
- onDisplay: (item) => item.name
61
- );
62
-
63
- final selected = switch(await select.handle()) {
64
- Ok(:final value) => 'My value is ${value.value}',
65
- Err(:final error) => Exception('Error: $error'),
66
- _ => 'Unknown',
67
- };
68
-
69
- print(selected);
57
+ 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
+ );
67
+
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
+ };
73
+
74
+ print(selected);
75
+ }
70
76
` ` `
71
77
72
78
# ## Switching component
73
79
A simple example of using Commander to create a switch component :
74
80
75
81
` ` ` dart
76
- StdinBuffer.initialize();
77
-
78
- final switch = Switch(
79
- answer: 'Do you love cat ?',
80
- defaultValue: false,
81
- );
82
-
83
- final result = switch(await switch.handle()) {
84
- Ok(:final value) => value.value
85
- ? 'I love cat 😍'
86
- : 'I hate cat 😕',
87
- Err(:final error) => Exception('Error: $error'),
88
- _ => 'Unknown',
89
- };
90
-
91
- print(result);
82
+ Future<void> main() async {
83
+ final component = Switch(
84
+ answer: 'Do you love cat ?',
85
+ defaultValue: false,
86
+ );
87
+
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',
96
+ };
97
+ }
98
+ ` ` `
99
+ # ## Delayed component
100
+ A simple example of using Commander to create a delayed component :
101
+
102
+ ` ` ` 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
+ }
114
+
115
+ Future<void> wait() =>
116
+ Future.delayed(Duration(seconds: Random().nextInt(3) + 1));
117
+ ` ` `
118
+
119
+ # ## Progress component
120
+ A simple example of using Commander to create a progress component :
121
+
122
+ ` ` ` dart
123
+ void main() async {
124
+ final progress = ProgressBar(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
+ ]);
137
+ }
92
138
` ` `
0 commit comments