ModelingDebug/ModeSwitch.cs

123 lines
3.9 KiB
C#
Raw Normal View History

2024-08-28 00:56:27 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Debug;
public class ModeSwitch
{
public event Action<int>? Enter;
private readonly string[] str = ["退出"];
private readonly int mode = 0;
public ModeSwitch(string title, string[] a, Action<int> Enter)
{
this.Enter = Enter;
str = a;
2024-10-12 02:47:46 +00:00
PrintCentered(title);
int windowWidth = Console.WindowWidth;
2024-08-28 00:56:27 +00:00
for (int i = 0; i < str.Length; i++)
{
2024-10-12 02:47:46 +00:00
// string paddedString = str[i].PadLeft((windowWidth + str[i].Length) / 2).PadRight(windowWidth);
// Console.WriteLine(paddedString);
PrintCentered($"{str[i]}");
2024-08-28 00:56:27 +00:00
}
do
{
2024-10-12 02:47:46 +00:00
var A1 = CalculateActualLength($"{mode}.{str[mode]}");
Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1);
Console.Write("<<");
Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1);
Console.Write(">>");
2024-08-28 00:56:27 +00:00
var key = Console.ReadKey(true);
2024-10-12 02:47:46 +00:00
Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1);
Console.Write(" ");
Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1);
Console.Write(" ");
2024-09-24 07:22:11 +00:00
// Console.Write(key.Key);
2024-08-28 00:56:27 +00:00
if (key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9)
{
int v1 = int.Parse(key.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
mode = v1;
}
else
switch (key.Key)
{
case ConsoleKey.UpArrow:
2024-08-29 01:05:13 +00:00
case ConsoleKey.W:
2024-08-28 00:56:27 +00:00
mode--;
break;
2024-08-29 01:05:13 +00:00
case ConsoleKey.S:
2024-08-28 00:56:27 +00:00
case ConsoleKey.DownArrow:
mode++;
break;
case ConsoleKey.Enter:
Enter?.Invoke(mode);
return;
default:
break;
}
mode = int.Max(0, int.Min(str.Length - 1, mode));
} while (true);
}
private static int CalculateActualLength(string text)
{
Encoding encoding = Encoding.UTF8;
int length = 0;
foreach (char c in text)
{
byte[] bytes = encoding.GetBytes(c.ToString());
if (bytes.Length > 1) // 汉字或宽字符
length += 2;
else
length += 1;
}
return length;
}
public static void New(string title, string[] a, Action<int> Enter)
{
_ = new ModeSwitch(title, a, Enter);
}
2024-10-12 02:47:46 +00:00
static void PrintCentered(string text)
{
// 获取控制台窗口的宽度
int consoleWidth = Console.WindowWidth;
// 计算文本的宽度,对于多字节字符(如中文),可能需要特殊处理
int textLength = GetTextDisplayLength(text);
// 计算左右两侧应该填充的空格数
int padding = (consoleWidth - textLength) / 2;
if (padding > 0)
{
// 打印左侧空格
Console.Write(new string(' ', padding));
}
// 打印文本
Console.WriteLine(text);
}
static int GetTextDisplayLength(string text)
{
// 默认每个字符占一个位置包括ASCII字符和非ASCII字符如中文
// return text.Length;
2024-08-28 00:56:27 +00:00
2024-10-12 02:47:46 +00:00
// * 如果需要更精确地计算字符宽度(例如考虑全角半角字符的区别),可以使用以下代码:
int length = 0;
foreach (char c in text)
{
// 假设所有非ASCII字符都是双宽度字符
length += char.IsHighSurrogate(c) || char.IsLowSurrogate(c) || c > 127 ? 2 : 1;
}
return length;
}
2024-08-28 00:56:27 +00:00
}