Skip to content

Commit 7e6d00a

Browse files
committed
feat: add defaultValue property in input component
1 parent fdcf26d commit 7e6d00a

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.5.1
2+
3+
- Fix bad selected value when searching in `select` component
4+
- Fix hidden cursor on exit in `input` component
5+
16
## 1.5.0
27

38
- Add `defaultValue` property on `input` component

lib/src/components/input.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Input with Tools implements Component<String> {
4646
Print('✘'),
4747
SetStyles.reset,
4848
Print(' Operation canceled by user'),
49+
AsciiControl.lineFeed
4950
];
5051

5152
this.validate = validate ?? (value) => Ok(null);
@@ -108,6 +109,7 @@ class Input with Tools implements Component<String> {
108109
restoreCursorPosition();
109110
clearFromCursorToEnd();
110111
showInput();
112+
showCursor();
111113

112114
stdout.writeAnsiAll(exitMessage);
113115
exit(1);

lib/src/components/select.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ final class Select<T> with Tools implements Component<T> {
2222
late final List<Sequence> Function(String) selectedLineStyle;
2323
late final List<Sequence> Function(String) unselectedLineStyle;
2424

25+
List<T> _filteredArr = [];
26+
2527
final _completer = Completer<T>();
2628

2729
/// Creates a new instance of [Select].
@@ -47,6 +49,8 @@ final class Select<T> with Tools implements Component<T> {
4749
}) {
4850
StdinBuffer.initialize();
4951

52+
_filteredArr = options;
53+
5054
this.noResultFoundMessage = noResultFoundMessage ??
5155
[
5256
SetStyles(Style.foreground(Color.brightBlack)),
@@ -121,18 +125,20 @@ final class Select<T> with Tools implements Component<T> {
121125
}
122126

123127
void onSubmit(String key, void Function() dispose) {
128+
if (_filteredArr.isEmpty) return;
129+
124130
restoreCursorPosition();
125131
clearFromCursorToEnd();
126132
showInput();
127133

128134
dispose();
129135

130-
if (options.elementAtOrNull(currentIndex) == null) {
136+
if (_filteredArr.elementAtOrNull(currentIndex) == null) {
131137
throw Exception('No result found');
132138
}
133139

134-
final value = onDisplay?.call(options[currentIndex]) ??
135-
options[currentIndex].toString();
140+
final value = onDisplay?.call(_filteredArr[currentIndex]) ??
141+
_filteredArr[currentIndex].toString();
136142

137143
stdout.writeAnsiAll([
138144
SetStyles(Style.foreground(Color.green)),
@@ -148,7 +154,7 @@ final class Select<T> with Tools implements Component<T> {
148154

149155
saveCursorPosition();
150156
showCursor();
151-
_completer.complete(options[currentIndex]);
157+
_completer.complete(_filteredArr[currentIndex]);
152158
}
153159

154160
void onExit(void Function() dispose) {
@@ -157,6 +163,7 @@ final class Select<T> with Tools implements Component<T> {
157163
restoreCursorPosition();
158164
clearFromCursorToEnd();
159165
showInput();
166+
showCursor();
160167

161168
stdout.writeAnsiAll(exitMessage);
162169
exit(1);
@@ -183,7 +190,7 @@ final class Select<T> with Tools implements Component<T> {
183190
final buffer = StringBuffer();
184191
final List<Sequence> copy = [];
185192

186-
List<T> filteredArr = options.where((item) {
193+
_filteredArr = options.where((item) {
187194
final value = onDisplay?.call(item) ?? item.toString();
188195
return filter.isNotEmpty
189196
? value.toLowerCase().contains(filter.toLowerCase())
@@ -200,7 +207,7 @@ final class Select<T> with Tools implements Component<T> {
200207
SetStyles.reset,
201208
]);
202209

203-
if (filteredArr.isEmpty) {
210+
if (_filteredArr.isEmpty) {
204211
buffer.writeAnsiAll([
205212
AsciiControl.lineFeed,
206213
...noResultFoundMessage,
@@ -212,18 +219,18 @@ final class Select<T> with Tools implements Component<T> {
212219
int start = currentIndex - displayCount + 1 >= 0
213220
? currentIndex - displayCount + 1
214221
: 0;
215-
if (currentIndex >= filteredArr.length &&
216-
filteredArr.length > displayCount) {
217-
start = filteredArr.length - displayCount;
222+
if (currentIndex >= _filteredArr.length &&
223+
_filteredArr.length > displayCount) {
224+
start = _filteredArr.length - displayCount;
218225
} else {}
219226

220-
int end = start + displayCount <= filteredArr.length
227+
int end = start + displayCount <= _filteredArr.length
221228
? start + displayCount
222-
: filteredArr.length;
229+
: _filteredArr.length;
223230

224231
for (int i = start; i < end; i++) {
225232
final value =
226-
onDisplay?.call(filteredArr[i]) ?? filteredArr[i].toString();
233+
onDisplay?.call(_filteredArr[i]) ?? _filteredArr[i].toString();
227234
if (i == currentIndex) {
228235
copy.addAll([...selectedLineStyle(value), AsciiControl.lineFeed]);
229236
} else {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: commander_ui
22
description: Commander is a Dart library for creating user interfaces within the terminal.
3-
version: 1.5.0
3+
version: 1.5.1
44
repository: https://github.com/LeadcodeDev/commander
55

66
topics:

0 commit comments

Comments
 (0)