Skip to content

Commit 7cdc8c5

Browse files
committed
Provide workaround for #69
1 parent ac1f6d1 commit 7cdc8c5

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

tone/Commands/DumpCommand.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,28 @@ public DumpCommand(ILogger logger, SpectreConsoleService console, DirectoryLoade
3737
_logger = logger;
3838
_fs = fs;
3939
}
40+
private async Task<ReturnCode> SpectreConsoleDelegate(IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct)
41+
{
42+
foreach (var line in lines)
43+
{
44+
_console.WriteLine(line);
45+
}
46+
return await Task.FromResult(ReturnCode.Success);
47+
}
4048

49+
private async Task<ReturnCode> WriteFileDelegate(IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct)
50+
{
51+
try
52+
{
53+
await fs.File.WriteAllLinesAsync(outputFile, lines, ct);
54+
}
55+
catch (Exception e)
56+
{
57+
return ReturnCode.GeneralError;
58+
}
59+
60+
return ReturnCode.Success;
61+
}
4162
public override async Task<int> ExecuteAsync(CommandContext context, DumpCommandSettings settings, CancellationToken cancellationToken)
4263
{
4364
var audioExtensions = DirectoryLoaderService.ComposeAudioExtensions(settings.IncludeExtensions);
@@ -47,14 +68,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, DumpCommand
4768
.ToArray();
4869
var returnCode = ReturnCode.Success;
4970

50-
var outputDelegate = async Task<ReturnCode> (IFileSystem fs, string outputFile, IEnumerable<string> lines, CancellationToken ct) =>
51-
{
52-
foreach (var line in lines)
53-
{
54-
_console.WriteLine(line);
55-
}
56-
return await Task.FromResult(ReturnCode.Success);
57-
};
71+
var outputDelegate = SpectreConsoleDelegate;
5872
var outputSuffix = "";
5973

6074

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

6882
// outputDelegate = async lines => ;
69-
outputDelegate = async Task<ReturnCode> (fs, outputFile, lines, ct) =>
70-
{
71-
try
72-
{
73-
await fs.File.WriteAllLinesAsync(outputFile, lines, ct);
74-
}
75-
catch (Exception e)
76-
{
77-
return ReturnCode.GeneralError;
78-
}
79-
80-
return ReturnCode.Success;
81-
};
83+
outputDelegate = WriteFileDelegate;
84+
8285
outputSuffix = settings.Format switch
8386
{
8487
SerializerFormat.Json => ToneJsonTagger.DefaultFileSuffix,

tone/Services/SpectreConsoleService.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,31 @@ public SpectreConsoleService(IAnsiConsole? stdout = null, IAnsiConsole? stderr =
1818
});
1919

2020
// fix unwanted wrapping if output is redirected
21-
if (Console.IsOutputRedirected)
21+
if (ShouldAdjustOutputWidth())
2222
{
2323
Output.Profile.Width = int.MaxValue;
2424
}
2525

26-
if (Console.IsErrorRedirected)
26+
if (ShouldAdjustErrorWidth())
2727
{
2828
Error.Profile.Width = int.MaxValue;
2929
}
3030
}
3131

32+
private static bool ShouldAdjustOutputWidth() => Environment.GetEnvironmentVariable("TONE_OUTPUT_REDIRECT") switch
33+
{
34+
"0" => false,
35+
"1" => true,
36+
_ => Console.IsOutputRedirected
37+
};
38+
39+
private static bool ShouldAdjustErrorWidth() => Environment.GetEnvironmentVariable("TONE_OUTPUT_REDIRECT") switch
40+
{
41+
"0" => false,
42+
"1" => true,
43+
_ => Console.IsErrorRedirected
44+
};
45+
3246
public Profile Profile => Output.Profile;
3347
public IAnsiConsoleCursor Cursor => Output.Cursor;
3448
public IAnsiConsoleInput Input => Output.Input;

0 commit comments

Comments
 (0)