Skip to content

Commit

Permalink
Console write as span
Browse files Browse the repository at this point in the history
  • Loading branch information
ladeak committed Sep 21, 2024
1 parent 555cdf0 commit 52ce5c5
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 105 deletions.
6 changes: 2 additions & 4 deletions src/CHttp/Abstractions/CHttpConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ internal class CHttpConsole : IConsole

public void SetCursorPosition(int left, int top) => Console.SetCursorPosition(left, top);

public void Write(char[] buffer) => Console.Write(buffer);

public void Write(string buffer) => Console.Write(buffer);
public void Write(ReadOnlySpan<char> buffer) => Console.Out.Write(buffer);

public void WriteLine() => Console.WriteLine();

public void WriteLine(string value) => Console.WriteLine(value);
public void WriteLine(ReadOnlySpan<char> value) => Console.Out.WriteLine(value);
}
10 changes: 4 additions & 6 deletions src/CHttp/Abstractions/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ internal interface IConsole

void SetCursorPosition(int left, int top);

void Write(char[] buffer);

void Write(string buffer);

void WriteLine();

void WriteLine(string value);
void Write(ReadOnlySpan<char> buffer);

void WriteLine(ReadOnlySpan<char> value);

ConsoleColor ForegroundColor { get; set; }
ConsoleColor ForegroundColor { get; set; }
}
11 changes: 4 additions & 7 deletions src/CHttp/Abstractions/NoOpConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace CHttp.Abstractions;

namespace CHttp.Abstractions;

internal class NoOpConsole : IConsole
{
Expand All @@ -12,19 +13,15 @@ public void SetCursorPosition(int left, int top)
{
}

public void Write(char[] buffer)
{
}

public void Write(string buffer)
public void Write(ReadOnlySpan<char> buffer)
{
}

public void WriteLine()
{
}

public void WriteLine(string value)
public void WriteLine(ReadOnlySpan<char> value)
{
}
}
4 changes: 1 addition & 3 deletions src/CHttp/Writers/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace CHttp.Writers;
internal sealed class ProgressBar<T> where T : struct
{
private readonly int _length;
private readonly char[] _complete;
private readonly IConsole _console;
private readonly IAwaiter _awaiter;
private T _value;
Expand All @@ -17,7 +16,6 @@ public ProgressBar(IConsole console, IAwaiter awaiter)
_console = console ?? new CHttpConsole();
_awaiter = awaiter ?? new Awaiter();
_length = Math.Min(50, _console.WindowWidth);
_complete = "100%".PadRight(_length).ToArray();
}

public async Task RunAsync<U>(CancellationToken token = default) where U : INumberFormatter<T>
Expand Down Expand Up @@ -51,7 +49,7 @@ public async Task RunAsync<U>(CancellationToken token = default) where U : INumb
await _awaiter.WaitAsync(TimeSpan.FromMilliseconds(50));
} while (!token.IsCancellationRequested);
_console.SetCursorPosition(position.Left, position.Top);
_console.Write(_complete);
_console.Write("100%".PadRight(_length));
_console.Write(U.FormatSize(_value));
_console.WriteLine();
_console.CursorVisible = true;
Expand Down
38 changes: 20 additions & 18 deletions src/CHttpExtension/StateConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ namespace CHttpExtension;

