-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
namespace ConsoleHelpers; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace ConsoleHelpers; | ||
|
||
public static class Helpers { | ||
public static int WriteToStdOut<T>(Func<T, Stream, long> func, T input, Stream? stdout = null) { | ||
try { | ||
stdout ??= Console.OpenStandardOutput(); | ||
try { | ||
func(input, stdout); | ||
} catch { | ||
return 4; | ||
} finally { | ||
stdout.Dispose(); | ||
} | ||
} catch { | ||
return 3; | ||
public static readonly string? debugEnv = Environment.GetEnvironmentVariable("Base16384_Net_Debug"); | ||
public static readonly bool DEBUG = debugEnv is not null && debugEnv.ToLower() is not "false" and not "0"; | ||
|
||
|
||
public static int PrintErrorMessage(string? message, string? debugMessage = null, int exitCode = 0) { | ||
if (message is not null) { | ||
Console.WriteLine(message); | ||
} | ||
return 0; | ||
if (DEBUG && !string.IsNullOrWhiteSpace(debugMessage)) { | ||
Console.Error.WriteLine(debugMessage); | ||
} | ||
return exitCode; | ||
} | ||
|
||
public static int WriteToFile<T>(Func<T, FileInfo, long> func, T input, string inputName, FileInfo output) { | ||
Console.Write($"{inputName} -> {output.Name} ... "); | ||
public static int PrintException(string? message, [NotNull] Exception e, int exitCode = 0) => | ||
PrintErrorMessage(message, e.ToString(), exitCode); | ||
|
||
|
||
public static int Execute<TIn, TOut>(Func<TIn, TOut, long> func, TIn input, TOut output, string? inputName, string? outputName, bool isStdOut = false) { | ||
if (!isStdOut) { | ||
Console.Write($"{inputName} -> {outputName} ... "); | ||
} | ||
try { | ||
func(input, output); | ||
} catch { | ||
Console.WriteLine("Failed."); | ||
return 4; | ||
} catch (Exception e) { | ||
return PrintException(isStdOut ? null : "Failed.", e, 4); | ||
} | ||
if (!isStdOut) { | ||
Console.WriteLine("Done."); | ||
} | ||
Console.WriteLine("Done."); | ||
return 0; | ||
} | ||
|
||
public static int WriteToStdOut<T>(Func<T, Stream, long> func, T input) { | ||
try { | ||
using var stdout = Console.OpenStandardOutput(); | ||
return Execute(func, input, stdout, null, null, true); | ||
} catch (Exception e) { | ||
return PrintException(null, e, 3); | ||
} | ||
} | ||
|
||
public static int WriteToFile<T>(Func<T, FileInfo, long> func, T input, FileInfo output, string inputName) => | ||
Execute(func, input, output, inputName, output.Name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters