Skip to content

Commit 08de983

Browse files
committed
feat(core): rework all components with the new commander core
1 parent 27b14e6 commit 08de983

19 files changed

+213
-169
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
## 2.0.0
2+
- Rework the whole library
3+
- Change `input` to `ask` component
4+
- Change `delayed` to `task` component
5+
- Implement `swap` component
6+
- Implement logger methods
7+
18
## 1.8.0
29

310
- Hide internal component methods
411
- Implement `onExit` property on components
512

6-
## 1.7.0
13+
## 1.7.0[CHANGELOG.md](CHANGELOG.md)
714

815
- Implement `alternative screen` component
916
- Add `Table` component in public export

README.md

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,144 +10,139 @@ user input.
1010
To use Commander in your Dart project, add this to your `pubspec.yaml` file :
1111
```yaml
1212
dependencies:
13-
commander_ui: ^1.8.0
13+
commander_ui: ^2.0.0
1414
```
1515
1616
Then 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
2929
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);
4342
}
4443
```
4544

4645
### Select component
4746
A 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
5755
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);
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
8274
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: '🐈'
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
141129
A 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
144137
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',
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
164159
Future<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
190186
Future<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

example/ask.dart

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

4-
enum Shape { square, circle, triangle }
5-
64
Future<void> main() async {
75
final commander = Commander(level: Level.verbose);
8-
print('Hello World !');
96

107
final value = await commander.ask('What is your name ?',
118
// defaultValue: 'John Doe',

example/checkbox.dart

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

4-
enum Shape { square, circle, triangle }
5-
64
Future<void> main() async {
75
final commander = Commander(level: Level.verbose);
8-
print('Hello World !');
96

10-
final value = await commander.checkbox('What is your name ?',
11-
defaultValue: 'Charlie',
12-
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John'],
7+
final value = await commander.checkbox(
8+
'What is your name ?',
9+
defaultValue: 'Charlie',
10+
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John'],
1311
);
1412

1513
print(value);

example/select.dart

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

4-
enum Shape { square, circle, triangle }
5-
64
Future<void> main() async {
75
final commander = Commander(level: Level.verbose);
86
print('Hello World !');
97

108
final value = await commander.select('What is your name ?',
119
defaultValue: 'Charlie',
12-
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John']
13-
);
10+
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John']);
1411

1512
print(value);
1613
}

example/swap.dart

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

4-
enum Shape { square, circle, triangle }
5-
64
Future<void> main() async {
75
final commander = Commander(level: Level.verbose);
8-
print('Hello World !');
96

10-
final value = await commander.swap(
11-
'What is your name ?',
12-
defaultValue: true,placeholder: 'ff'
13-
);
7+
final value = await commander.swap('What is your name ?',
8+
defaultValue: true, placeholder: 'ff');
149

1510
print(value);
1611
}

example/task.dart

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

4-
enum Shape { square, circle, triangle }
5-
64
Future<void> sleep() => Future.delayed(Duration(seconds: 1));
75

8-
Future<String> sleepWithValue() => Future.delayed(Duration(seconds: 1), () => 'Hello World !');
6+
Future<String> sleepWithValue() =>
7+
Future.delayed(Duration(seconds: 1), () => 'Hello World !');
98

109
Future<void> main() async {
1110
final commander = Commander(level: Level.verbose);
12-
print('Hello World !');
1311

14-
final successTask = await commander.task('I am an success task', colored: true);
12+
final successTask =
13+
await commander.task('I am an success task', colored: true);
1514
await successTask.step('Success step 1', callback: sleepWithValue);
1615
await successTask.step('Success step 2', callback: sleep);
1716
successTask.success('Success task data are available !');

lib/commander_ui.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
export 'package:commander_ui/src/commander.dart';
2+
export 'package:commander_ui/src/level.dart';
3+
export 'package:commander_ui/src/application/utils/terminal_tools.dart';
4+
export 'package:commander_ui/src/domains/models/commander_theme.dart';
5+
export 'package:commander_ui/src/domains/models/component.dart';
16

7+
export 'package:mansion/mansion.dart';

lib/src/application/components/ask.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ final class Ask with TerminalTools implements Component<Future<String?>> {
5454

5555
void _waitResponse() {
5656
final input = _hidden ? readLineHiddenSync() : readLineSync();
57-
final response = input == null || input.isEmpty ? resolvedDefaultValue : input;
57+
final response =
58+
input == null || input.isEmpty ? resolvedDefaultValue : input;
5859

5960
if (_validate != null) {
6061
final result = _validate!(response);

0 commit comments

Comments
 (0)