Skip to content

Commit 42fd2f8

Browse files
committed
Fixing AdjustWindowSize when window has 0 height.
1 parent 76b3f01 commit 42fd2f8

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

ConsoleGUI/Api/SimplifiedConsole.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public override void Write(Position position, in Character character)
2121

2222
if (content == '\n') content = ' ';
2323

24-
Console.SetCursorPosition(position.X, position.Y);
25-
Console.BackgroundColor = ColorConverter.GetNearestConsoleColor(background);
26-
Console.ForegroundColor = ColorConverter.GetNearestConsoleColor(foreground);
27-
Console.Write(content);
24+
SafeConsole.WriteOrThrow(
25+
position.X,
26+
position.Y,
27+
ColorConverter.GetNearestConsoleColor(background),
28+
ColorConverter.GetNearestConsoleColor(foreground),
29+
content);
2830
}
2931

3032
public override void OnRefresh()

ConsoleGUI/Api/StandardConsole.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public virtual void Write(Position position, in Character character)
4545

4646
if (content == '\n') content = ' ';
4747

48-
Console.SetCursorPosition(position.X, position.Y);
49-
Console.Write($"\x1b[38;2;{foreground.Red};{foreground.Green};{foreground.Blue}m\x1b[48;2;{background.Red};{background.Green};{background.Blue}m{content}");
48+
SafeConsole.WriteOrThrow(position.X, position.Y, $"\x1b[38;2;{foreground.Red};{foreground.Green};{foreground.Blue}m\x1b[48;2;{background.Red};{background.Green};{background.Blue}m{content}");
5049
}
5150

5251
public ConsoleKeyInfo ReadKey()

ConsoleGUI/ConsoleManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private static void Update(Rect rect)
147147
{
148148
Console.Write(position, cell.Character);
149149
}
150-
catch (ArgumentOutOfRangeException)
150+
catch (SafeConsoleException)
151151
{
152152
rect = Rect.Intersect(rect, Rect.OfSize(WindowSize));
153153
}

ConsoleGUI/Utils/SafeConsole.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using ConsoleGUI.Data;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

@@ -46,6 +47,34 @@ public static void SetBufferSize(int width, int height)
4647
{ }
4748
}
4849

50+
public static void WriteOrThrow(int left, int top, string content)
51+
{
52+
try
53+
{
54+
Console.SetCursorPosition(left, top);
55+
Console.Write(content);
56+
}
57+
catch (Exception)
58+
{
59+
throw new SafeConsoleException();
60+
}
61+
}
62+
63+
public static void WriteOrThrow(int left, int top, ConsoleColor background, ConsoleColor foreground, char content)
64+
{
65+
try
66+
{
67+
Console.SetCursorPosition(left, top);
68+
Console.BackgroundColor = background;
69+
Console.ForegroundColor = foreground;
70+
Console.Write(content);
71+
}
72+
catch (Exception)
73+
{
74+
throw new SafeConsoleException();
75+
}
76+
}
77+
4978
public static void SetUtf8()
5079
{
5180
try
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ConsoleGUI.Utils
6+
{
7+
internal sealed class SafeConsoleException : Exception
8+
{ }
9+
}

0 commit comments

Comments
 (0)