Skip to content

Commit 94d20ff

Browse files
authored
LogDisplay.block support for writing fewer lines than previous block. (#4032)
1 parent 2fc09d9 commit 94d20ff

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

build_runner_core/lib/src/logging/ansi_buffer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AnsiBuffer {
2121
final List<String> lines = [];
2222

2323
/// The width that the buffer wraps to.
24-
int get width =>
24+
static int get width =>
2525
buildLog.configuration.forceConsoleWidthForTesting ??
2626
(stdout.hasTerminal ? stdout.terminalColumns : 80);
2727

@@ -40,7 +40,7 @@ class AnsiBuffer {
4040
/// In addition to ANSI codes, [nbsp] is a non-breaking space which will not
4141
/// be used for wrapping. In the buffer it is replaced with a normal space.
4242
void writeLine(List<String> items, {int indent = 0, int? hangingIndent}) {
43-
final width = this.width;
43+
final width = AnsiBuffer.width;
4444
hangingIndent ??= indent;
4545
indent = min(indent, width ~/ 2);
4646
hangingIndent = min(hangingIndent, width ~/ 2);

build_runner_core/lib/src/logging/log_display.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ class LogDisplay {
6060
// https://en.wikipedia.org/wiki/ANSI_escape_code#:~:text=Cursor%20Previous
6161
// Moves cursor to the beginning of the line n lines up.
6262
final moveCursor = _displayedLines == 0 ? '' : '\x1b[${_displayedLines}F';
63-
_displayedLines = lines.length;
6463
stdout.writeln('$moveCursor${lines.join('\n')}');
64+
if (_displayedLines > lines.length) {
65+
// If the block is smaller than last time, erase the rest of the display.
66+
// https://en.wikipedia.org/wiki/ANSI_escape_code#:~:text=Erase%20in%20Display
67+
stdout.write('\x1b[J');
68+
}
69+
_displayedLines = lines.length;
6570

6671
if (block.overflowsConsole) {
6772
prompt('Log overflowed the console, switching to line-by-line logging.');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:build_runner_core/src/logging/ansi_buffer.dart';
6+
import 'package:build_runner_core/src/logging/log_display.dart';
7+
8+
// TODO(davidmorgan): figure out how to make this a test.
9+
void main() async {
10+
final display = LogDisplay();
11+
display.block(
12+
AnsiBuffer()
13+
..writeLine(['start with'])
14+
..writeLine(['two lines']),
15+
);
16+
await Future<void>.delayed(const Duration(seconds: 2));
17+
display.block(AnsiBuffer()..writeLine(['now there should be one line']));
18+
await Future<void>.delayed(const Duration(seconds: 2));
19+
display.block(
20+
AnsiBuffer()
21+
..writeLine(['now'])
22+
..writeLine(['three'])
23+
..writeLine(['lines']),
24+
);
25+
await Future<void>.delayed(const Duration(seconds: 2));
26+
display.block(AnsiBuffer()..writeLine(['finally one line again']));
27+
}

0 commit comments

Comments
 (0)