ModelingDebug/ModeSwitch.cs

146 lines
4.8 KiB
C#
Raw Normal View History

2024-08-28 00:56:27 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2025-01-04 11:26:18 +00:00
using System.Reflection.Metadata;
2024-08-28 00:56:27 +00:00
using System.Text;
using System.Threading.Tasks;
namespace Debug;
public class ModeSwitch
{
public event Action<int>? Enter;
private readonly string[] str = ["退出"];
2025-01-04 11:26:18 +00:00
private int mode = 0;
// private bool zhuyi = false;//是否显示提醒词
public ModeSwitch(string title, string[] a, Action<int> Enter) : this(title, a, Enter, string.Empty)
2024-08-28 00:56:27 +00:00
{
2025-01-04 11:26:18 +00:00
}
public ModeSwitch(string title, string[] a, Action<int> Enter, string message)
{
this.message = message;
2024-08-28 00:56:27 +00:00
this.Enter = Enter;
str = a;
2025-01-04 11:26:18 +00:00
new Thread(() =>
2024-08-28 00:56:27 +00:00
{
2025-01-04 11:26:18 +00:00
// Thread logic here
PrintCentered(title);
int windowWidth = Console.WindowWidth;
for (int i = 0; i < str.Length; i++)
2024-08-28 00:56:27 +00:00
{
2025-01-04 11:26:18 +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
}
2025-01-04 11:26:18 +00:00
// 判断message是不是为空
if (message != string.Empty)
{
for (int i = 0; i < windowWidth; i++)
{
Console.Write('-');
}
Console.WriteLine();
PrintCentered("按W/S或↑/↓选择或数字键选择按Enter确定");
Console.Write(message);
}
do
{
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(">>");
var key = Console.ReadKey(true);
Console.SetCursorPosition(windowWidth / 2 - A1, mode + 1);
Console.Write(" ");
Console.SetCursorPosition(windowWidth / 2 + A1 - 2, mode + 1);
Console.Write(" ");
// Console.Write(key.Key);
if (key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9)
2024-08-28 00:56:27 +00:00
{
2025-01-04 11:26:18 +00:00
int v1 = int.Parse(key.KeyChar.ToString());
if (v1 >= 0 && v1 < str.Length)
mode = v1;
2024-08-28 00:56:27 +00:00
}
2025-01-04 11:26:18 +00:00
else
switch (key.Key)
{
case ConsoleKey.UpArrow:
case ConsoleKey.W:
mode--;
break;
case ConsoleKey.S:
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);
}).Start();
2024-08-28 00:56:27 +00:00
}
2025-01-04 11:26:18 +00:00
private string message = "";
2024-08-28 00:56:27 +00:00
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
}