Add option to clear the terminal by pressing "c" (#50034)

This commit is contained in:
Levi Lesches
2020-02-06 18:53:04 -05:00
committed by GitHub
parent a9b8d360cf
commit 149e06232b
9 changed files with 49 additions and 5 deletions

View File

@@ -53,6 +53,9 @@ class CommandHelp {
CommandHelpOption _a;
CommandHelpOption get a => _a ??= _makeOption('a', 'Toggle timeline events for all widget build methods.', 'debugProfileWidgetBuilds');
CommandHelpOption _c;
CommandHelpOption get c => _c ??= _makeOption('c', 'Clear the screen');
CommandHelpOption _d;
CommandHelpOption get d => _d ??= _makeOption('d', 'Detach (terminate "flutter run" but leave application running).');

View File

@@ -168,6 +168,9 @@ abstract class Logger {
/// Only surfaces a value in machine modes, Loggers may ignore this message in
/// non-machine modes.
void sendEvent(String name, [Map<String, dynamic> args]) { }
/// Clears all output.
void clear();
}
class StdoutLogger extends Logger {
@@ -326,6 +329,13 @@ class StdoutLogger extends Logger {
@override
void sendEvent(String name, [Map<String, dynamic> args]) { }
@override
void clear() {
_status?.pause();
writeToStdOut(_terminal.clearScreen() + '\n');
_status?.resume();
}
}
/// A [StdoutLogger] which replaces Unicode characters that cannot be printed to
@@ -470,7 +480,7 @@ class BufferLogger extends Logger {
)..start();
}
/// Clears all buffers.
@override
void clear() {
_error.clear();
_status.clear();
@@ -627,6 +637,9 @@ class VerboseLogger extends Logger {
@override
bool get hasTerminal => parent.hasTerminal;
@override
void clear() => parent.clear();
}
enum _LogType { error, status, trace }

View File

@@ -1000,6 +1000,10 @@ class NotifyingLogger extends Logger {
@override
bool get hasTerminal => false;
// This method is only relevant for terminals.
@override
void clear() { }
}
/// A running application, started by this daemon.
@@ -1230,6 +1234,10 @@ class _AppRunLogger extends Logger {
@override
bool get hasTerminal => false;
// This method is only relevant for terminals.
@override
void clear() { }
}
class LogMessage {

View File

@@ -1052,9 +1052,6 @@ abstract class ResidentRunner {
commandHelp.P.print();
commandHelp.a.print();
}
if (flutterDevices.any((FlutterDevice d) => d.device.supportsScreenshot)) {
commandHelp.s.print();
}
}
/// Called when a signal has requested we exit.
@@ -1062,6 +1059,9 @@ abstract class ResidentRunner {
/// Called right before we exit.
Future<void> cleanupAtFinish();
// Clears the screen.
void clearScreen() => globals.logger.clear();
}
class OperationResult {
@@ -1167,6 +1167,9 @@ class TerminalHandler {
return true;
}
return false;
case 'c':
residentRunner.clearScreen();
return true;
case 'd':
case 'D':
await residentRunner.detach();

View File

@@ -189,6 +189,7 @@ class ColdRunner extends ResidentRunner {
if (_didAttach) {
commandHelp.d.print();
}
commandHelp.c.print();
commandHelp.q.print();
for (final FlutterDevice device in flutterDevices) {
final String dname = device.device.name;

View File

@@ -1053,6 +1053,7 @@ class HotRunner extends ResidentRunner {
if (_didAttach) {
commandHelp.d.print();
}
commandHelp.c.print();
commandHelp.q.print();
if (details) {
printHelpDetails();

View File

@@ -737,6 +737,9 @@ class StreamLogger extends Logger {
@override
bool get hasTerminal => false;
@override
void clear() => _log('[stdout] ${globals.terminal.clearScreen()}\n');
}
class LoggerInterrupted implements Exception {

View File

@@ -377,6 +377,7 @@ void main() {
commandHelp.r,
commandHelp.R,
commandHelp.h,
commandHelp.c,
commandHelp.q,
commandHelp.s,
commandHelp.w,
@@ -390,7 +391,6 @@ void main() {
commandHelp.z,
commandHelp.P,
commandHelp.a,
commandHelp.s,
'An Observatory debugger and profiler on null is available at: null',
''
].join('\n')
@@ -423,6 +423,15 @@ void main() {
expect(testLogger.errorText, contains('Error'));
}));
test('ResidentTunner clears the screen when it should', () => testbed.run(() async {
const String message = 'This should be cleared';
expect(testLogger.statusText, equals(''));
testLogger.printStatus(message);
expect(testLogger.statusText, equals(message + '\n')); // printStatus makes a newline
residentRunner.clearScreen();
expect(testLogger.statusText, equals(''));
}));
test('ResidentRunner bails taking screenshot on debug device if debugAllowBanner throws post', () => testbed.run(() async {
when(mockDevice.supportsScreenshot).thenReturn(true);
when(mockIsolate.flutterDebugAllowBanner(true)).thenThrow(Exception());

View File

@@ -822,6 +822,9 @@ class DelegateLogger implements Logger {
@override
bool get supportsColor => delegate.supportsColor;
@override
void clear() => delegate.clear();
}
/// An implementation of the Cache which does not download or require locking.