GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Decide the console state instead of catching the throw (#259) · NosCoreIO/NosCore.Shared@41c90d3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/NosCore.Shared
    • Helpers
      • ConsoleHelper.cs
    • I18N
      • Logger.cs
    • NosCore.Shared.csproj
  • test/NosCore.Shared.Tests
    • ConsoleHelperTests.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/NosCore.Shared/Helpers/ConsoleHelper.cs‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// __ _ __ __ ___ __ ___ ___
2+
// | \| |/__\ /' _/ / _//__\| _ \ __|
3+
// | | ' | \/ |`._`.| \_| \/ | v / _|
4+
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
5+
// -----------------------------------
6+
7+
using System;
8+
using System.Runtime.InteropServices;
9+
10+
namespace NosCore.Shared.Helpers
11+
{
12+
public static class ConsoleHelper
13+
{
14+
// Console.WindowWidth reads the screen buffer through stdout, then stderr. Its third
15+
// try, stdin, cannot answer: a console input handle has no screen buffer, so a process
16+
// whose output is piped while stdin is still on the console has no width to read.
17+
public static bool HasWindowSize => !Console.IsOutputRedirected || !Console.IsErrorRedirected;
18+
19+
public static void SetTitle(string title)
20+
{
21+
if (OperatingSystem.IsWindows() && CanCarryTitle)
22+
{
23+
Console.Title = title;
24+
}
25+
}
26+
27+
public static void AppendTitle(string suffix)
28+
{
29+
if (OperatingSystem.IsWindows() && CanCarryTitle)
30+
{
31+
Console.Title += suffix;
32+
}
33+
}
34+
35+
public static bool CanCarryTitle => !Console.IsOutputRedirected && GetConsoleWindow() != IntPtr.Zero;
36+
37+
[DllImport("kernel32.dll")]
38+
private static extern IntPtr GetConsoleWindow();
39+
}
40+
}
Collapse file

‎src/NosCore.Shared/I18N/Logger.cs‎

Copy file name to clipboardExpand all lines: src/NosCore.Shared/I18N/Logger.cs
+4-9Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
using System;
88
using System.Globalization;
9-
using System.IO;
109
using Microsoft.Extensions.Configuration;
10+
using NosCore.Shared.Helpers;
1111
using Serilog;
1212

1313
namespace NosCore.Shared.I18N
@@ -57,14 +57,9 @@ public static void PrintHeader(string text)
5757

5858
private static int GetWindowWidth()
5959
{
60-
try
61-
{
62-
return Console.WindowHeight > 0 ? Console.WindowWidth : HeadlessWindowWidth;
63-
}
64-
catch (IOException)
65-
{
66-
return HeadlessWindowWidth;
67-
}
60+
return ConsoleHelper.HasWindowSize && Console.WindowHeight > 0
61+
? Console.WindowWidth
62+
: HeadlessWindowWidth;
6863
}
6964
}
7065
}
Collapse file

‎src/NosCore.Shared/NosCore.Shared.csproj‎

Copy file name to clipboardExpand all lines: src/NosCore.Shared/NosCore.Shared.csproj
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/NosCoreIO/NosCore.Dao.git</RepositoryUrl>
1313
<PackageIconUrl></PackageIconUrl>
1414
<PackageTags>nostale, noscore, nostale private server source, nostale emulator</PackageTags>
15-
<Version>6.0.1</Version>
15+
<Version>6.0.2</Version>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
<Description>NosCore's Shared Components</Description>
1818
<PackageLicenseExpression></PackageLicenseExpression>
Collapse file

‎test/NosCore.Shared.Tests/ConsoleHelperTests.cs‎

Copy file name to clipboard
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// __ _ __ __ ___ __ ___ ___
2+
// | \| |/__\ /' _/ / _//__\| _ \ __|
3+
// | | ' | \/ |`._`.| \_| \/ | v / _|
4+
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
5+
// -----------------------------------
6+
7+
using System;
8+
using System.IO;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
using NosCore.Shared.Helpers;
11+
using NosCore.Shared.I18N;
12+
13+
namespace NosCore.Shared.Tests
14+
{
15+
// A test process cannot choose whether it owns a console, so each case asserts the
16+
// half of the contract its own state can prove and stands aside for the other.
17+
[TestClass]
18+
public class ConsoleHelperTests
19+
{
20+
private const int HeadlessSeparatorWidth = 20;
21+
22+
[TestMethod]
23+
public void TitleIsLeftAloneWhenNothingCanCarryIt()
24+
{
25+
if (ConsoleHelper.CanCarryTitle)
26+
{
27+
Assert.Inconclusive("This process owns a console, so the title is expected to change.");
28+
}
29+
30+
var before = ReadTitle();
31+
32+
ConsoleHelper.SetTitle("NosCore");
33+
ConsoleHelper.AppendTitle(" - Port : 4000");
34+
35+
Assert.AreEqual(before, ReadTitle());
36+
}
37+
38+
[TestMethod]
39+
public void TitleIsSetWhenAConsoleCanCarryIt()
40+
{
41+
if (!ConsoleHelper.CanCarryTitle)
42+
{
43+
Assert.Inconclusive("This process owns no console to carry a title.");
44+
}
45+
46+
ConsoleHelper.SetTitle("NosCore");
47+
ConsoleHelper.AppendTitle(" - Port : 4000");
48+
49+
Assert.AreEqual("NosCore - Port : 4000", ReadTitle());
50+
}
51+
52+
[TestMethod]
53+
public void HeaderFallsBackToAFixedWidthWhenTheSizeIsUnreadable()
54+
{
55+
if (ConsoleHelper.HasWindowSize)
56+
{
57+
Assert.Inconclusive("This process can read a console width, so the fallback does not apply.");
58+
}
59+
60+
Assert.AreEqual(new string('=', HeadlessSeparatorWidth), PrintHeaderAndReadSeparator());
61+
}
62+
63+
[TestMethod]
64+
public void HeaderUsesTheConsoleWidthWhenThereIsOne()
65+
{
66+
if (!ConsoleHelper.HasWindowSize || Console.WindowHeight <= 0)
67+
{
68+
Assert.Inconclusive("This process cannot read a console width.");
69+
}
70+
71+
Assert.AreEqual(new string('=', Console.WindowWidth - 1), PrintHeaderAndReadSeparator());
72+
}
73+
74+
private static string PrintHeaderAndReadSeparator()
75+
{
76+
var captured = new StringWriter();
77+
var original = Console.Out;
78+
try
79+
{
80+
Console.SetOut(captured);
81+
Logger.PrintHeader("NosCore");
82+
}
83+
finally
84+
{
85+
Console.SetOut(original);
86+
}
87+
88+
return captured.ToString().Split('\n')[0].TrimEnd('\r');
89+
}
90+
91+
private static string? ReadTitle()
92+
{
93+
if (!OperatingSystem.IsWindows())
94+
{
95+
return null;
96+
}
97+
98+
try
99+
{
100+
return Console.Title;
101+
}
102+
catch (IOException)
103+
{
104+
return null;
105+
}
106+
}
107+
}
108+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.