diff --git a/packages/flutter_tools/lib/src/base/terminal.dart b/packages/flutter_tools/lib/src/base/terminal.dart index ed2e0ac650..eb9bfdb1a8 100644 --- a/packages/flutter_tools/lib/src/base/terminal.dart +++ b/packages/flutter_tools/lib/src/base/terminal.dart @@ -9,15 +9,6 @@ import '../globals.dart'; import 'context.dart'; import 'io.dart' as io; import 'platform.dart'; -import 'utils.dart'; - -final AnsiTerminal _kAnsiTerminal = AnsiTerminal(); - -AnsiTerminal get terminal { - return (context == null || context.get() == null) - ? _kAnsiTerminal - : context.get(); -} enum TerminalColor { red, @@ -29,11 +20,15 @@ enum TerminalColor { grey, } -final OutputPreferences _kOutputPreferences = OutputPreferences(); +AnsiTerminal get terminal { + return context?.get() ?? _defaultAnsiTerminal; +} +final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal(); -OutputPreferences get outputPreferences => (context == null || context.get() == null) - ? _kOutputPreferences - : context.get(); +OutputPreferences get outputPreferences { + return context?.get() ?? _defaultOutputPreferences; +} +final OutputPreferences _defaultOutputPreferences = OutputPreferences(); /// A class that contains the context settings for command text output to the /// console. @@ -42,31 +37,32 @@ class OutputPreferences { bool wrapText, int wrapColumn, bool showColor, - }) : wrapText = wrapText ?? io.stdio?.hasTerminal ?? const io.Stdio().hasTerminal, + }) : wrapText = wrapText ?? io.stdio.hasTerminal, _overrideWrapColumn = wrapColumn, showColor = showColor ?? platform.stdoutSupportsAnsi ?? false; + /// A version of this class for use in tests. + OutputPreferences.test() : wrapText = false, _overrideWrapColumn = null, showColor = false; + /// If [wrapText] is true, then any text sent to the context's [Logger] /// instance (e.g. from the [printError] or [printStatus] functions) will be /// wrapped (newlines added between words) to be no longer than the /// [wrapColumn] specifies. Defaults to true if there is a terminal. To /// determine if there's a terminal, [OutputPreferences] asks the context's - /// stdio to see, and if that's not set, it tries creating a new [io.Stdio] - /// and asks it if there is a terminal. + /// stdio. final bool wrapText; + /// The terminal width used by the [wrapText] function if there is no terminal + /// attached to [io.Stdio], --wrap is on, and --wrap-columns was not specified. + static const int kDefaultTerminalColumns = 100; + /// The column at which output sent to the context's [Logger] instance /// (e.g. from the [printError] or [printStatus] functions) will be wrapped. /// Ignored if [wrapText] is false. Defaults to the width of the output /// terminal, or to [kDefaultTerminalColumns] if not writing to a terminal. - /// To find out if we're writing to a terminal, it tries the context's stdio, - /// and if that's not set, it tries creating a new [io.Stdio] and asks it, if - /// that doesn't have an idea of the terminal width, then we just use a - /// default of 100. It will be ignored if [wrapText] is false. final int _overrideWrapColumn; int get wrapColumn { - return _overrideWrapColumn ?? io.stdio?.terminalColumns - ?? const io.Stdio().terminalColumns ?? kDefaultTerminalColumns; + return _overrideWrapColumn ?? io.stdio.terminalColumns ?? kDefaultTerminalColumns; } /// Whether or not to output ANSI color codes when writing to the output diff --git a/packages/flutter_tools/lib/src/base/utils.dart b/packages/flutter_tools/lib/src/base/utils.dart index 382060d95a..08db07cd4f 100644 --- a/packages/flutter_tools/lib/src/base/utils.dart +++ b/packages/flutter_tools/lib/src/base/utils.dart @@ -307,9 +307,6 @@ class Poller { Future> waitGroup(Iterable> futures) { return Future.wait(futures.where((Future future) => future != null)); } -/// The terminal width used by the [wrapText] function if there is no terminal -/// attached to [io.Stdio], --wrap is on, and --wrap-columns was not specified. -const int kDefaultTerminalColumns = 100; /// Smallest column that will be used for text wrapping. If the requested column /// width is smaller than this, then this is what will be used. diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index c3519b1a2f..0ecd81ef67 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -82,7 +82,7 @@ void testUsingContext( when(mock.getAttachedDevices()).thenAnswer((Invocation _) async => []); return mock; }, - OutputPreferences: () => OutputPreferences(showColor: false), + OutputPreferences: () => OutputPreferences.test(), Logger: () => BufferLogger(), OperatingSystemUtils: () => FakeOperatingSystemUtils(), SimControl: () => MockSimControl(), diff --git a/packages/flutter_tools/test/src/testbed.dart b/packages/flutter_tools/test/src/testbed.dart index 4c2fae1378..b331604586 100644 --- a/packages/flutter_tools/test/src/testbed.dart +++ b/packages/flutter_tools/test/src/testbed.dart @@ -33,7 +33,7 @@ final Map _testbedDefaults = { FileSystem: () => MemoryFileSystem(style: platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix), Logger: () => BufferLogger(), // Allows reading logs and prevents stdout. OperatingSystemUtils: () => FakeOperatingSystemUtils(), - OutputPreferences: () => OutputPreferences(showColor: false), // configures BufferLogger to avoid color codes. + OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes. Usage: () => NoOpUsage(), // prevent addition of analytics from burdening test mocks FlutterVersion: () => FakeFlutterVersion(), // prevent requirement to mock git for test runner. Signals: () => FakeSignals(), // prevent registering actual signal handlers.