Skip to content

Commit a29cda2

Browse files
committed
support redirected status lines
1 parent 2c4e620 commit a29cda2

1 file changed

Lines changed: 100 additions & 63 deletions

File tree

Extensions/Log.cs

Lines changed: 100 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,23 @@ public static Line AddStatusLine(string _text = "", string? _prefix = null, floa
130130
var newLine = new Line(_prefix, _text, _progress);
131131
statusLines.Add(newLine);
132132

133-
using (var _ = new ConsoleStateRestorer())
133+
if (VTEnabled)
134134
{
135-
if (statusLines.Count == 1)
136-
Console.Write($"\e[;r\e[1;H\e[1L\e[1;r\e[;r\e[2;H\e[1L\e[2;r");
135+
using (var _ = new ConsoleStateRestorer())
136+
{
137+
if (statusLines.Count == 1)
138+
Console.Write($"\e[;r\e[1;H\e[1L\e[1;r\e[;r\e[2;H\e[1L\e[2;r");
137139

138-
Console.Write($"\e[;r\e[{2 + statusLines.Count};H\e[1L\e[{3 + statusLines.Count};r");
139-
UpdateStatusLines();
140+
Console.Write($"\e[;r\e[{2 + statusLines.Count};H\e[1L\e[{3 + statusLines.Count};r");
141+
UpdateStatusLines();
142+
}
143+
144+
if (statusLines.Count == 1)
145+
Console.Write("\e[3B");
146+
else
147+
Console.Write("\e[B");
140148
}
141149

142-
if (statusLines.Count == 1)
143-
Console.Write("\e[3B");
144-
else
145-
Console.Write("\e[B");
146150
return newLine;
147151
}
148152
}
@@ -195,51 +199,7 @@ void WriteLine(int row, string text)
195199
using var _ = new ConsoleStateRestorer();
196200
WriteLine(-1, "");
197201
for (var i = 0; i < statusLines.Count; i++)
198-
{
199-
string progress = "";
200-
if (statusLines[i].Progress is float p)
201-
{
202-
var barFilled = (int)Math.Round(p * barWidth);
203-
204-
var firstPart = $"{p,4:P0} {new string('━', barFilled)}";
205-
206-
var secondPart = new string('━', barWidth - barFilled);
207-
208-
if (statusLines[i].Active)
209-
{
210-
if (p >= 0.995)
211-
firstPart = firstPart.StyleBrightGreen();
212-
else
213-
firstPart = firstPart.StyleOrange();
214-
}
215-
else
216-
{
217-
if (p >= 0.995)
218-
firstPart = firstPart.StyleDarkGreen();
219-
else
220-
firstPart = firstPart.StyleDarkYellow();
221-
secondPart = secondPart.StyleDarkGray();
222-
}
223-
224-
progress = $"{firstPart}{secondPart} ";
225-
}
226-
227-
string text;
228-
if (statusLines[i].Active)
229-
{
230-
text = $"{progress}{statusLines[i].Text}";
231-
if (statusLines[i].Prefix is not null)
232-
text = $"{statusLines[i].Prefix}: {text}";
233-
}
234-
else
235-
{
236-
text = $"{progress}{statusLines[i].Text.StyleDarkGray()}";
237-
if (statusLines[i].Prefix is not null)
238-
text = $"{statusLines[i].Prefix.StyleDarkGray()}: {text}";
239-
}
240-
241-
WriteLine(i, $" {text} ");
242-
}
202+
WriteLine(i, $" {statusLines[i].GetConsoleText(barWidth)} ");
243203

244204
WriteLine(statusLines.Count, "");
245205
}
@@ -254,30 +214,91 @@ public class Line : IDisposable
254214
private System.Threading.Timer? RemovalTimer;
255215

256216
private static volatile System.Threading.Timer? UpdateTimer = null;
217+
private volatile System.Threading.Timer? UpdateLineTimer = null;
257218

258219
internal Line(string? _prefix, string _text, float? _progress)
259220
{
260221
_Prefix = _prefix?.Replace('\n', ' ');
261222
_Text = _text.Replace('\n', ' ');
262223
_Progress = _progress;
224+
225+
if (!VTEnabled)
226+
AsyncUpdate();
263227
}
264228

