Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -99,7 +99,7 @@
/// </summary>
internal static int UserInputBufferMaxLength {
get {
int result = Console.BufferWidth - 1 - PrefixTotalLength;
int result = InternalContext.SafeBufferWidth - 1 - PrefixTotalLength;
return result > 0 ? result : 0;
}
}
Expand Down Expand Up @@ -326,13 +326,16 @@
/// Does not clear the internal buffer.
/// </summary>
internal static void ClearVisibleUserInput(int startPos = 0) {
if (!InternalContext.IsInteractiveConsole) return;
lock (InternalContext.WriteLock) {
if (startPos < Console.BufferWidth) {
Console.CursorLeft = startPos;
Console.Write(new string(' ', Math.Max(0,
PrefixTotalLength + Math.Min(UserInputBuffer.Length - BufferOutputAnchor, UserInputBufferMaxLength) - startPos)));
}
Console.CursorLeft = 0;
try {
if (startPos < Console.BufferWidth) {
Console.CursorLeft = startPos;
Console.Write(new string(' ', Math.Max(0,
PrefixTotalLength + Math.Min(UserInputBuffer.Length - BufferOutputAnchor, UserInputBufferMaxLength) - startPos)));
}
Console.CursorLeft = 0;
} catch { }

Check warning on line 338 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TS&open=AZ0UqQIxmRbbgKiJX4TS&pullRequest=25

Check warning on line 338 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TT&open=AZ0UqQIxmRbbgKiJX4TT&pullRequest=25
}
}

Expand Down Expand Up @@ -360,10 +363,10 @@
if (InternalContext.SuppressInput)
return;

if (Console.IsOutputRedirected || !ConsoleReader.DisplayUesrInput)
if (!InternalContext.IsInteractiveConsole || Console.IsOutputRedirected || !ConsoleReader.DisplayUesrInput)
return;

StringBuilder sb = new(Console.BufferWidth);
StringBuilder sb = new(InternalContext.SafeBufferWidth);
int bufMaxLen = UserInputBufferMaxLength;

int leftCursorPos;
Expand Down Expand Up @@ -412,16 +415,19 @@
}

