Skip to content

Commit

Permalink
feat: prepare 1.3.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadcodeDev committed Sep 27, 2024
1 parent fcfe114 commit 722e85a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 56 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.3.0

- Add select max result into configurable option
- Add delayed component into readme
- Add progress component into readme

## 1.2.0

- Implement switch component
Expand Down
138 changes: 92 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ user input.
To use Commander in your Dart project, add this to your `pubspec.yaml` file :
```yaml
dependencies:
commander_ui: ^1.1.0
commander_ui: ^1.3.0
```
Then run `pub get` to install the dependencies.
Expand All @@ -26,16 +26,21 @@ A simple example of using Commander to create an input component :
- ❌ Default value

```dart
StdinBuffer.initialize();
final input = Input(
answer: 'Please give us your name',
placeholder: 'firstname lastname',
validate: (value) => switch(value) {
String value when value.trim().isNotEmpty => Ok(null),
_ => Err('Please provide a valid name')
}
);
Future<void> main() async {
final input = Input(
answer: 'Please give us your name',
placeholder: 'firstname lastname',
validate: (value) =>
switch(value) {
String value when value
.trim()
.isNotEmpty => Ok(null),
_ => Err('Please provide a valid name')
}
);
print(await input.handle());
}
```

### Select component
Expand All @@ -46,47 +51,88 @@ A simple example of using Commander to create an option selection component :
- ✅ Selected line custom style
- ✅ Unselected line custom style
- ✅ Display transformer
- ❌ Count limitation (actually defined as 5)
- ✅ Max display count (default as 5)

```dart
StdinBuffer.initialize();
final select = Select(
answer: "Please select your best hello",
options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
placeholder: 'Type to filter',
selectedLineStyle: (line) => '${AsciiColors.green('❯')} ${AsciiColors.lightCyan(line)}',
unselectedLineStyle: (line) => ' $line',
onDisplay: (item) => item.name
);
final selected = switch(await select.handle()) {
Ok(:final value) => 'My value is ${value.value}',
Err(:final error) => Exception('Error: $error'),
_ => 'Unknown',
};
print(selected);
Future<void> main() async {
final select = Select(
answer: "Please select your best hello",
options: List.generate(20, (index) => Item('${index + 1}. Hello World', index + 1)),
placeholder: 'Type to filter',
selectedLineStyle: (line) => '${AsciiColors.green('❯')} ${AsciiColors.lightCyan(line)}',
unselectedLineStyle: (line) => ' $line',
onDisplay: (item) => item.name,
displayCount: 4
);
final selected = switch(await select.handle()) {
Ok(:final value) => 'My value is ${value.value}',
Err(:final error) => Exception('Error: $error'),
_ => 'Unknown',
};
print(selected);
}
```

### Switching component
A simple example of using Commander to create a switch component :

```dart
StdinBuffer.initialize();
final switch = Switch(
answer: 'Do you love cat ?',
defaultValue: false,
);
final result = switch(await switch.handle()) {
Ok(:final value) => value.value
? 'I love cat 😍'
: 'I hate cat 😕',
Err(:final error) => Exception('Error: $error'),
_ => 'Unknown',
};
print(result);
Future<void> main() async {
final component = Switch(
answer: 'Do you love cat ?',
defaultValue: false,
);
final value = await component.handle();
final result = switch(value) {
Ok(:final value) => value.value
? 'I love cat 😍'
: 'I hate cat 😕',
Err(:final error) => Exception('Error: $error'),
_ => 'Unknown',
};
}
```
### Delayed component
A simple example of using Commander to create a delayed component :

```dart
Future<void> main() async {
final delayed = Delayed();
delayed.step('Fetching data from remote api...');
await wait();
delayed.step('Find remote location...');
await wait();
delayed.step('Extract data...');
await wait();
delayed.success('Data are available !');
}
Future<void> wait() =>
Future.delayed(Duration(seconds: Random().nextInt(3) + 1));
```

### Progress component
A simple example of using Commander to create a progress component :

```dart
void main() async {
final progress = ProgressBar(max: 50);
for (int i = 0; i < 50; i++) {
progress.next(message: [Print('Downloading file ${i + 1}/50...')]);
await Future.delayed(Duration(milliseconds: 50));
}
progress.done(message: [
SetStyles(Style.foreground(Color.green)),
Print('✔'),
SetStyles.reset,
Print(' Download complete!')
]);
}
```
2 changes: 1 addition & 1 deletion example/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Future<void> main() async {
final select = Select(
answer: 'What is your name?',
options: ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'John'],
displayElementCount: 2);
displayCount: 2);

final value = await select.handle();
print(value);
Expand Down
16 changes: 8 additions & 8 deletions lib/src/components/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:mansion/mansion.dart';
final class Select<T> with Tools implements Component<T> {
String filter = '';
int currentIndex = 0;
int displayElementCount;
int displayCount;
bool isRendering = false;

final List<T> options;
Expand Down Expand Up @@ -37,7 +37,7 @@ final class Select<T> with Tools implements Component<T> {
Select({
required this.answer,
required this.options,
this.displayElementCount = 5,
this.displayCount = 5,
this.onDisplay,
this.placeholder,
List<Sequence>? noResultFoundMessage,
Expand Down Expand Up @@ -209,16 +209,16 @@ final class Select<T> with Tools implements Component<T> {
} else {
copy.add(AsciiControl.lineFeed);

int start = currentIndex - displayElementCount + 1 >= 0
? currentIndex - displayElementCount + 1
int start = currentIndex - displayCount + 1 >= 0
? currentIndex - displayCount + 1
: 0;
if (currentIndex >= filteredArr.length &&
filteredArr.length > displayElementCount) {
start = filteredArr.length - displayElementCount;
filteredArr.length > displayCount) {
start = filteredArr.length - displayCount;
} else {}

int end = start + displayElementCount <= filteredArr.length
? start + displayElementCount
int end = start + displayCount <= filteredArr.length
? start + displayCount
: filteredArr.length;

for (int i = start; i < end; i++) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: commander_ui
description: Commander is a Dart library for creating user interfaces within the terminal.
version: 1.2.0
version: 1.3.0
repository: https://github.com/LeadcodeDev/commander

topics:
Expand Down

0 comments on commit 722e85a

Please sign in to comment.