Skip to content

Commit 991a0e2

Browse files
committed
feat(swap): implement swap component
1 parent e441617 commit 991a0e2

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

example/swap.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:commander_ui/src/commander.dart';
2+
import 'package:commander_ui/src/level.dart';
3+
4+
enum Shape { square, circle, triangle }
5+
6+
Future<void> main() async {
7+
final commander = Commander(level: Level.verbose);
8+
print('Hello World !');
9+
10+
final value = await commander.swap(
11+
'What is your name ?',
12+
defaultValue: true,
13+
);
14+
15+
print(value);
16+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

lib/src/commander.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io' as io;
44
import 'package:commander_ui/src/application/components/ask.dart';
55
import 'package:commander_ui/src/application/components/checkbox.dart';
66
import 'package:commander_ui/src/application/components/select.dart';
7+
import 'package:commander_ui/src/application/components/swap.dart';
78
import 'package:commander_ui/src/application/terminals/terminal.dart';
89
import 'package:commander_ui/src/application/utils/terminal_tools.dart';
910
import 'package:commander_ui/src/level.dart';
@@ -71,4 +72,8 @@ class Commander with TerminalTools {
7172
multiple: multiple,
7273
onDisplay: onDisplay)
7374
.handle();
75+
76+
Future<bool> swap<T>(String message, {bool defaultValue = false, String placeholder = ''}) =>
77+
Swap<T>(_terminal, message: message, defaultValue: defaultValue, placeholder: placeholder)
78+
.handle();
7479
}

0 commit comments

Comments
 (0)