@@ -22,6 +22,8 @@ final class Select<T> with Tools implements Component<T> {
22
22
late final List <Sequence > Function (String ) selectedLineStyle;
23
23
late final List <Sequence > Function (String ) unselectedLineStyle;
24
24
25
+ List <T > _filteredArr = [];
26
+
25
27
final _completer = Completer <T >();
26
28
27
29
/// Creates a new instance of [Select] .
@@ -47,6 +49,8 @@ final class Select<T> with Tools implements Component<T> {
47
49
}) {
48
50
StdinBuffer .initialize ();
49
51
52
+ _filteredArr = options;
53
+
50
54
this .noResultFoundMessage = noResultFoundMessage ??
51
55
[
52
56
SetStyles (Style .foreground (Color .brightBlack)),
@@ -121,18 +125,20 @@ final class Select<T> with Tools implements Component<T> {
121
125
}
122
126
123
127
void onSubmit (String key, void Function () dispose) {
128
+ if (_filteredArr.isEmpty) return ;
129
+
124
130
restoreCursorPosition ();
125
131
clearFromCursorToEnd ();
126
132
showInput ();
127
133
128
134
dispose ();
129
135
130
- if (options .elementAtOrNull (currentIndex) == null ) {
136
+ if (_filteredArr .elementAtOrNull (currentIndex) == null ) {
131
137
throw Exception ('No result found' );
132
138
}
133
139
134
- final value = onDisplay? .call (options [currentIndex]) ??
135
- options [currentIndex].toString ();
140
+ final value = onDisplay? .call (_filteredArr [currentIndex]) ??
141
+ _filteredArr [currentIndex].toString ();
136
142
137
143
stdout.writeAnsiAll ([
138
144
SetStyles (Style .foreground (Color .green)),
@@ -148,7 +154,7 @@ final class Select<T> with Tools implements Component<T> {
148
154
149
155
saveCursorPosition ();
150
156
showCursor ();
151
- _completer.complete (options [currentIndex]);
157
+ _completer.complete (_filteredArr [currentIndex]);
152
158
}
153
159
154
160
void onExit (void Function () dispose) {
@@ -157,6 +163,7 @@ final class Select<T> with Tools implements Component<T> {
157
163
restoreCursorPosition ();
158
164
clearFromCursorToEnd ();
159
165
showInput ();
166
+ showCursor ();
160
167
161
168
stdout.writeAnsiAll (exitMessage);
162
169
exit (1 );
@@ -183,7 +190,7 @@ final class Select<T> with Tools implements Component<T> {
183
190
final buffer = StringBuffer ();
184
191
final List <Sequence > copy = [];
185
192
186
- List < T > filteredArr = options.where ((item) {
193
+ _filteredArr = options.where ((item) {
187
194
final value = onDisplay? .call (item) ?? item.toString ();
188
195
return filter.isNotEmpty
189
196
? value.toLowerCase ().contains (filter.toLowerCase ())
@@ -200,7 +207,7 @@ final class Select<T> with Tools implements Component<T> {
200
207
SetStyles .reset,
201
208
]);
202
209
203
- if (filteredArr .isEmpty) {
210
+ if (_filteredArr .isEmpty) {
204
211
buffer.writeAnsiAll ([
205
212
AsciiControl .lineFeed,
206
213
...noResultFoundMessage,
@@ -212,18 +219,18 @@ final class Select<T> with Tools implements Component<T> {
212
219
int start = currentIndex - displayCount + 1 >= 0
213
220
? currentIndex - displayCount + 1
214
221
: 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;
218
225
} else {}
219
226
220
- int end = start + displayCount <= filteredArr .length
227
+ int end = start + displayCount <= _filteredArr .length
221
228
? start + displayCount
222
- : filteredArr .length;
229
+ : _filteredArr .length;
223
230
224
231
for (int i = start; i < end; i++ ) {
225
232
final value =
226
- onDisplay? .call (filteredArr [i]) ?? filteredArr [i].toString ();
233
+ onDisplay? .call (_filteredArr [i]) ?? _filteredArr [i].toString ();
227
234
if (i == currentIndex) {
228
235
copy.addAll ([...selectedLineStyle (value), AsciiControl .lineFeed]);
229
236
} else {
0 commit comments