if (startIndex == sb.Length) {
lock (InternalContext.WriteLock)
Console.CursorLeft = leftCursorPos;
lock (InternalContext.WriteLock) {
try { Console.CursorLeft = leftCursorPos; } catch { }

Check warning on line 419 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TW&open=AZ0UqQIxmRbbgKiJX4TW&pullRequest=25

Check warning on line 419 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TU&open=AZ0UqQIxmRbbgKiJX4TU&pullRequest=25
}
} else {
lock (InternalContext.WriteLock) {

Check warning on line 422 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not lock on writable field 'WriteLock', use a readonly field instead.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TY&open=AZ0UqQIxmRbbgKiJX4TY&pullRequest=25
InternalContext.SetCursorVisible(false);
Console.CursorLeft = startIndex;
Console.Write(sb.ToString(startIndex, sb.Length - startIndex));
if (leftCursorPos != sb.Length)
Console.CursorLeft = leftCursorPos;
InternalContext.SetCursorVisible(true);
try {
InternalContext.SetCursorVisible(false);
Console.CursorLeft = startIndex;
Console.Write(sb.ToString(startIndex, sb.Length - startIndex));
if (leftCursorPos != sb.Length)
Console.CursorLeft = leftCursorPos;
InternalContext.SetCursorVisible(true);
} catch { }

Check warning on line 430 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TX&open=AZ0UqQIxmRbbgKiJX4TX&pullRequest=25

Check warning on line 430 in ConsoleInteractive/ConsoleInteractive/ConsoleBuffer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQIxmRbbgKiJX4TV&open=AZ0UqQIxmRbbgKiJX4TV&pullRequest=25
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions ConsoleInteractive/ConsoleInteractive/ConsoleSuggestion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -27,7 +27,7 @@ public static class ConsoleSuggestion {
private static Suggestion[] Suggestions = Array.Empty<Suggestion>();

public static void UpdateSuggestions(Suggestion[] Suggestions, Tuple<int, int> range) {
if (Console.IsOutputRedirected) {
if (Console.IsOutputRedirected || !InternalContext.IsInteractiveConsole) {
ClearSuggestions();
return;
}
Expand All @@ -43,7 +43,7 @@ public static void UpdateSuggestions(Suggestion[] Suggestions, Tuple<int, int> r
return;
}

if (Console.BufferWidth < maxLength) {
if (InternalContext.SafeBufferWidth < maxLength) {
ClearSuggestions();
return;
}
Expand Down Expand Up @@ -348,10 +348,11 @@ private enum ColorType { Reset, Normal, NormalBg, Highlight, HighlightBg, Toolti
private static readonly BgMessageBuffer[] BgBuffer = new BgMessageBuffer[MaxValueOfMaxSuggestionCount];

internal static void DrawSuggestionPopup(bool refreshMsgBuf = true, int bufWidth = -1) {
if (!InternalContext.IsInteractiveConsole) return;
BgMessageBuffer[] messageBuffers = Array.Empty<BgMessageBuffer>();
int curBufIdx = -1, nextMessageIdx = 0;
lock (InternalContext.WriteLock) {
if (bufWidth == -1) bufWidth = Console.BufferWidth;
if (bufWidth == -1) bufWidth = InternalContext.SafeBufferWidth;
if (PopupWidth > bufWidth) return;
(int left, int top) = Console.GetCursorPosition();
LastDrawStartPos = GetDrawStartPos(bufWidth);
Expand All @@ -374,9 +375,10 @@ internal static void DrawSuggestionPopup(bool refreshMsgBuf = true, int bufWidth
}

internal static void ClearSuggestionPopup(int linesAdded = 0, int bufWidth = -1) {
if (!InternalContext.IsInteractiveConsole) return;
int DisplaySuggestionsCnt = Math.Min(MaxSuggestionCount, Suggestions.Length);
lock (InternalContext.WriteLock) {
if (bufWidth == -1) bufWidth = Console.BufferWidth;
if (bufWidth == -1) bufWidth = InternalContext.SafeBufferWidth;
int drawStartPos = GetDrawStartPos(bufWidth);
(int left, int top) = Console.GetCursorPosition();
InternalContext.SetCursorVisible(false);
Expand All @@ -390,6 +392,7 @@ internal static void ClearSuggestionPopup(int linesAdded = 0, int bufWidth = -1)
}

internal static void RedrawOnArrowKey(int offset) {
if (!InternalContext.IsInteractiveConsole) return;
lock (InternalContext.WriteLock) {
(int left, int top) = Console.GetCursorPosition();
InternalContext.SetCursorVisible(false);
Expand All @@ -404,8 +407,9 @@ internal static void RedrawOnArrowKey(int offset) {
}

internal static void RedrawOnTab() {
if (!InternalContext.IsInteractiveConsole) return;
lock (InternalContext.WriteLock) {
int bufWidth = Console.BufferWidth;
int bufWidth = InternalContext.SafeBufferWidth;
if (GetDrawStartPos(bufWidth) != LastDrawStartPos) {
ClearSuggestionPopup(bufWidth: bufWidth);
DrawSuggestionPopup(refreshMsgBuf: true, bufWidth: bufWidth);
Expand Down
28 changes: 15 additions & 13 deletions ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -14,9 +14,10 @@

public static void Init() {
SetWindowsConsoleAnsi();
if (!Console.IsOutputRedirected)
Console.Clear();
if (InDocker)
if (!Console.IsOutputRedirected && InternalContext.IsInteractiveConsole) {
try { Console.Clear(); } catch { }

Check warning on line 18 in ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQLjmRbbgKiJX4TZ&open=AZ0UqQLjmRbbgKiJX4TZ&pullRequest=25

Check warning on line 18 in ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQLjmRbbgKiJX4Ta&open=AZ0UqQLjmRbbgKiJX4Ta&pullRequest=25
}
if (InDocker || !InternalContext.IsInteractiveConsole)
BackendWriter = new FallbackWriter();
}

Expand Down Expand Up @@ -58,12 +59,12 @@
/// Gets the number of lines and the width of the first line of the message.
/// </summary>
private Tuple<int, int> GetLineCountInTerminal(string value) {
if (Console.IsOutputRedirected)
if (Console.IsOutputRedirected || !InternalContext.IsInteractiveConsole)
return new(0, 0);

bool escape = false;
int lineCnt = 0, cursorPos = 0, firstLineLength = -1;
int bufWidth = Console.BufferWidth;
int bufWidth = InternalContext.SafeBufferWidth;
foreach (char c in value) {
if (!escape && c == '\u001B') {
escape = true;
Expand Down Expand Up @@ -129,17 +130,18 @@
lock (InternalContext.WriteLock) {
ConsoleSuggestion.BeforeWrite(value, linesAdded);

if (!Console.IsOutputRedirected) {
if (InternalContext.BufferInitialized)
ConsoleBuffer.ClearVisibleUserInput(startPos: firstLineLength);
else
Console.CursorLeft = 0;
if (!Console.IsOutputRedirected && InternalContext.IsInteractiveConsole) {
try {
if (InternalContext.BufferInitialized)
ConsoleBuffer.ClearVisibleUserInput(startPos: firstLineLength);
else
Console.CursorLeft = 0;
} catch { }

Check warning on line 139 in ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQLjmRbbgKiJX4Tc&open=AZ0UqQLjmRbbgKiJX4Tc&pullRequest=25

Check warning on line 139 in ConsoleInteractive/ConsoleInteractive/ConsoleWriter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQLjmRbbgKiJX4Tb&open=AZ0UqQLjmRbbgKiJX4Tb&pullRequest=25
}

WriteConsole(value, colors);

if (!Console.IsOutputRedirected) {
// Only redraw if we have a buffer initialized.
if (!Console.IsOutputRedirected && InternalContext.IsInteractiveConsole) {
if (InternalContext.BufferInitialized)
ConsoleBuffer.RedrawInputArea(RedrawAll: true);
}
Expand Down
28 changes: 26 additions & 2 deletions ConsoleInteractive/ConsoleInteractive/InternalContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Text.RegularExpressions;

namespace ConsoleInteractive {
Expand All @@ -13,6 +13,28 @@
internal static volatile bool _suppressInput = false;
internal static volatile bool BufferInitialized = false;

internal static readonly bool IsInteractiveConsole = DetectInteractiveConsole();

private static bool DetectInteractiveConsole() {
if (Console.IsInputRedirected || Console.IsOutputRedirected)
return false;
try {
_ = Console.BufferWidth;
_ = Console.KeyAvailable;
return true;
} catch {
return false;
}
}

internal static int SafeBufferWidth {
get {
if (!IsInteractiveConsole) return 120;
try { return Console.BufferWidth; }
catch { return 120; }
}
}

internal static bool SuppressInput {
get { return _suppressInput; }
set {
Expand All @@ -26,9 +48,11 @@
}

internal static void SetCursorVisible(bool visible) {
if (!IsInteractiveConsole) return;

// It's useful to have the cursor visible in debug situations
#if !DEBUG
Console.CursorVisible = visible;
try { Console.CursorVisible = visible; } catch { }

Check warning on line 55 in ConsoleInteractive/ConsoleInteractive/InternalContext.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove or fill this block of code.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQMGmRbbgKiJX4Te&open=AZ0UqQMGmRbbgKiJX4Te&pullRequest=25

Check warning on line 55 in ConsoleInteractive/ConsoleInteractive/InternalContext.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle the exception or explain in a comment why it can be ignored.

See more on https://sonarcloud.io/project/issues?id=breadbyte_ConsoleInteractive&issues=AZ0UqQMGmRbbgKiJX4Td&open=AZ0UqQMGmRbbgKiJX4Td&pullRequest=25
#endif
}

Expand Down