|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:commander_ui/src/application/terminals/terminal.dart'; |
| 5 | +import 'package:commander_ui/src/application/utils/terminal_tools.dart'; |
| 6 | +import 'package:commander_ui/src/domains/models/component.dart'; |
| 7 | +import 'package:commander_ui/src/io.dart'; |
| 8 | +import 'package:mansion/mansion.dart'; |
| 9 | + |
| 10 | +final class Swap<T> with TerminalTools implements Component<bool> { |
| 11 | + final _completer = Completer<bool>(); |
| 12 | + |
| 13 | + final Terminal _terminal; |
| 14 | + late bool _value; |
| 15 | + |
| 16 | + late final String _message; |
| 17 | + late final bool _defaultValue; |
| 18 | + late final String _placeholder; |
| 19 | + |
| 20 | + bool _keepAlive = true; |
| 21 | + |
| 22 | + Swap(this._terminal, |
| 23 | + {required String message, bool defaultValue = false, String placeholder = ''}) { |
| 24 | + _message = message; |
| 25 | + _placeholder = placeholder; |
| 26 | + _defaultValue = defaultValue; |
| 27 | + |
| 28 | + if (_defaultValue case bool value) { |
| 29 | + _value = value; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + Future<bool> handle() async { |
| 35 | + saveCursorPosition(); |
| 36 | + hideCursor(); |
| 37 | + |
| 38 | + _render(isInitialRender: true); |
| 39 | + |
| 40 | + while (_keepAlive) { |
| 41 | + final key = readKey(_terminal); |
| 42 | + |
| 43 | + if (key.controlChar == ControlCharacter.arrowLeft) { |
| 44 | + _value = true; |
| 45 | + _render(); |
| 46 | + } else if (key.controlChar == ControlCharacter.arrowRight) { |
| 47 | + _value = false; |
| 48 | + _render(); |
| 49 | + } else if ([ControlCharacter.ctrlJ, ControlCharacter.ctrlM].contains(key.controlChar)) { |
| 50 | + _onSubmit(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return _completer.future; |
| 55 | + } |
| 56 | + |
| 57 | + void _render({bool isInitialRender = false}) { |
| 58 | + restoreCursorPosition(); |
| 59 | + clearFromCursorToEnd(); |
| 60 | + |
| 61 | + final buffer = StringBuffer(); |
| 62 | + |
| 63 | + buffer.writeAnsiAll([ |
| 64 | + SetStyles(Style.foreground(Color.yellow)), |
| 65 | + Print('?'), |
| 66 | + SetStyles.reset, |
| 67 | + Print(' $_message'), |
| 68 | + if (_placeholder.isNotEmpty) ...[ |
| 69 | + Print(' : '), |
| 70 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 71 | + Print('($_placeholder)'), |
| 72 | + SetStyles.reset, |
| 73 | + ], |
| 74 | + Print(' : '), |
| 75 | + ]); |
| 76 | + |
| 77 | + final List<bool> values = [true, false]; |
| 78 | + |
| 79 | + for (final value in values) { |
| 80 | + buffer.writeAnsiAll([ |
| 81 | + SetStyles(Style.foreground(value == _value ? Color.reset : Color.brightBlack)), |
| 82 | + Print(value ? 'Yes' : ' No'), |
| 83 | + SetStyles.reset, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + buffer.writeAnsiAll([ |
| 88 | + AsciiControl.lineFeed, |
| 89 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 90 | + Print('(Type to filter, press ←/→ to navigate, enter to confirm)'), |
| 91 | + SetStyles.reset, |
| 92 | + ]); |
| 93 | + |
| 94 | + if (isInitialRender) { |
| 95 | + createSpace(_terminal, buffer.toString().split('\x0A').length); |
| 96 | + } |
| 97 | + |
| 98 | + stdout.write(buffer.toString()); |
| 99 | + } |
| 100 | + |
| 101 | + void _onSubmit() { |
| 102 | + restoreCursorPosition(); |
| 103 | + clearFromCursorToEnd(); |
| 104 | + showCursor(); |
| 105 | + |
| 106 | + final buffer = StringBuffer(); |
| 107 | + buffer.writeAnsiAll([ |
| 108 | + SetStyles(Style.foreground(Color.green)), |
| 109 | + Print('✔'), |
| 110 | + SetStyles.reset, |
| 111 | + Print(' $_message '), |
| 112 | + SetStyles(Style.foreground(Color.brightBlack)), |
| 113 | + Print(_value ? 'Yes' : 'No'), |
| 114 | + SetStyles.reset, |
| 115 | + AsciiControl.lineFeed, |
| 116 | + ]); |
| 117 | + |
| 118 | + stdout.write(buffer.toString()); |
| 119 | + |
| 120 | + _completer.complete(_value); |
| 121 | + _keepAlive = false; |
| 122 | + } |
| 123 | +} |
0 commit comments