summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-01-16 18:01:19 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-01-16 18:01:19 -0500
commit1b9ff9cd101621cacc246fc2b7d936f964ae4b3f (patch)
treeff742b2a12270b57c28e8ddecdc5322c64f6c2ce /src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
parent8a2618d812e4e2f36161907af5af5c484eb37abf (diff)
parentad0e6b315dc7b4e616dcdf6c090cd54a2f3b7499 (diff)
downloadSMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.tar.gz
SMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.tar.bz2
SMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Logging/InterceptingTextWriter.cs')
-rw-r--r--src/SMAPI/Framework/Logging/InterceptingTextWriter.cs40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs b/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
index d99f1dd2..bad69a2a 100644
--- a/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
+++ b/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
@@ -8,15 +8,11 @@ namespace StardewModdingAPI.Framework.Logging
internal class InterceptingTextWriter : TextWriter
{
/*********
- ** Fields
+ ** Accessors
*********/
/// <summary>Prefixing a message with this character indicates that the console interceptor should write the string without intercepting it. (The character itself is not written.)</summary>
- private readonly char IgnoreChar;
-
+ public const char IgnoreChar = '\u200B';
- /*********
- ** Accessors
- *********/
/// <summary>The underlying console output.</summary>
public TextWriter Out { get; }
@@ -26,30 +22,48 @@ namespace StardewModdingAPI.Framework.Logging
/// <summary>The event raised when a message is written to the console directly.</summary>
public event Action<string> OnMessageIntercepted;
+ /// <summary>Whether the text writer should ignore the next input if it's a newline.</summary>
+ /// <remarks>This is used when log output is suppressed from the console, since <c>Console.WriteLine</c> writes the trailing newline as a separate call.</remarks>
+ public bool IgnoreNextIfNewline { get; set; }
+
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="output">The underlying output writer.</param>
- /// <param name="ignoreChar">Prefixing a message with this character indicates that the console interceptor should write the string without intercepting it. (The character itself is not written.)</param>
- public InterceptingTextWriter(TextWriter output, char ignoreChar)
+ public InterceptingTextWriter(TextWriter output)
{
this.Out = output;
- this.IgnoreChar = ignoreChar;
}
/// <inheritdoc />
public override void Write(char[] buffer, int index, int count)
{
- if (buffer.Length == 0)
+ // track newline skip
+ bool ignoreIfNewline = this.IgnoreNextIfNewline;
+ this.IgnoreNextIfNewline = false;
+
+ // get first character if valid
+ if (count == 0 || index < 0 || index >= buffer.Length)
+ {
this.Out.Write(buffer, index, count);
- else if (buffer[0] == this.IgnoreChar)
+ return;
+ }
+ char firstChar = buffer[index];
+
+ // handle output
+ if (firstChar == InterceptingTextWriter.IgnoreChar)
this.Out.Write(buffer, index + 1, count - 1);
- else if (this.IsEmptyOrNewline(buffer))
+ else if (char.IsControl(firstChar) && firstChar is not ('\r' or '\n'))
this.Out.Write(buffer, index, count);
+ else if (this.IsEmptyOrNewline(buffer))
+ {
+ if (!ignoreIfNewline)
+ this.Out.Write(buffer, index, count);
+ }
else
- this.OnMessageIntercepted?.Invoke(new string(buffer, index, count).TrimEnd('\r', '\n'));
+ this.OnMessageIntercepted?.Invoke(new string(buffer, index, count));
}
/// <inheritdoc />