Skip to content

Commit effdebc

Browse files
author
Me
committed
Fix color output
1 parent 6a507f0 commit effdebc

6 files changed

Lines changed: 278 additions & 24 deletions

File tree

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+

2+
namespace TestPlotly
3+
{
4+
5+
6+
public class SecretManager
7+
{
8+
9+
// TestPlotly.SecretManager.GetSecret<string>("DefaultDbPassword")
10+
// TestPlotly.SecretManager.GetSecret<string>("GoogleGeoCodingApiKey")
11+
public static T GetSecret<T>(string secretName)
12+
{
13+
string asmName = typeof(SecretManager).Assembly.FullName;
14+
15+
int ipos = asmName.IndexOf(',');
16+
if (ipos != -1)
17+
{
18+
asmName = asmName.Substring(0, ipos);
19+
}
20+
21+
return GetSecret<T>(secretName, asmName);
22+
} // End Function GetSecret
23+
24+
25+
public static T GetSecret<T>(string secretName, string asmName)
26+
{
27+
T obj = default(T);
28+
29+
if (System.Environment.OSVersion.Platform == System.PlatformID.Unix)
30+
{
31+
obj = SecretManagerHelper.GetEtcKey<T>("/etc/COR/" + asmName, secretName);
32+
if(obj == null)
33+
obj = SecretManagerHelper.GetEtcKey<T>(@"/etc/COR/All", secretName);
34+
}
35+
else
36+
{
37+
obj = SecretManagerHelper.GetRegistryKey<T>(@"Software\COR\" + asmName, secretName);
38+
if(obj == null)
39+
obj = SecretManagerHelper.GetRegistryKey<T>(@"Software\COR\All", secretName);
40+
}
41+
42+
return obj;
43+
} // End Function GetSecret
44+
45+
46+
} // End Class SecretManager
47+
48+
49+
public class SecretManagerHelper
50+
{
51+
52+
53+
public static T GetRegistryKey<T>(string key, string value)
54+
{
55+
object obj = GetRegistryKey(key, value);
56+
return ObjectToGeneric<T>(obj);
57+
} // End Function GetRegistryKey
58+
59+
60+
public static T GetEtcKey<T>(string path, string value)
61+
{
62+
string obj = null;
63+
64+
string p = System.IO.Path.Combine(path, value);
65+
if(System.IO.File.Exists(p))
66+
obj = System.IO.File.ReadAllText(p, System.Text.Encoding.Default);
67+
68+
if(obj == null)
69+
return ObjectToGeneric<T>((object)obj);
70+
71+
// || obj.EndsWith(" ") || obj.EndsWith("\t")
72+
while ( obj.EndsWith("\r") || obj.EndsWith("\n") )
73+
obj = obj.Substring(0, obj.Length - 1);
74+
75+
return ObjectToGeneric<T>((object)obj);
76+
} // End Function GetRegistryKey
77+
78+
79+
private static object GetRegistryKey(string key, string value)
80+
{
81+
object objReturnValue = null;
82+
// HKEY_CURRENT_USER
83+
84+
//using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine
85+
using (Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser
86+
.OpenSubKey(key))
87+
{
88+
if (regKey != null)
89+
{
90+
objReturnValue = regKey.GetValue(value);
91+
} // End if (regKey != null)
92+
93+
} // End Using regKey
94+
95+
return objReturnValue;
96+
} // End Function GetRegistryKey
97+
98+
99+
private static T InlineTypeAssignHelper<T>(object UTO)
100+
{
101+
if (UTO == null)
102+
{
103+
T NullSubstitute = default(T);
104+
return NullSubstitute;
105+
} // End if (UTO == null)
106+
107+
return (T)UTO;
108+
} // End Template InlineTypeAssignHelper
109+
110+
111+
private static T ObjectToGeneric<T>(object objReturnValue)
112+
{
113+
string strReturnValue = null;
114+
System.Type tReturnType = typeof(T);
115+
116+
if (!object.ReferenceEquals(tReturnType, typeof(System.Byte[])))
117+
{
118+
if(objReturnValue != null)
119+
strReturnValue = System.Convert.ToString(objReturnValue);
120+
} // End if (!object.ReferenceEquals(tReturnType, typeof(System.Byte[])))
121+
122+
try
123+
{
124+
125+
if (object.ReferenceEquals(tReturnType, typeof(object)))
126+
{
127+
return InlineTypeAssignHelper<T>(objReturnValue);
128+
}
129+
else if (object.ReferenceEquals(tReturnType, typeof(string)))
130+
{
131+
return InlineTypeAssignHelper<T>(strReturnValue);
132+
} // End if string
133+
else if (object.ReferenceEquals(tReturnType, typeof(bool)))
134+
{
135+
bool bReturnValue = false;
136+
bool bSuccess = bool.TryParse(strReturnValue, out bReturnValue);
137+
138+
if (bSuccess)
139+
return InlineTypeAssignHelper<T>(bReturnValue);
140+
141+
if (strReturnValue == "0")
142+
return InlineTypeAssignHelper<T>(false);
143+
144+
return InlineTypeAssignHelper<T>(true);
145+
} // End if bool
146+
else if (object.ReferenceEquals(tReturnType, typeof(int)))
147+
{
148+
int iReturnValue = int.Parse(strReturnValue);
149+
return InlineTypeAssignHelper<T>(iReturnValue);
150+
} // End if int
151+
else if (object.ReferenceEquals(tReturnType, typeof(uint)))
152+
{
153+
uint uiReturnValue = uint.Parse(strReturnValue);
154+
return InlineTypeAssignHelper<T>(uiReturnValue);
155+
} // End if uint
156+
else if (object.ReferenceEquals(tReturnType, typeof(long)))
157+
{
158+
long lngReturnValue = long.Parse(strReturnValue);
159+
return InlineTypeAssignHelper<T>(lngReturnValue);
160+
} // End if long
161+
else if (object.ReferenceEquals(tReturnType, typeof(ulong)))
162+
{
163+
ulong ulngReturnValue = ulong.Parse(strReturnValue);
164+
return InlineTypeAssignHelper<T>(ulngReturnValue);
165+
} // End if ulong
166+
else if (object.ReferenceEquals(tReturnType, typeof(float)))
167+
{
168+
float fltReturnValue = float.Parse(strReturnValue);
169+
return InlineTypeAssignHelper<T>(fltReturnValue);
170+
}
171+
else if (object.ReferenceEquals(tReturnType, typeof(double)))
172+
{
173+
double dblReturnValue = double.Parse(strReturnValue);
174+
return InlineTypeAssignHelper<T>(dblReturnValue);
175+
}
176+
else if (object.ReferenceEquals(tReturnType, typeof(System.Net.IPAddress)))
177+
{
178+
System.Net.IPAddress ipaAddress = null;
179+
180+
if (string.IsNullOrEmpty(strReturnValue))
181+
return InlineTypeAssignHelper<T>(ipaAddress);
182+
183+
ipaAddress = System.Net.IPAddress.Parse(strReturnValue);
184+
return InlineTypeAssignHelper<T>(ipaAddress);
185+
} // End if IPAddress
186+
else if (object.ReferenceEquals(tReturnType, typeof(System.Byte[])))
187+
{
188+
if (objReturnValue == System.DBNull.Value)
189+
return InlineTypeAssignHelper<T>(null);
190+
191+
return InlineTypeAssignHelper<T>(objReturnValue);
192+
}
193+
else if (object.ReferenceEquals(tReturnType, typeof(System.Guid)))
194+
{
195+
if (string.IsNullOrEmpty(strReturnValue)) return InlineTypeAssignHelper<T>(null);
196+
197+
return InlineTypeAssignHelper<T>(new System.Guid(strReturnValue));
198+
} // End if GUID
199+
else if (object.ReferenceEquals(tReturnType, typeof(System.DateTime)))
200+
{
201+
System.DateTime bReturnValue = System.DateTime.Now;
202+
bool bSuccess = System.DateTime.TryParse(strReturnValue, out bReturnValue);
203+
204+
if (bSuccess)
205+
return InlineTypeAssignHelper<T>(bReturnValue);
206+
207+
if (strReturnValue == "0")
208+
return InlineTypeAssignHelper<T>(false);
209+
210+
return InlineTypeAssignHelper<T>(true);
211+
} // End if datetime
212+
else // No datatype matches
213+
{
214+
throw new System.NotImplementedException("ExecuteScalar<T>: This type is not yet defined.");
215+
} // End else of if tReturnType = datatype
216+
217+
} // End Try
218+
catch (System.Exception ex)
219+
{
220+
throw;
221+
} // End Catch
222+
223+
return InlineTypeAssignHelper<T>(null);
224+
} // End Function ObjectToGeneric
225+
226+
227+
} // End Class SecretManager
228+
229+
230+
} // End Namespace

sql_profiler/Code/Lexer/YukonLexer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ public string SyntaxHighlight(OutputWriter sb, string value)
137137
}
138138
Next();
139139
}
140-
140+
141141
sb.AppendLine();
142142
sb.AppendLine();
143-
143+
string lol = sb.ToString();
144+
144145
lsTokenTypeHistory.Clear();
145146
lsTokenTypeHistory = null;
146-
147-
return sb.ToString();
147+
148+
return lol;
148149
}
149150

