Skip to content

Commit

Permalink
Stale WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbilas committed Aug 13, 2024
1 parent 05f1ee6 commit f35880f
Show file tree
Hide file tree
Showing 10 changed files with 813 additions and 322 deletions.
27 changes: 27 additions & 0 deletions Stale.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OkTools", "OkTools", "{3B7B
Directory.Build.rsp = Directory.Build.rsp
Directory.Packages.props = Directory.Packages.props
global.json = global.json
targets\Exe.targets = targets\Exe.targets
targets\Library.targets = targets\Library.targets
targets\Tests.targets = targets\Tests.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flog", "src\Flog\Flog.csproj", "{74302A65-1C99-4EEE-BCE5-E6871220C557}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flog.Cli", "src\Flog.Cli\Flog.Cli.csproj", "{8B492F02-DFFE-49A0-91FA-2F7D6DAFC96B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Terminal", "src\Core.Terminal\Core.Terminal.csproj", "{97404C8D-5AF4-4421-952C-0942D453D692}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Terminal-Tests", "src\Core.Terminal\Core.Terminal-Tests.csproj", "{85998317-0436-45AD-A4CB-278EC8055ADD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,5 +64,21 @@ Global
{3D70F5C9-4AA8-4B3F-AB87-E23B8F6B7507}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D70F5C9-4AA8-4B3F-AB87-E23B8F6B7507}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D70F5C9-4AA8-4B3F-AB87-E23B8F6B7507}.Release|Any CPU.Build.0 = Release|Any CPU
{74302A65-1C99-4EEE-BCE5-E6871220C557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74302A65-1C99-4EEE-BCE5-E6871220C557}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74302A65-1C99-4EEE-BCE5-E6871220C557}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74302A65-1C99-4EEE-BCE5-E6871220C557}.Release|Any CPU.Build.0 = Release|Any CPU
{8B492F02-DFFE-49A0-91FA-2F7D6DAFC96B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B492F02-DFFE-49A0-91FA-2F7D6DAFC96B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B492F02-DFFE-49A0-91FA-2F7D6DAFC96B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B492F02-DFFE-49A0-91FA-2F7D6DAFC96B}.Release|Any CPU.Build.0 = Release|Any CPU
{97404C8D-5AF4-4421-952C-0942D453D692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97404C8D-5AF4-4421-952C-0942D453D692}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97404C8D-5AF4-4421-952C-0942D453D692}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97404C8D-5AF4-4421-952C-0942D453D692}.Release|Any CPU.Build.0 = Release|Any CPU
{85998317-0436-45AD-A4CB-278EC8055ADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85998317-0436-45AD-A4CB-278EC8055ADD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85998317-0436-45AD-A4CB-278EC8055ADD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85998317-0436-45AD-A4CB-278EC8055ADD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/Stale.Cli/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
static class Constants
{
public const string WrapText = "»";
public const string WrapColor = "blue";

public const string TopStatusColor = "bold yellow on navyblue";
public const string BottomStatusColor = "white on grey";
public const string StderrColor = "white on darkred";
}
86 changes: 57 additions & 29 deletions src/Stale.Cli/Context.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Dumpify;
using Spectre.Console;
using Spectre.Console.Rendering;
using Vezel.Cathode;
using InvalidOperationException = System.InvalidOperationException;

#pragma warning disable CA1822

enum StatType
enum PerfStatType
{
Init,
Command,
}

class Stat
class PerfStat
{
DateTime? _start, _stop;

Expand Down Expand Up @@ -40,34 +42,75 @@ public void Stop()
class Context : IDisposable
{
readonly CancellationTokenSource _cancelSource = new();
readonly LongTasks _longTasks;

public Context()
{
_ansiConsole = new TerminalAnsiConsole(this);
_longTasks = new(_cancelSource.Token);
}

public void Dispose()
{
_cancelSource.Dispose();

var stillRunning = _longTasks.GetTasksSnapshot();
if (stillRunning.Any())
{
if (IsVerbose || Debugger.IsAttached)
{
ErrorLine("Long tasks were still running on exit:");
for (var i = 0; i < stillRunning.Count; ++i)
{
var task = stillRunning[i];
ErrorLine($" [{i+1}/{stillRunning.Count}] \"{task.Name}\"");
ErrorLine($" serial={task.Serial}, id={task.Task.Id}, status={task.Task.Status}");
ErrorLine(task.Creation.ToString().Split('\n').Select(l => " " + l).StringJoin("\n"));
}
}
else
ErrorLine("Long tasks were still running on exit: " + stillRunning.Select(v => v.Name).StringJoin(", "));
}
}

public CancellationToken CancelToken => _cancelSource.Token;
public bool IsCancellationRequested => _cancelSource.IsCancellationRequested;
public void Cancel() => _cancelSource.Cancel();

public LongTasks LongTasks => _longTasks;

public StaleCliArguments Options = null!;
public bool IsVerbose;
public bool IsVerbose; // TODO: either make readonly for better JIT or force caller to check this before calling Verbose funcs

public void VerboseLine(string text)
{
if (IsVerbose)
_ansiConsole.MarkupLineInterpolated($"[grey]{text}[/]");
OutMarkupLineInterp($"[grey]{text}[/]");
}

public void Verbose(string text)
{
if (IsVerbose)
_ansiConsole.MarkupInterpolated($"[grey]{text}[/]");
OutMarkupInterp($"[grey]{text}[/]");
}

[Conditional("DEBUG")]
public void DebugLine(string text)
{
var frames = new StackTrace(1)
.GetFrames()
.Reverse()
.SelectWhere(frame =>
{
var name = MiscUtils.ToNiceMethodName(frame.GetMethod()!);
// "just my code" only
return (name, !name.StartsWith("System."));
});

Debug.WriteLine(
$"{Environment.CurrentManagedThreadId,2}: "+
$"#{Task.CurrentId ?? '-',2} | "+
$"{MiscUtils.SequenceDuplicatesAsDots(frames).StringJoin("")}{text}");
}

public void OutLine() =>
Expand All @@ -87,16 +130,18 @@ public void Out(IRenderable renderable) =>
Terminal.Out(renderable.ToAnsi());

public void OutMarkupLine(string text) =>
_ansiConsole.MarkupLine(text);
OutLine(new Markup(text).ToAnsi());
public void OutMarkup(string text) =>
_ansiConsole.Markup(text);
Out(new Markup(text).ToAnsi());
public void OutMarkupLineInterp(FormattableString value) =>
_ansiConsole.MarkupLineInterpolated(value);
OutLine(Markup.FromInterpolated(value).ToAnsi());
public void OutMarkupInterp(FormattableString value) =>
_ansiConsole.MarkupInterpolated(value);
Out(Markup.FromInterpolated(value).ToAnsi());

public void ErrorLine<T>(T value) =>
Terminal.ErrorLine(value);
public void ErrorLine() =>
Terminal.ErrorLine();
public void Error<T>(T value) =>
Terminal.Error(value);

Expand Down Expand Up @@ -124,23 +169,7 @@ void OutDump<T>(T value, string? label, ColorConfig colors) => value.Dump(
output: s_dumpOutput,
tableConfig: new() { ShowTableHeaders = false });

public Stat this[StatType type] => _stats[(int)type];

class TerminalAnsiConsole(Context ctx) : IAnsiConsole
{
// we only need Write()
public Profile Profile => throw new InvalidOperationException();
public IAnsiConsoleCursor Cursor => throw new InvalidOperationException();
public IAnsiConsoleInput Input => throw new InvalidOperationException();
public IExclusivityMode ExclusivityMode => throw new InvalidOperationException();
public RenderPipeline Pipeline => throw new InvalidOperationException();
public void Clear(bool home) => throw new InvalidOperationException();

public void Write(IRenderable renderable)
{
ctx.Out(renderable);
}
}
public PerfStat this[PerfStatType type] => _stats[(int)type];

class TerminalDumpOutput : IDumpOutput
{
Expand All @@ -152,8 +181,7 @@ class TerminalDumpOutput : IDumpOutput
public TextWriter TextWriter { get; } = Terminal.StandardOut.TextWriter;
}

readonly IAnsiConsole _ansiConsole;
readonly Stat[] _stats = EnumUtility.GetNames<StatType>().Select(_ => new Stat()).ToArray();
readonly PerfStat[] _stats = EnumUtility.GetNames<PerfStatType>().Select(_ => new PerfStat()).ToArray();

static readonly IDumpOutput s_dumpOutput = new TerminalDumpOutput();
static readonly ColorConfig k_verboseDumpColors = new(new DumpColor("#808080"));
Expand Down
Loading

0 comments on commit f35880f

Please sign in to comment.