Skip to content

Commit

Permalink
Provide workaround for #69
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreas committed Oct 24, 2024
1 parent ac1f6d1 commit 7cdc8c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
45 changes: 24 additions & 21 deletions tone/Commands/DumpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,28 @@ public DumpCommand(ILogger logger, SpectreConsoleService console, DirectoryLoade
_logger = logger;
_fs = fs;
}
private async Task<ReturnCode> SpectreConsoleDelegate(IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct)
{
foreach (var line in lines)
{
_console.WriteLine(line);
}
return await Task.FromResult(ReturnCode.Success);
}

private async Task<ReturnCode> WriteFileDelegate(IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct)
{
try
{
await fs.File.WriteAllLinesAsync(outputFile, lines, ct);
}
catch (Exception e)

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (macOS)

The variable 'e' is declared but never used

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (linux-musl-arm64, ubuntu-latest, linux-musl-arm64)

The variable 'e' is declared but never used

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (linux)

The variable 'e' is declared but never used

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (linux-arm64)

The variable 'e' is declared but never used

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (linux-arm)

The variable 'e' is declared but never used

Check warning on line 55 in tone/Commands/DumpCommand.cs

View workflow job for this annotation

GitHub Actions / Release (linux-musl-x64)

The variable 'e' is declared but never used
{
return ReturnCode.GeneralError;
}

return ReturnCode.Success;
}
public override async Task<int> ExecuteAsync(CommandContext context, DumpCommandSettings settings, CancellationToken cancellationToken)
{
var audioExtensions = DirectoryLoaderService.ComposeAudioExtensions(settings.IncludeExtensions);
Expand All @@ -47,14 +68,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, DumpCommand
.ToArray();
var returnCode = ReturnCode.Success;

var outputDelegate = async Task<ReturnCode> (IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct) =>
{
foreach (var line in lines)
{
_console.WriteLine(line);
}
return await Task.FromResult(ReturnCode.Success);
};
var outputDelegate = SpectreConsoleDelegate;
var outputSuffix = "";


Expand All @@ -66,19 +80,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, DumpCommand
}

// outputDelegate = async lines => ;
outputDelegate = async Task<ReturnCode> (fs, outputFile, lines, ct) =>
{
try
{
await fs.File.WriteAllLinesAsync(outputFile, lines, ct);
}
catch (Exception e)
{
return ReturnCode.GeneralError;
}

return ReturnCode.Success;
};
outputDelegate = WriteFileDelegate;

outputSuffix = settings.Format switch
{
SerializerFormat.Json => ToneJsonTagger.DefaultFileSuffix,
Expand Down
18 changes: 16 additions & 2 deletions tone/Services/SpectreConsoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@ public SpectreConsoleService(IAnsiConsole? stdout = null, IAnsiConsole? stderr =
});

// fix unwanted wrapping if output is redirected
if (Console.IsOutputRedirected)
if (ShouldAdjustOutputWidth())
{
Output.Profile.Width = int.MaxValue;
}

if (Console.IsErrorRedirected)
if (ShouldAdjustErrorWidth())
{
Error.Profile.Width = int.MaxValue;
}
}

private static bool ShouldAdjustOutputWidth() => Environment.GetEnvironmentVariable("TONE_OUTPUT_REDIRECT") switch
{
"0" => false,
"1" => true,
_ => Console.IsOutputRedirected
};

private static bool ShouldAdjustErrorWidth() => Environment.GetEnvironmentVariable("TONE_OUTPUT_REDIRECT") switch
{
"0" => false,
"1" => true,
_ => Console.IsErrorRedirected
};

public Profile Profile => Output.Profile;
public IAnsiConsoleCursor Cursor => Output.Cursor;
public IAnsiConsoleInput Input => Output.Input;
Expand Down

0 comments on commit 7cdc8c5

Please sign in to comment.