@@ -5,12 +5,22 @@ import 'package:ansi_slice/ansi_slice.dart';
5
5
import 'package:ansi_strip/ansi_strip.dart' ;
6
6
import 'package:ansi_wrap/ansi_wrap.dart' ;
7
7
8
- int getWidth (IOSink stream) {
9
- return stdout.terminalColumns;
8
+ int getWidth (Stdout sink) {
9
+ if (sink.hasTerminal) {
10
+ return sink.terminalColumns;
11
+ }
12
+ return 80 ;
13
+ }
14
+
15
+ int getHeight (Stdout sink) {
16
+ if (sink.hasTerminal) {
17
+ return sink.terminalLines;
18
+ }
19
+ return 24 ;
10
20
}
11
21
12
- String fitToTerminalHeight (IOSink stream , String text) {
13
- final terminalHeight = stdout.terminalLines ;
22
+ String fitToTerminalHeight (Stdout sink , String text) {
23
+ final terminalHeight = getHeight (sink) ;
14
24
final lines = text.split ('\n ' );
15
25
16
26
final toRemove = lines.length - terminalHeight;
@@ -24,7 +34,7 @@ String fitToTerminalHeight(IOSink stream, String text) {
24
34
);
25
35
}
26
36
27
- Render createLogUpdate (IOSink stream, {bool showCursor = true }) {
37
+ Render createLogUpdate (Stdout stream, {bool showCursor = true }) {
28
38
return Render (stream, showCursor);
29
39
}
30
40
@@ -35,41 +45,41 @@ class Render {
35
45
int previousLineCount = 0 ;
36
46
int previousWidth;
37
47
String previousOutput = '' ;
38
- final IOSink stream ;
48
+ final Stdout sink ;
39
49
final bool showCursor;
40
50
41
- Render (this .stream , this .showCursor) : previousWidth = getWidth (stream );
51
+ Render (this .sink , this .showCursor) : previousWidth = getWidth (sink );
42
52
43
53
void call (String output) {
44
54
if (! showCursor) {
45
- stream .write (ansiEscapes.cursorHide);
55
+ sink .write (ansiEscapes.cursorHide);
46
56
}
47
- output = fitToTerminalHeight (stream , output);
48
- final width = getWidth (stream );
57
+ output = fitToTerminalHeight (sink , output);
58
+ final width = getWidth (sink );
49
59
if (output == previousOutput && previousWidth == width) {
50
60
return ;
51
61
}
52
62
53
63
previousOutput = output;
54
64
previousWidth = width;
55
65
output = wrapAnsi (output, width, trim: false , hard: true , wordWrap: false );
56
- stream .write (ansiEscapes.eraseLines (previousLineCount) + output);
66
+ sink .write (ansiEscapes.eraseLines (previousLineCount) + output);
57
67
previousLineCount = output.split ('\n ' ).length;
58
68
}
59
69
60
70
void clear () {
61
- stream .write (ansiEscapes.eraseLines (previousLineCount));
71
+ sink .write (ansiEscapes.eraseLines (previousLineCount));
62
72
previousOutput = '' ;
63
- previousWidth = getWidth (stream );
73
+ previousWidth = getWidth (sink );
64
74
previousLineCount = 0 ;
65
75
}
66
76
67
77
void done () {
68
78
previousOutput = '' ;
69
- previousWidth = getWidth (stream );
79
+ previousWidth = getWidth (sink );
70
80
previousLineCount = 0 ;
71
81
if (! showCursor) {
72
- stream .write (ansiEscapes.cursorShow);
82
+ sink .write (ansiEscapes.cursorShow);
73
83
}
74
84
}
75
85
}
0 commit comments