|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:collection/collection.dart'; |
| 5 | +import 'package:commander_ui/commander_ui.dart'; |
| 6 | +import 'package:mansion/mansion.dart'; |
| 7 | + |
| 8 | +/// A class that represents a checkbox component. |
| 9 | +/// This component handles user selection from a list of options. |
| 10 | +final class Checkbox<T> with Tools implements Component<T> { |
| 11 | + int currentIndex = 0; |
| 12 | + bool isRendering = false; |
| 13 | + |
| 14 | + final List<T> options; |
| 15 | + final List<int> _selectedIndexes = []; |
| 16 | + final String answer; |
| 17 | + final String? placeholder; |
| 18 | + late final List<Sequence> noResultFoundMessage; |
| 19 | + late final List<Sequence> exitMessage; |
| 20 | + |
| 21 | + final String Function(T)? onDisplay; |
| 22 | + late final List<Sequence> Function(String) selectedLineStyle; |
| 23 | + late final List<Sequence> Function(String) unselectedLineStyle; |
| 24 | + late final List<Sequence> Function(String) highlightedSelectedLineStyle; |
| 25 | + late final List<Sequence> Function(String) highlightedUnselectedLineStyle; |
| 26 | + |
| 27 | + final _completer = Completer<List<T>>(); |
| 28 | + |
| 29 | + /// Creates a new instance of [Checkbox]. |
| 30 | + /// |
| 31 | + /// * The [answer] parameter is the question that the user is asked. |
| 32 | + /// * The [options] parameter is the list of options that the user can select from. |
| 33 | + /// * The [onDisplay] parameter is a function that transforms an option into a string for display. |
| 34 | + /// * The [placeholder] parameter is an optional placeholder for the input. |
| 35 | + /// * The [noResultFoundMessage] parameter is an optional message that is displayed when no results are found. |
| 36 | + /// * The [exitMessage] parameter is an optional message that is displayed when the user exits the input. |
| 37 | + /// * The [selectedLineStyle] parameter is a function that styles the selected line. |
| 38 | + /// * The [unselectedLineStyle] parameter is a function that styles the unselected line. |
| 39 | + /// * The [highlightedSelectedLineStyle] parameter is a function that styles the highlighted selected line. |
| 40 | + /// * The [highlightedUnselectedLineStyle] parameter is a function that styles the highlighted unselected line. |
| 41 | + Checkbox({ |
| 42 | + required this.answer, |
| 43 | + required this.options, |
| 44 | + this.onDisplay, |
| 45 | + this.placeholder, |
| 46 | + List<Sequence>? noResultFoundMessage, |
| 47 | + List<Sequence>? exitMessage, |
| 48 | + List<Sequence> Function(String)? selectedLineStyle, |
| 49 | + List<Sequence> Function(String)? unselectedLineStyle, |
| 50 | + List<Sequence> Function(String)? highlightedSelectedLineStyle, |
| 51 | + List<Sequence> Function(String)? highlightedUnselectedLineStyle, |
| 52 | + }) { |
| 53 | + StdinBuffer.initialize(); |
| 54 | + |
| 55 | + this.noResultFoundMessage = noResultFoundMessage ?? |
| 56 | + [ |
| 57 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 58 | + Print('No result found'), |
| 59 | + SetStyles.reset, |
| 60 | + ]; |
| 61 | + |
| 62 | + this.exitMessage = exitMessage ?? |
| 63 | + [ |
| 64 | + SetStyles(Style.foreground(Color.brightRed)), |
| 65 | + Print('✘'), |
| 66 | + SetStyles.reset, |
| 67 | + Print(' Operation canceled by user'), |
| 68 | + AsciiControl.lineFeed, |
| 69 | + ]; |
| 70 | + |
| 71 | + this.selectedLineStyle = selectedLineStyle ?? |
| 72 | + (line) => [ |
| 73 | + SetStyles(Style.foreground(Color.brightGreen)), |
| 74 | + Print('•'), |
| 75 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 76 | + Print(' $line'), |
| 77 | + SetStyles.reset, |
| 78 | + ]; |
| 79 | + |
| 80 | + this.unselectedLineStyle = unselectedLineStyle ?? |
| 81 | + (line) => [ |
| 82 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 83 | + Print('•'.padRight(2)), |
| 84 | + Print(line), |
| 85 | + SetStyles.reset, |
| 86 | + ]; |
| 87 | + |
| 88 | + this.highlightedSelectedLineStyle = highlightedSelectedLineStyle ?? |
| 89 | + (line) => [ |
| 90 | + SetStyles(Style.foreground(Color.brightGreen)), |
| 91 | + Print('•'), |
| 92 | + SetStyles.reset, |
| 93 | + Print(' $line'), |
| 94 | + ]; |
| 95 | + |
| 96 | + this.highlightedUnselectedLineStyle = highlightedUnselectedLineStyle ?? |
| 97 | + (line) => [ |
| 98 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 99 | + Print('•'), |
| 100 | + SetStyles.reset, |
| 101 | + Print(' $line'), |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + /// Handles the select component and returns a [Future] that completes with the result of the selection. |
| 106 | + Future<List<T>> handle() async { |
| 107 | + saveCursorPosition(); |
| 108 | + hideCursor(); |
| 109 | + hideInput(); |
| 110 | + |
| 111 | + KeyDownEventListener() |
| 112 | + ..match(AnsiCharacter.downArrow, onKeyDown) |
| 113 | + ..match(AnsiCharacter.upArrow, onKeyUp) |
| 114 | + ..match(AnsiCharacter.enter, onSubmit) |
| 115 | + ..match(AnsiCharacter.space, onSpace) |
| 116 | + ..onExit(onExit); |
| 117 | + |
| 118 | + render(); |
| 119 | + |
| 120 | + return _completer.future; |
| 121 | + } |
| 122 | + |
| 123 | + void onKeyDown(String key, void Function() dispose) { |
| 124 | + saveCursorPosition(); |
| 125 | + if (currentIndex != 0) { |
| 126 | + currentIndex = currentIndex - 1; |
| 127 | + } |
| 128 | + render(); |
| 129 | + } |
| 130 | + |
| 131 | + void onKeyUp(String key, void Function() dispose) { |
| 132 | + saveCursorPosition(); |
| 133 | + if (currentIndex < options.length - 1) { |
| 134 | + currentIndex = currentIndex + 1; |
| 135 | + } |
| 136 | + render(); |
| 137 | + } |
| 138 | + |
| 139 | + void onSubmit(String key, void Function() dispose) { |
| 140 | + restoreCursorPosition(); |
| 141 | + clearFromCursorToEnd(); |
| 142 | + showInput(); |
| 143 | + |
| 144 | + dispose(); |
| 145 | + |
| 146 | + if (options.elementAtOrNull(currentIndex) == null) { |
| 147 | + throw Exception('No result found'); |
| 148 | + } |
| 149 | + |
| 150 | + final value = onDisplay?.call(options[currentIndex]) ?? options[currentIndex].toString(); |
| 151 | + |
| 152 | + stdout.writeAnsiAll([ |
| 153 | + SetStyles(Style.foreground(Color.green)), |
| 154 | + Print('✔'), |
| 155 | + SetStyles.reset, |
| 156 | + Print(' $answer '), |
| 157 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 158 | + Print(value), |
| 159 | + SetStyles.reset |
| 160 | + ]); |
| 161 | + |
| 162 | + stdout.writeln(); |
| 163 | + |
| 164 | + saveCursorPosition(); |
| 165 | + showCursor(); |
| 166 | + |
| 167 | + final selectedOptions = |
| 168 | + options.whereIndexed((index, _) => _selectedIndexes.contains(index)).toList(); |
| 169 | + _completer.complete(selectedOptions); |
| 170 | + } |
| 171 | + |
| 172 | + void onExit(void Function() dispose) { |
| 173 | + dispose(); |
| 174 | + |
| 175 | + restoreCursorPosition(); |
| 176 | + clearFromCursorToEnd(); |
| 177 | + showInput(); |
| 178 | + |
| 179 | + stdout.writeAnsiAll(exitMessage); |
| 180 | + exit(1); |
| 181 | + } |
| 182 | + |
| 183 | + void onSpace(String key, void Function() dispose) { |
| 184 | + saveCursorPosition(); |
| 185 | + |
| 186 | + if (_selectedIndexes.contains(currentIndex)) { |
| 187 | + _selectedIndexes.remove(currentIndex); |
| 188 | + } else { |
| 189 | + _selectedIndexes.add(currentIndex); |
| 190 | + } |
| 191 | + |
| 192 | + render(); |
| 193 | + } |
| 194 | + |
| 195 | + void render() async { |
| 196 | + isRendering = true; |
| 197 | + |
| 198 | + saveCursorPosition(); |
| 199 | + |
| 200 | + final buffer = StringBuffer(); |
| 201 | + final List<Sequence> copy = []; |
| 202 | + |
| 203 | + buffer.writeAnsiAll([ |
| 204 | + SetStyles(Style.foreground(Color.yellow)), |
| 205 | + Print('?'), |
| 206 | + SetStyles.reset, |
| 207 | + Print(' $answer : '), |
| 208 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 209 | + Print(placeholder ?? ''), |
| 210 | + SetStyles.reset, |
| 211 | + ]); |
| 212 | + |
| 213 | + copy.add(AsciiControl.lineFeed); |
| 214 | + |
| 215 | + for (int i = 0; i < options.length; i++) { |
| 216 | + final value = onDisplay?.call(options[i]) ?? options[i].toString(); |
| 217 | + |
| 218 | + if (_selectedIndexes.contains(i)) { |
| 219 | + if (currentIndex == i) { |
| 220 | + copy.addAll([...highlightedSelectedLineStyle(value), AsciiControl.lineFeed]); |
| 221 | + } else { |
| 222 | + copy.addAll([...selectedLineStyle(value), AsciiControl.lineFeed]); |
| 223 | + } |
| 224 | + } else { |
| 225 | + if (currentIndex == i) { |
| 226 | + copy.addAll([...highlightedUnselectedLineStyle(value), AsciiControl.lineFeed]); |
| 227 | + } else { |
| 228 | + copy.addAll([...unselectedLineStyle(value), AsciiControl.lineFeed]); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + while (copy.isNotEmpty) { |
| 234 | + buffer.writeAnsi(copy.removeAt(0)); |
| 235 | + } |
| 236 | + |
| 237 | + buffer.writeAnsiAll([ |
| 238 | + AsciiControl.lineFeed, |
| 239 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 240 | + Print('(Type to filter, press ↑/↓ to navigate, enter to select)'), |
| 241 | + SetStyles.reset, |
| 242 | + ]); |
| 243 | + |
| 244 | + final availableLines = await getAvailableLinesBelowCursor(); |
| 245 | + final linesNeeded = buffer.toString().split('\n').length; |
| 246 | + |
| 247 | + if (availableLines < linesNeeded) { |
| 248 | + moveCursorUp(count: linesNeeded - availableLines); |
| 249 | + saveCursorPosition(); |
| 250 | + clearFromCursorToEnd(); |
| 251 | + } |
| 252 | + |
| 253 | + clearFromCursorToEnd(); |
| 254 | + restoreCursorPosition(); |
| 255 | + saveCursorPosition(); |
| 256 | + |
| 257 | + stdout.write(buffer.toString()); |
| 258 | + |
| 259 | + restoreCursorPosition(); |
| 260 | + |
| 261 | + isRendering = false; |
| 262 | + } |
| 263 | +} |
0 commit comments