150151
private string Line

sql_profiler/Code/OutputWriter/ConsoleOutputWriter.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace ExpressProfiler
66
public class ConsoleOutputWriter
77
: OutputWriter
88
{
9+
private static bool s_isWindows;
910
private static System.Collections.Generic.Dictionary<
1011
System.Drawing.Color, System.ConsoleColor> s_colorDict;
1112

@@ -16,6 +17,10 @@ private static System.Collections.Generic.Dictionary<
1617

1718
static ConsoleOutputWriter()
1819
{
20+
s_isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(
21+
System.Runtime.InteropServices.OSPlatform.Windows
22+
);
23+
1924
s_colorDict = SetupColorDictionary();
2025
}
2126

@@ -78,19 +83,19 @@ public override System.Drawing.Color BackColor
7883
// if (!s_colorDict.ContainsKey(value)) System.Console.WriteLine(value);
7984
System.Console.BackgroundColor = s_colorDict[value];
8085
this.AppendLine();
81-
//System.Console.WriteLine(new string(' ', System.Console.BufferWidth));
8286
}
8387
}
8488

8589

8690
public override void AppendLine()
87-
{
91+
{
8892
// Finish the line with empty color
8993
System.Console.Write(new string(' ', System.Console.BufferWidth - System.Console.CursorLeft));
90-
// System.Console.WriteLine();
94+
System.Console.Write(System.Environment.NewLine);
95+
//else System.Console.WriteLine();
9196
}
92-
93-
97+
98+
9499
public override void Append(string text)
95100
{
96101
System.Console.Write(text);

sql_profiler/Code/SqlServerProfiler.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,11 @@ protected string GetEventCaption(ProfilerEvent evt)
365365
return "Trace paused";
366366

367367
if (evt == m_EventStopped)
368+
{
369+
368370
return "Trace stopped";
369-
371+
}
372+
370373
return ProfilerEvents.Names[evt.EventClass];
371374
} // End Function GetEventCaption
372375

@@ -389,12 +392,14 @@ protected void NewEventArrived(ProfilerEvent evt, bool last)
389392
//m_columns.Add(new PerfColumn { Caption = "Object name", Column = ProfilerEventColumns.ObjectName, Width = 70 });
390393
//m_columns.Add(new PerfColumn { Caption = "Application name", Column = ProfilerEventColumns.ApplicationName, Width = 70 });
391394
//m_columns.Add(new PerfColumn { Caption = "Host name", Column = ProfilerEventColumns.HostName, Width = 70 });
392-
393-
395+
396+
394397
// System.Console.WriteLine(evt);
398+
System.Console.ResetColor();
395399
string caption = GetEventCaption(evt);
396400
System.Console.Write(caption);
397401
System.Console.Write(new string(' ', System.Console.BufferWidth - System.Console.CursorLeft));
402+
System.Console.Write(System.Environment.NewLine);
398403

399404
string td = evt.GetFormattedData(ProfilerEventColumns.TextData, null);
400405
// System.Console.WriteLine(td);

sql_profiler/Program.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,20 @@ static void Main(string[] args)
283283
// MainTest(args);
284284
DoProfiling(args);
285285
}
286-
286+
287+
287288
static void MainTest(string[] args)
288289
{
289290
string instance = GetPlatformDefaultInstance();
290291
// dotnet run --server localhost --username MY_USER --password MY_PASSWORD --db MY_DB
291292
// string ar = $"--server {instance} --username WebAppWebServices --password TOP_SECRET --Db COR_Basic_Demo_V4";
292-
string ar = $"--server {instance} /db \"SwissRe_Test_V4\"";
293+
// string ar = $"--server {instance} /db \"COR_Basic_Demo_V4\"";
294+
295+
string un = TestPlotly.SecretManager.GetSecret<string>("DefaultDbUser");
296+
string pw = TestPlotly.SecretManager.GetSecret<string>("DefaultDbPassword");
297+
298+
string ar = $"--server {instance} --username {un} --password {pw} --db \"Redmine\"";
299+
293300
DoProfiling(ar.Split(' '));
294301
} // End Sub Main
295302

