-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleWriter.cs
111 lines (101 loc) · 3.14 KB
/
ConsoleWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
namespace AndroidUITestFramework
{
public class ConsoleWriter : StringWriter
{
Stack<ConsoleColor> foregroundStack;
Stack<ConsoleColor> backgroundStack;
static bool modifyColors = true;
bool wasTrue;
static LinkedList<System.IO.TextWriter> CURRENT = new();
public ConsoleWriter() : base()
{
foregroundStack = new Stack<ConsoleColor>();
backgroundStack = new Stack<ConsoleColor>();
foregroundStack.Push(Console.ForegroundColor);
backgroundStack.Push(Console.BackgroundColor);
wasTrue = false;
if (CURRENT.Count == 0)
{
CURRENT.AddLast(Console.Out);
}
CURRENT.AddLast(this);
Console.SetOut(this);
}
~ConsoleWriter()
{
Dispose(true);
}
protected override void Dispose(bool disposing)
{
if (CURRENT.Last.Value == this)
{
CURRENT.RemoveLast();
Console.SetOut(CURRENT.Last.Value);
Console.WriteLine("RESTORED OUT");
}
else
{
// we are not current, but we exist inside the CURRENT stack
CURRENT.Remove(this);
}
base.Dispose(disposing);
}
public void pushForegroundColor(ConsoleColor color)
{
foregroundStack.Push(color);
}
public void popForegroundColor()
{
if (foregroundStack.Count > 1)
{
foregroundStack.Pop();
}
}
public void pushBackgroundColor(ConsoleColor color)
{
backgroundStack.Push(color);
}
public void popBackgroundColor()
{
if (backgroundStack.Count > 1)
{
backgroundStack.Pop();
}
}
public override void Flush()
{
ConsoleColor oldF = ConsoleColor.Gray;
ConsoleColor oldB = ConsoleColor.Black;
bool color_changed = false;
if (modifyColors) {
modifyColors = false;
wasTrue = true;
oldF = Console.ForegroundColor;
oldB = Console.BackgroundColor;
ConsoleColor newF = foregroundStack.Peek();
ConsoleColor newB = backgroundStack.Peek();
color_changed = oldF != newF || oldB != newB;
if (color_changed) {
Console.ForegroundColor = newF;
Console.BackgroundColor = newB;
}
}
Console.SetOut(CURRENT.Find(this).Previous.Value);
Console.Write(base.ToString());
if (color_changed)
{
Console.BackgroundColor = oldB;
Console.ForegroundColor = oldF;
}
if (wasTrue)
{
wasTrue = false;
modifyColors = true;
Console.SetOut(this);
}
base.Flush();
}
}
}