Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 18 additions & 10 deletions Kurukuru/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;

namespace Kurukuru
{
Expand Down Expand Up @@ -46,23 +45,32 @@ public static void WriteWithColor(string s, ConsoleColor color)
Console.ForegroundColor = foregroundColor;
}

// See: https://stackoverflow.com/questions/8946808/can-console-clear-be-used-to-only-clear-a-line-instead-of-whole-console/8946847#8946847
public static void ClearCurrentConsoleLine(int length, int top)
public static void RewriteConsoleLine(int length, int top, Action writeAction)
{
if (Console.IsOutputRedirected) return;
if (Console.IsOutputRedirected)
{
writeAction();
return;
}

Console.SetCursorPosition(0, top);
if (CanAcceptEscapeSequence)
{
Console.SetCursorPosition(0, top);
Console.Write("\u001B[2K");
writeAction();
Console.Write("\u001B[0K");
}
else
{
Console.SetCursorPosition(0, top);
Console.Write(new string(' ', length));
Console.SetCursorPosition(0, top);
writeAction();

// See: https://stackoverflow.com/questions/8946808/can-console-clear-be-used-to-only-clear-a-line-instead-of-whole-console/8946847#8946847
var currentLeft = Console.CursorLeft;
if (length > currentLeft)
{
Console.Write(new string(' ', length - currentLeft));
}
Console.SetCursorPosition(currentLeft, top);
}
Console.Out.Flush();
}

public static void SetCursorVisibility(bool visible)
Expand Down
30 changes: 18 additions & 12 deletions Kurukuru/Spinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,17 @@ public void Start(string terminator)

private void Render(string terminator)
{
var pattern = CurrentPattern;
var frame = pattern.Frames[_frameIndex++ % pattern.Frames.Length];
int RenderBody()
{
var pattern = CurrentPattern;
var frame = pattern.Frames[_frameIndex++ % pattern.Frames.Length];

ConsoleHelper.WriteWithColor(frame, Color ?? Console.ForegroundColor);
Console.Write(' ');
Console.Write(Text);

return frame.Length + 1 + Text.Length;
}

lock (s_consoleLock)
{
Expand All @@ -138,23 +147,20 @@ private void Render(string terminator)
var currentLeft = Console.CursorLeft;
var currentTop = Console.CursorTop;

ConsoleHelper.ClearCurrentConsoleLine(_lineLength, _enabled ? _cursorTop : currentTop);
ConsoleHelper.WriteWithColor(frame, Color ?? Console.ForegroundColor);
Console.Write(" ");
Console.Write(Text);
_lineLength = Console.CursorLeft; // get line length before write terminator
ConsoleHelper.RewriteConsoleLine(_lineLength, _enabled ? _cursorTop : currentTop, () =>
{
RenderBody();
_lineLength = Console.CursorLeft; // get line length before write terminator
});
Console.Write(terminator);
Console.Out.Flush();

Console.SetCursorPosition(currentLeft, currentTop);
}
else
{

ConsoleHelper.WriteWithColor(frame, Color ?? Console.ForegroundColor);
Console.Write(" ");
Console.Write(Text);
_lineLength = frame.Length + 1 + Text.Length;
var length = RenderBody();
_lineLength = length;
Console.Write(terminator);
Console.Out.Flush();
}
Expand Down
Loading