Skip to content

Commit 5cbfd94

Browse files
committed
feat(listener): add exit terminal cleanup
1 parent 9444a58 commit 5cbfd94

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

lib/src/application/components/ask.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ final class Ask<T> with TerminalTools implements Component<Future<T>> {
2727
SetStyles(Style.foreground(Color.brightBlack)),
2828
Print(' ($_defaultValue)'),
2929
SetStyles.reset,
30-
Print(' :'),
3130
];
3231
}
3332

@@ -112,13 +111,16 @@ final class Ask<T> with TerminalTools implements Component<Future<T>> {
112111
SetStyles(Style.foreground(Color.brightRed)),
113112
Print(error),
114113
SetStyles.reset,
114+
CursorPosition.restore,
115+
Clear.afterCursor,
115116
]);
116117

117-
resetCursor();
118-
119-
buffer.writeAnsi(
118+
buffer.writeAnsiAll([
119+
CursorPosition.restore,
120+
Clear.afterCursor,
120121
SetStyles(Style.foreground(Color.brightBlack)),
121-
);
122+
]);
123+
122124
stdout.write(buffer.toString());
123125

124126
stdout.writeAnsiAll([
@@ -147,16 +149,14 @@ final class Ask<T> with TerminalTools implements Component<Future<T>> {
147149
Print(_hidden ? '******' : response),
148150
SetStyles.reset,
149151
AsciiControl.lineFeed,
152+
CursorPosition.restore,
153+
Clear.afterCursor,
150154
]);
151155

152-
resetCursor();
153156
stdout.write(buffer.toString());
154157

155158
_completer.complete(response as T);
156159
}
157-
158-
void resetCursor() {
159-
restoreCursorPosition();
160-
clearFromCursorToEnd();
161-
}
162160
}
161+
162+

lib/src/application/components/checkbox.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ final class Checkbox<T>
5454
_render(isInitialRender: true);
5555

5656
while (_keepAlive) {
57-
final key = readKey(_terminal);
57+
final key = readKey(_terminal, onExit: () {
58+
stdout.writeAnsiAll([
59+
CursorVisibility.show,
60+
CursorPosition.restore,
61+
Clear.afterCursor,
62+
]);
63+
});
5864

5965
if (key.controlChar == ControlCharacter.arrowUp || key.char == 'k') {
6066
if (_currentIndex != 0) {

lib/src/application/components/select.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ final class Select<T> with TerminalTools implements Component<Future<T>> {
5252
_render(isInitialRender: true);
5353

5454
while (_selectedOption == null) {
55-
final key = readKey(_terminal);
55+
final key = readKey(_terminal, onExit: () {
56+
stdout.writeAnsiAll([
57+
CursorVisibility.show,
58+
CursorPosition.restore,
59+
Clear.afterCursor,
60+
]);
61+
});
5662

5763
if (key.controlChar == ControlCharacter.arrowUp || key.char == 'k') {
5864
if (_currentIndex != 0) {
@@ -107,7 +113,7 @@ final class Select<T> with TerminalTools implements Component<Future<T>> {
107113
SetStyles(Style.foreground(Color.yellow)),
108114
Print('?'),
109115
SetStyles.reset,
110-
Print(' $_message : '),
116+
Print(' $_message '),
111117
SetStyles(Style.foreground(Color.brightBlack)),
112118
Print(_filter.isEmpty ? _placeholder : _filter),
113119
SetStyles.reset,

lib/src/application/components/swap.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ final class Swap<T> with TerminalTools implements Component<Future<bool>> {
4141
_render(isInitialRender: true);
4242

4343
while (_keepAlive) {
44-
final key = readKey(_terminal);
44+
final key = readKey(_terminal, onExit: () {
45+
stdout.writeAnsiAll([
46+
CursorVisibility.show,
47+
CursorPosition.restore,
48+
Clear.afterCursor,
49+
]);
50+
});
4551

4652
if (key.controlChar == ControlCharacter.arrowLeft) {
4753
_value = true;

lib/src/application/utils/terminal_tools.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,16 @@ mixin TerminalTools {
109109
if (!stdout.hasTerminal) throw NoTerminalAttachedError();
110110
}
111111

112-
KeyStroke readKey(Terminal terminal) {
112+
KeyStroke readKey(Terminal terminal, {Function()? onExit}) {
113113
_ensureTerminalAttached();
114114
terminal.enableRawMode();
115115
final key = _readKey();
116+
print(key.controlChar);
116117
terminal.disableRawMode();
117118

118119
if (key.controlChar == ControlCharacter.ctrlC) {
119-
showCursor();
120+
stdout.writeAnsi(CursorVisibility.show);
121+
onExit?.call();
120122
exit(130);
121123
}
122124
return key;

0 commit comments

Comments
 (0)