Skip to content

Commit

Permalink
Improve console logging when running inside powershell or other conso…
Browse files Browse the repository at this point in the history
…le streams.
  • Loading branch information
SebastianStehle committed May 5, 2022
1 parent e2c6c4e commit 261ff80
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/Squidex.CLI/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/Squidex/squidex/</PackageProjectUrl>
<PackageTags>Squidex HeadlessCMS</PackageTags>
<Version>8.11</Version>
<Version>8.12</Version>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ private sealed class ConsoleLine : ILogLine
{
private readonly int consoleTop;

public bool CanWriteToSameLine => consoleTop >= 0;

public ConsoleLine()
{
consoleTop = Console.CursorTop;
try
{
consoleTop = Console.CursorTop;
}
catch
{
consoleTop = -1;
}
}

public void Dispose()
Expand All @@ -27,7 +36,11 @@ public void Dispose()
public void WriteLine(string message, params object[] args)
{
Console.WriteLine(message, args);
Console.SetCursorPosition(0, consoleTop);

if (consoleTop >= 0)
{
Console.SetCursorPosition(0, consoleTop);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Squidex.CLI.Commands.Implementation
{
public interface ILogLine : IDisposable
{
bool CanWriteToSameLine { get; }

void WriteLine(string message, params object[] args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public static async Task ExportAsync(this ISession session, IExportSettings sett

await handler(entity);

logLine.WriteLine("> Exported: {0} of {1}.", totalRead, total);
if (logLine.CanWriteToSameLine)
{
logLine.WriteLine("> Exported: {0} of {1}.", totalRead, total);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public static async Task ImportAsync(this ISession session, IImportSettings sett

totalWritten += update.Jobs.Count;

logLine.WriteLine("> Imported: {0}.", totalWritten);
if (logLine.CanWriteToSameLine)
{
logLine.WriteLine("> Imported: {0}.", totalWritten);
}
}
}

Expand Down

0 comments on commit 261ff80

Please sign in to comment.