forked from microsoft/Generative-AI-for-beginners-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleHelper.cs
More file actions
89 lines (80 loc) · 2.67 KB
/
Copy pathConsoleHelper.cs
File metadata and controls
89 lines (80 loc) · 2.67 KB
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
/// <summary>
/// Small helper to keep console printing consistent and easy to read.
/// Uses simple colored output and labeled sections to make the streaming flow obvious.
/// </summary>
public static class ConsoleHelper
{
public static void PrintHeader(string text)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(new string('=', 60));
Console.WriteLine(text);
Console.WriteLine(new string('=', 60));
Console.ResetColor();
Console.WriteLine();
}
public static void PrintSection(string title)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"--- {title} ---");
Console.ResetColor();
}
public static void PrintUpdate(string updateText, string? continuationToken)
{
var timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"[{timestamp}] ");
Console.ResetColor();
// Print the update text (streaming chunk)
if (string.IsNullOrEmpty(updateText))
{
Console.WriteLine("(empty update)");
}
else
{
Console.WriteLine(updateText);
}
// Print a compact representation of the continuation token for debugging
if (!string.IsNullOrWhiteSpace(continuationToken))
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($" token: {Truncate(continuationToken, 60)}");
Console.ResetColor();
}
}
public static void PrintLabeled(string label, string content)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"{label}:");
Console.ResetColor();
Console.WriteLine(content);
}
public static void PrintInfo(string message)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(message);
Console.ResetColor();
}
public static void PrintError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}
public static void PrintFooter(string text)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine(new string('=', 60));
Console.WriteLine(text);
Console.WriteLine(new string('=', 60));
Console.ResetColor();
}
private static string Truncate(string value, int maxLength)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength) + "...";
}
}