@@ -351,17 +358,22 @@ static void DoProfiling(string[] args)
351358

352359

353360
s_profiler = new ExpressProfiler.SqlServerProfiler(server, db, username, password);
354-
355-
361+
356362
s_profiler.StartProfiling();
357-
358-
359-
// System.Console.WriteLine("--- Press any key to stop profiling --- ");
360-
//System.Console.ReadKey();
361-
362-
System.Console.WriteLine("--- Press ENTER to stop profiling --- ");
363-
System.Console.ReadLine();
364-
363+
364+
365+
// System.Console.WriteLine("--- Press ENTER to stop profiling --- ");
366+
// System.Console.ReadLine();
367+
368+
System.Console.WriteLine("--- Press any key to stop profiling --- ");
369+
// System.Console.ReadKey();
370+
371+
// This is Sparta !
372+
while (!System.Console.KeyAvailable)
373+
{
374+
System.Threading.Thread.Sleep(500);
375+
}
376+
365377
OnExit();
366378
} // End Sub Test
367379

sql_profiler/sql_profiler.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<EmbeddedResource Include="SQL\Clear_Traces.sql" />
1515
</ItemGroup>
1616
<ItemGroup>
17+
<PackageReference Include="Microsoft.Win32.Registry" Version="4.4.0" />
1718
<PackageReference Include="System.Data.Common" Version="4.3.0" />
1819
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
1920
</ItemGroup>

0 commit comments

Comments
 (0)