Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Stopwatch.App/StopwatchApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public sealed class StopwatchApplication
private readonly ICommandParser _commandParser;
private readonly CommandHandler _commandHandler;
private readonly StringBuilder _inputBuffer = new();
private int _previousLineLength;

public StopwatchApplication(
IStopwatchService stopwatchService,
Expand All @@ -31,9 +32,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
while (!cancellationToken.IsCancellationRequested && running)
{
var elapsed = await _stopwatchService.GetElapsedAsync();
var formatted = FormatElapsed(elapsed);

_console.Write($"\r{formatted} > {_inputBuffer} ");
RenderLine(elapsed);

if (_console.KeyAvailable)
{
Expand All @@ -43,6 +42,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
{
var command = _commandParser.Parse(_inputBuffer.ToString());
_inputBuffer.Clear();
_previousLineLength = 0;

_console.Write("\n");

Expand All @@ -55,10 +55,12 @@ public async Task RunAsync(CancellationToken cancellationToken)
else if (key.Key == ConsoleKey.Backspace && _inputBuffer.Length > 0)
{
_inputBuffer.Length--;
RenderLine(elapsed);
}
else if (!char.IsControl(key.KeyChar))
{
_inputBuffer.Append(key.KeyChar);
RenderLine(elapsed);
}
}

Expand All @@ -77,6 +79,17 @@ public async Task RunAsync(CancellationToken cancellationToken)
_console.Write("\n");
}

private void RenderLine(TimeSpan elapsed)
{
var formatted = FormatElapsed(elapsed);
var output = $"\r{formatted} > {_inputBuffer}";
var padding = Math.Max(0, _previousLineLength - output.Length);
var paddedOutput = output + new string(' ', padding);
_previousLineLength = output.Length;

_console.Write(paddedOutput);
}

private static string FormatElapsed(TimeSpan elapsed)
{
var days = (int)elapsed.TotalDays;
Expand Down