Initial commit - framework

This commit is contained in:
2022-11-30 17:19:53 -06:00
commit 23945154d4
39 changed files with 9161 additions and 0 deletions

48
src/Timer.cs Normal file
View File

@ -0,0 +1,48 @@
using System.Diagnostics;
namespace aoc2022;
internal class Timer : IDisposable
{
private readonly Stopwatch stopwatch = Stopwatch.StartNew();
private readonly string? name;
private bool stopped = false;
public bool Disabled { get; set; }
public Timer(string? inName = null)
{
name = inName;
}
public void Stop()
{
if (stopped)
{
return;
}
stopwatch.Stop();
stopped = true;
}
public void Dispose()
{
Stop();
if (Disabled)
{
return;
}
var (elapsed, unit) = stopwatch.ConvertToHumanReadable();
var color = "<red>";
if (unit == "us" || (unit == "ms" && elapsed < 10))
{
color = "<green>";
}
else if (unit == "ms" && elapsed < 250)
{
color = "<yellow>";
}
Logger.Log($"<cyan>{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}<r>");
}
}