From 74e25fe41a8241893956437dd476ec14740faaac Mon Sep 17 00:00:00 2001 From: BruceChen Date: Sun, 4 Sep 2022 19:13:08 +0800 Subject: [PATCH 1/2] Fix string length calculation --- .../ConsoleInteractive/ConsoleWriter.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs b/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs index ad52420..a4501df 100644 --- a/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs +++ b/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs @@ -36,9 +36,14 @@ public static void WriteLineFormatted(string value) { internal static class InternalWriter { private static void Write(string value) { - int linesAdded = 0; + List lineLens = new(); foreach (string line in value.Split('\n')) - linesAdded += (line.Length / InternalContext.CursorLeftPosLimit) + 1; + { + int lineLen = line.Length; + foreach (Match colorCode in Regex.Matches(line, @"\u001B\[\d+m")) + lineLen -= colorCode.Groups[0].Length; + lineLens.Add(Math.Max(0, lineLen - 1)); + } lock (InternalContext.WriteLock) { @@ -58,7 +63,11 @@ private static void Write(string value) { ConsoleBuffer.ClearCurrentLine(); Console.WriteLine(value); - + + int linesAdded = 0; + foreach (int lineLen in lineLens) + linesAdded += (lineLen / InternalContext.CursorLeftPosLimit) + 1; + // Determine if we need to use the previous top position. // i.e. vertically constrained. if (InternalContext.CurrentCursorTopPos + linesAdded >= InternalContext.CursorTopPosLimit) From 4ffa21d3f4d71488e64d844f9a63ab4d2754e4b5 Mon Sep 17 00:00:00 2001 From: BruceChen Date: Mon, 5 Sep 2022 10:09:21 +0800 Subject: [PATCH 2/2] Trim --- ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs b/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs index a4501df..393e5d0 100644 --- a/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs +++ b/ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs @@ -36,13 +36,13 @@ public static void WriteLineFormatted(string value) { internal static class InternalWriter { private static void Write(string value) { - List lineLens = new(); + int linesAdded = 0; foreach (string line in value.Split('\n')) { int lineLen = line.Length; foreach (Match colorCode in Regex.Matches(line, @"\u001B\[\d+m")) lineLen -= colorCode.Groups[0].Length; - lineLens.Add(Math.Max(0, lineLen - 1)); + linesAdded += (Math.Max(0, lineLen - 1) / InternalContext.CursorLeftPosLimit) + 1; } lock (InternalContext.WriteLock) { @@ -64,10 +64,6 @@ private static void Write(string value) { Console.WriteLine(value); - int linesAdded = 0; - foreach (int lineLen in lineLens) - linesAdded += (lineLen / InternalContext.CursorLeftPosLimit) + 1; - // Determine if we need to use the previous top position. // i.e. vertically constrained. if (InternalContext.CurrentCursorTopPos + linesAdded >= InternalContext.CursorTopPosLimit)