public class StateConsole : IConsole
{
private readonly Action<string> _callback;
private readonly Action<string> _callback;

public StateConsole(Action<string> callback)
{
_callback = callback ?? throw new ArgumentNullException(nameof(callback));
}
public StateConsole(Action<string> callback)
{
_callback = callback ?? throw new ArgumentNullException(nameof(callback));
}

public bool CursorVisible { get; set; } = false;
public bool CursorVisible { get; set; } = false;

public string Text => string.Empty;
public string Text => string.Empty;

public int WindowWidth => 72;
public int WindowWidth => 72;

public ConsoleColor ForegroundColor { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public ConsoleColor ForegroundColor { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public (int Left, int Top) GetCursorPosition() => (0, 0);
public (int Left, int Top) GetCursorPosition() => (0, 0);

public void SetCursorPosition(int left, int top)
{
}
public void SetCursorPosition(int left, int top)
{
}

public void Write(char[] buffer) { }
public void Write(ReadOnlySpan<char> buffer)
{
if (buffer[0] != '[' && buffer[^1] != ']')
_callback(buffer.ToString());
}

public void Write(string buffer) => _callback(buffer);
public void WriteLine(ReadOnlySpan<char> value) { }

public void WriteLine() => _callback("Completed");

public void WriteLine(string value) { }
public void WriteLine() => _callback("Completed");
}
72 changes: 35 additions & 37 deletions src/CHttpExtension/StringConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,51 @@ namespace CHttpExtension;

public class StringConsole : IConsole
{
private ConsoleColor _color = ConsoleColor.Black;
private bool _colorize;
private ConsoleColor _color = ConsoleColor.Black;
private bool _colorize;

public StringConsole()
{
}
public StringConsole()
{
}

public StringConsole(string color)
{
if (!Enum.TryParse<ConsoleColor>(color, out _color))
_color = ConsoleColor.Black;
}
public StringConsole(string color)
{
if (!Enum.TryParse<ConsoleColor>(color, out _color))
_color = ConsoleColor.Black;
}

public bool CursorVisible { get; set; } = false;
public bool CursorVisible { get; set; } = false;

private StringBuilder _sb = new StringBuilder();
private StringBuilder _sb = new StringBuilder();

public string Text { get => _sb.ToString(); }
public string Text { get => _sb.ToString(); }

public int WindowWidth => 72;
public int WindowWidth => 72;

public ConsoleColor ForegroundColor
{
get => _color;
set
{
_color = value;
_colorize = !_colorize;
if (_colorize)
Write($"<span style=\"color:{_color};\">");
else
Write("</span>");
}
}
public ConsoleColor ForegroundColor
{
get => _color;
set
{
_color = value;
_colorize = !_colorize;
if (_colorize)
Write($"<span style=\"color:{_color};\">");
else
Write("</span>");
}
}

public (int Left, int Top) GetCursorPosition() => (0, 0);
public (int Left, int Top) GetCursorPosition() => (0, 0);

public void SetCursorPosition(int left, int top)
{
_sb.Clear();
}
public void SetCursorPosition(int left, int top)
{
_sb.Clear();
}

public void Write(char[] buffer) => _sb.Append(buffer);
public void WriteLine() => _sb.AppendLine();

public void Write(string buffer) => _sb.Append(buffer);
public void Write(ReadOnlySpan<char> buffer) => _sb.Append(buffer);

public void WriteLine() => _sb.AppendLine();

public void WriteLine(string value) => _sb.AppendLine(value);
public void WriteLine(ReadOnlySpan<char> value) { _sb.Append(value); _sb.AppendLine(); }
}
6 changes: 3 additions & 3 deletions tests/CHttp.Tests/Http/CHttpFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task VerboseWriter_TestVanilaHttp3Request()
var client = await CommandFactory.CreateRootCommand(writer).InvokeAsync("--method GET --no-certificate-validation --uri https://localhost:5011");

await writer.CompleteAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(10));
Assert.Contains($"Status: OK Version: 3.0 Encoding: utf-8{Environment.NewLine}Date{Environment.NewLine}:{Environment.NewLine}{DateReplacement}{Environment.NewLine}{Environment.NewLine}Server{Environment.NewLine}:{Environment.NewLine}Kestrel{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}test{Environment.NewLine}https://localhost:5011/ 4 B 00:0", console.Text);
Assert.Contains($"Status: OK Version: 3.0 Encoding: utf-8{Environment.NewLine}Date:{DateReplacement}{Environment.NewLine}Server:Kestrel{Environment.NewLine}{Environment.NewLine}test{Environment.NewLine}https://localhost:5011/ 4 B 00:0", console.Text);
}

[Fact]
Expand All @@ -39,7 +39,7 @@ public async Task VerboseWriter_TestVanilaHttp2Request()
var client = await CommandFactory.CreateRootCommand(writer).InvokeAsync("--method GET --no-certificate-validation --uri https://localhost:5011 -v 2");