265-
private static void AsyncUpdate()
229+
internal string GetConsoleText(int barWidth)
266230
{
267-
UpdateTimer ??= new System.Threading.Timer(_ =>
231+
string progress = "";
232+
if (Progress is float p)
268233
{
269-
UpdateTimer = null;
270-
UpdateStatusLines();
271-
}, null, 50, Timeout.Infinite);
234+
var barFilled = (int)Math.Round(p * barWidth);
235+
236+
var firstPart = $"{p,4:P0} {new string(VTEnabled ? '━' : '#', barFilled)}";
237+
238+
var secondPart = new string(VTEnabled ? '━' : '.', barWidth - barFilled);
239+
240+
if (Active)
241+
{
242+
if (p >= 0.995)
243+
firstPart = firstPart.StyleBrightGreen();
244+
else
245+
firstPart = firstPart.StyleOrange();
246+
}
247+
else
248+
{
249+
if (p >= 0.995)
250+
firstPart = firstPart.StyleDarkGreen();
251+
else
252+
firstPart = firstPart.StyleDarkYellow();
253+
secondPart = secondPart.StyleDarkGray();
254+
}
255+
256+
progress = $"{firstPart}{secondPart} ";
257+
}
258+
259+
string text;
260+
if (Active)
261+
{
262+
text = $"{progress}{Text}";
263+
if (Prefix is not null)
264+
text = $"{Prefix}: {text}";
265+
}
266+
else
267+
{
268+
text = $"{progress}{Text.StyleDarkGray()}";
269+
if (Prefix is not null)
270+
text = $"{Prefix.StyleDarkGray()}: {text}";
271+
}
272+
return text;
273+
}
274+
275+
private void AsyncUpdate()
276+
{
277+
if (VTEnabled)
278+
UpdateTimer ??= new System.Threading.Timer(_ =>
279+
{
280+
UpdateTimer = null;
281+
UpdateStatusLines();
282+
}, null, 50, Timeout.Infinite);
283+
else
284+
UpdateLineTimer ??= new System.Threading.Timer(_ =>
285+
{
286+
UpdateLineTimer = null;
287+
288+
lock (ConsoleLock)
289+
Console.WriteLine(GetConsoleText(8));
290+
}, null, 250, Timeout.Infinite);
272291
}
273292

274293
public string? Prefix
275294
{
276295
get => _Prefix;
277296
set
278297
{
298+
var oldValue = _Prefix;
279299
_Prefix = value?.Replace('\n', ' ');
280-
AsyncUpdate();
300+
if (_Prefix != oldValue)
301+
AsyncUpdate();
281302
}
282303
}
283304

@@ -286,8 +307,10 @@ public string Text
286307
get => _Text;
287308
set
288309
{
310+
var oldValue = _Text;
289311
_Text = value.Replace('\n', ' ');
290-
AsyncUpdate();
312+
if (oldValue != _Text)
313+
AsyncUpdate();
291314
}
292315
}
293316

@@ -296,11 +319,14 @@ public float? Progress
296319
get => _Progress;
297320
set
298321
{
322+
var oldValue = _Progress;
299323
if (value is null)
300324
_Progress = null;
301325
else
302326
_Progress = Math.Clamp(value.Value, 0, 1);
303-
AsyncUpdate();
327+
328+
if (oldValue != _Progress)
329+
AsyncUpdate();
304330
}
305331
}
306332

@@ -309,6 +335,16 @@ public float? Progress
309335
public void Remove(TimeSpan? Delay = null)
310336
{
311337
Active = false;
338+
339+
if (!VTEnabled)
340+
{
341+
lock (ConsoleLock)
342+
{
343+
statusLines.Remove(this);
344+
return;
345+
}
346+
}
347+
312348
Delay ??= TimeSpan.FromSeconds(2);
313349

314350
if (Delay.Value != TimeSpan.Zero)
@@ -323,8 +359,9 @@ public void Remove(TimeSpan? Delay = null)
323359
if (index == -1)
324360
return;
325361

326-
using var _ = new ConsoleStateRestorer(-1);
327362
statusLines.RemoveAt(index);
363+
364+
using var _ = new ConsoleStateRestorer(-1);
328365
Console.WriteLine($"\e[;r\e[{3 + index};H\e[1M\e[{3 + statusLines.Count};r");
329366

330367
if (statusLines.Count == 0)

0 commit comments

Comments
 (0)