Skip to content

Commit

Permalink
More iteration and simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbilas committed Aug 18, 2024
1 parent 79c275b commit c060b44
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 281 deletions.
4 changes: 3 additions & 1 deletion src/Core.Terminal/KeyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// TODO: let's also pack in a little 10-char (null-term) array to store the captured pattern, and have little helpers to access it.
// sometimes might be nicer when detecting ctrl-c for example, and may help with diagnostics as well when there is a misfire on the key matcher.

public interface ITerminalEvent {}

[PublicAPI]
public readonly record struct KeyEvent(ConsoleKey Key, char Char, bool Alt = false, bool Shift = false, bool Ctrl = false)
public readonly record struct KeyEvent(ConsoleKey Key, char Char, bool Alt = false, bool Shift = false, bool Ctrl = false) : ITerminalEvent
{
public KeyEvent(ConsoleKey key, bool alt = false, bool shift = false, bool ctrl = false)
: this(key, default, alt, shift, ctrl) {}
Expand Down
10 changes: 2 additions & 8 deletions src/Stale.Cli/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
M:Vezel.Cathode.IO.TerminalWriter.WriteLineAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.IO.TerminalWriter.WriteLine`1(``0)
M:Vezel.Cathode.IO.TerminalWriter.Write`1(``0)
M:Vezel.Cathode.Terminal.ErrorAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.Terminal.ErrorLineAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.Terminal.ErrorLine`1(``0)
M:Vezel.Cathode.Terminal.Error`1(``0)
M:Vezel.Cathode.Terminal.OutAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.Terminal.OutLineAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.Terminal.OutLine`1(``0)
M:Vezel.Cathode.Terminal.Out`1(``0)
M:Vezel.Cathode.Text.Control.ControlBuilder.PrintLine`1(``0)
M:Vezel.Cathode.Text.Control.ControlBuilder.Print`1(``0)
M:Vezel.Cathode.VirtualTerminal.ErrorAsync`1(``0,System.Threading.CancellationToken)
Expand All @@ -20,3 +12,5 @@ M:Vezel.Cathode.VirtualTerminal.OutAsync`1(``0,System.Threading.CancellationToke
M:Vezel.Cathode.VirtualTerminal.OutLineAsync`1(``0,System.Threading.CancellationToken)
M:Vezel.Cathode.VirtualTerminal.OutLine`1(``0)
M:Vezel.Cathode.VirtualTerminal.Out`1(``0)

T:Vezel.Cathode.Terminal
24 changes: 16 additions & 8 deletions src/Stale.Cli/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ public void Stop()
class Context : IDisposable
{
readonly CancellationTokenSource _cancelSource = new();
readonly LongTasks _longTasks;
readonly IDumpOutput _dumpOutput;

public Context()
public Context(VirtualTerminal? terminal = null)
{
_longTasks = new(_cancelSource.Token);
# pragma warning disable RS0030
Terminal = terminal ?? Vezel.Cathode.Terminal.System;
# pragma warning restore RS0030

LongTasks = new(_cancelSource.Token);
_dumpOutput = new TerminalDumpOutput(Terminal);
}

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

var stillRunning = _longTasks.GetTasksSnapshot();
var stillRunning = LongTasks.GetTasksSnapshot();
if (stillRunning.Any(t => !t.Weak))
{
if (IsVerbose || Debugger.IsAttached)
Expand Down Expand Up @@ -84,7 +89,8 @@ public void Dispose()
public bool IsCancellationRequested => _cancelSource.IsCancellationRequested;
public void Cancel() => _cancelSource.Cancel();

public LongTasks LongTasks => _longTasks;
public VirtualTerminal Terminal { get; }
public LongTasks LongTasks { get; }

public StaleCliArguments Options = null!;
public bool IsVerbose; // TODO: either make readonly for better JIT or force caller to check this before calling Verbose funcs
Expand Down Expand Up @@ -187,7 +193,7 @@ void OutDump<T>(T value, string? label, ColorConfig colors) => value.Dump(
useDescriptors: false,
typeNames: new() { ShowTypeNames = false },
colors: colors,
output: s_dumpOutput,
output: _dumpOutput,
tableConfig: new() { ShowTableHeaders = false });

// Other
Expand All @@ -198,16 +204,18 @@ void OutDump<T>(T value, string? label, ColorConfig colors) => value.Dump(

class TerminalDumpOutput : IDumpOutput
{
public TerminalDumpOutput(VirtualTerminal terminal) =>
TextWriter = terminal.StandardOut.TextWriter;

// i'd like to adjust the config to have a MemberProvider that skips fields that are set to their default values
// but that would break the table rendering, where all rows need the same fields. would require a deeper change
// to dumpify to support that.

public RendererConfig AdjustConfig(in RendererConfig config) => config;
public TextWriter TextWriter { get; } = Terminal.StandardOut.TextWriter;
public TextWriter TextWriter { get; }
}

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"));
}
2 changes: 1 addition & 1 deletion src/Stale.Cli/Playback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static async Task<CliExitCode> Record(Context ctx, string? recordedPath,

var jsonStream = recordedPath != null
? File.Create(recordedPath)
: Terminal.StandardOut.Stream;
: ctx.Terminal.StandardOut.Stream;

await using var writer = new StreamWriter(jsonStream);
writer.AutoFlush = true;
Expand Down
131 changes: 0 additions & 131 deletions src/Stale.Cli/Screen.cs

This file was deleted.

Loading

0 comments on commit c060b44

Please sign in to comment.