await writer.CompleteAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(10));
Assert.Contains($"Status: OK Version: 2.0 Encoding: utf-8{Environment.NewLine}Date{Environment.NewLine}:{Environment.NewLine}{DateReplacement}{Environment.NewLine}{Environment.NewLine}Server{Environment.NewLine}:{Environment.NewLine}Kestrel{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}test{Environment.NewLine}https://localhost:5011/ 4 B 00:0", console.Text);
Assert.Contains($"Status: OK Version: 2.0 Encoding: utf-8{Environment.NewLine}Date:{DateReplacement}{Environment.NewLine}Server:Kestrel{Environment.NewLine}{Environment.NewLine}test{Environment.NewLine}https://localhost:5011/ 4 B 00:0", console.Text);
}

[Fact]
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task ContentTypeHeader_WrittenToConsole()
var client = await CommandFactory.CreateRootCommand(writer).InvokeAsync("--method GET --no-certificate-validation --uri https://localhost:5011 -v 2");

await writer.CompleteAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(10));
Assert.Contains($"Content-Type{Environment.NewLine}:{Environment.NewLine}application/json", console.Text);
Assert.Contains($"Content-Type:application/json", console.Text);
}

[Fact]
Expand Down
14 changes: 3 additions & 11 deletions tests/CHttp.Tests/TestConsoleAsOuput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ public void SetCursorPosition(int left, int top)
// NoOp
}

public void Write(char[] buffer) => _sb.Append(buffer);

public void Write(string buffer)
{
_sb.Append(buffer);
}

public void WriteLine() => _sb.AppendLine();

public void WriteLine(string value)
{
_sb.AppendLine(value);
}
public void Write(ReadOnlySpan<char> buffer) => _sb.Append(buffer);

public void WriteLine(ReadOnlySpan<char> value) { _sb.Append(value); _sb.AppendLine(); }
}
14 changes: 4 additions & 10 deletions tests/CHttp.Tests/TestConsolePerWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,16 @@ public TestConsolePerWrite(string filterDate) : this()

public void SetCursorPosition(int left, int top)
{
// NoOp
}

public void Write(char[] buffer) => _sb.Append(FilterDate(buffer.AsSpan()));

public void Write(string buffer)
{
_sb.Append(FilterDate(buffer.AsSpan()));
_sb.AppendLine();
}

public void Write(ReadOnlySpan<char> buffer) => _sb.Append(FilterDate(buffer));

public void WriteLine() => _sb.AppendLine();

public void WriteLine(string value)
public void WriteLine(ReadOnlySpan<char> value)
{
_sb.Append(FilterDate(value.AsSpan()));
_sb.Append(FilterDate(value));
_sb.AppendLine();
}

Expand Down
12 changes: 6 additions & 6 deletions tests/CHttp.Tests/Writers/ProgressBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public async Task SingleLoop_Writes100PercentAndZero()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
100% 0 B
", testConsole.Text);
}

Expand All @@ -32,9 +32,9 @@ public async Task Run_Resets_Size()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
100% 0 B
", testConsole.Text);
}

Expand All @@ -52,10 +52,10 @@ public async Task WhenEnds_Writes100PercentAndSize()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
[-=----] 100 B
100% 100 B
", testConsole.Text);
}

Expand All @@ -76,12 +76,12 @@ public async Task MultipleLoops_IncrasesSize()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
[-=----] 100 B
[--=---] 200 B
[---=--] 300 B
100% 300 B
", testConsole.Text);
}

Expand All @@ -102,6 +102,7 @@ public async Task Complete_Cycle_StartsOverSemicolons()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
[-=----] 100 B
[--=---] 200 B
Expand All @@ -115,7 +116,6 @@ public async Task Complete_Cycle_StartsOverSemicolons()
[----=-]1000 B
[-----=] 1 KB
100% 1 KB
", testConsole.Text);
}

Expand All @@ -136,14 +136,14 @@ public async Task DisplayAllSizes()
await loopHandle.CompleteLoopAsync();
await sutTask;
Assert.Equal(@"
[=-----] 0 B
[-=----] 1 KB
[--=---] 1 MB
[---=--] 1 GB
[----=-] 1 TB
[-----=]1024 TB
100% 1024 TB
", testConsole.Text);
}
}

0 comments on commit 52ce5c5

Please sign in to comment.