Skip to content

Commit c0c7012

Browse files
committed
refactor(OptionsDebugger): 优化表格列宽计算逻辑并添加多语言支持
- 为表头常量添加英文翻译,支持多语言显示 - 重构列宽计算逻辑,确保表头完整显示 - 添加最小宽度限制防止压缩过小 - 优化自适应宽度调整算法,优先压缩描述和帮助文本列
1 parent b03115c commit c0c7012

1 file changed

Lines changed: 59 additions & 39 deletions

File tree

GameFrameX.Foundation.Options/OptionsDebugger.cs

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ namespace GameFrameX.Foundation.Options
88
/// </summary>
99
public static class OptionsDebugger
1010
{
11-
const string OptionHeader = "选项";
12-
const string ValueHeader = "值";
13-
const string RequiredHeader = "必需";
14-
const string TypeNameHeader = "类型";
15-
const string DescriptionHeader = "描述";
16-
const string DefaultValueHeader = "默认值";
17-
const string HelpTextHeader = "帮助文本";
18-
const string RequiredYesLabel = "是";
19-
const string RequiredNoLabel = "否";
20-
const string NoDescriptionLabel = "无描述";
21-
const string NoOptionAttributeLabel = "无选项特性";
11+
const string OptionHeader = "选项 (Option)";
12+
const string ValueHeader = "值 (Value)";
13+
const string RequiredHeader = "必需 (Required)";
14+
const string TypeNameHeader = "类型 (Type)";
15+
const string DescriptionHeader = "描述 (Description)";
16+
const string DefaultValueHeader = "默认值 (Default)";
17+
const string HelpTextHeader = "帮助文本 (Help)";
18+
const string RequiredYesLabel = "是 (Yes)";
19+
const string RequiredNoLabel = "否 (No)";
20+
const string NoDescriptionLabel = "无描述 (No Description)";
21+
const string NoOptionAttributeLabel = "无选项特性 (No Option Attribute)";
2222

2323
/// <summary>
2424
/// 打印解析完成后的选项对象
@@ -99,29 +99,48 @@ public static void PrintParsedOptions<T>(T options) where T : class
9999
}
100100

101101
// 重新基于“显示宽度”计算各列宽度,中文字符按双列宽
102-
nameWidth = Math.Max(GetDisplayWidth(OptionHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Name)) : 0);
103-
valueWidth = Math.Max(GetDisplayWidth(ValueHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Value)) : 0);
104-
requiredWidth = Math.Max(GetDisplayWidth(RequiredHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Required)) : 0);
105-
typeWidth = Math.Max(GetDisplayWidth(TypeNameHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.TypeName)) : 0);
106-
descWidth = Math.Max(GetDisplayWidth(DescriptionHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Description)) : 0);
107-
defaultWidth = Math.Max(GetDisplayWidth(DefaultValueHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.DefaultValue)) : 0);
108-
helpWidth = Math.Max(GetDisplayWidth(HelpTextHeader), rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.HelpText)) : 0);
109-
110-
// 限制每列最大宽度,防止控制台过宽
102+
int hdName = GetDisplayWidth(OptionHeader);
103+
int hdValue = GetDisplayWidth(ValueHeader);
104+
int hdRequired = GetDisplayWidth(RequiredHeader);
105+
int hdType = GetDisplayWidth(TypeNameHeader);
106+
int hdDesc = GetDisplayWidth(DescriptionHeader);
107+
int hdDefault = GetDisplayWidth(DefaultValueHeader);
108+
int hdHelp = GetDisplayWidth(HelpTextHeader);
109+
110+
nameWidth = Math.Max(hdName, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Name)) : 0);
111+
valueWidth = Math.Max(hdValue, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Value)) : 0);
112+
requiredWidth = Math.Max(hdRequired, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Required)) : 0);
113+
typeWidth = Math.Max(hdType, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.TypeName)) : 0);
114+
descWidth = Math.Max(hdDesc, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.Description)) : 0);
115+
defaultWidth = Math.Max(hdDefault, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.DefaultValue)) : 0);
116+
helpWidth = Math.Max(hdHelp, rows.Count > 0 ? rows.Max(r => GetDisplayWidth(r.HelpText)) : 0);
117+
118+
// 限制每列最大宽度,但不得小于表头显示宽度
111119
int Limit(int width, int max) => Math.Min(width, max);
112-
nameWidth = Limit(nameWidth, 24);
113-
valueWidth = Limit(valueWidth, 30);
114-
requiredWidth = Limit(requiredWidth, 2);
115-
// 允许中文表头“必需”完整显示(2字=4列宽)
116-
if (requiredWidth < GetDisplayWidth(RequiredHeader))
117-
{
118-
requiredWidth = Math.Min(GetDisplayWidth(RequiredHeader), 4);
119-
}
120-
121-
typeWidth = Limit(typeWidth, 18);
122-
descWidth = Limit(descWidth, 40);
123-
defaultWidth = Limit(defaultWidth, 20);
124-
helpWidth = Limit(helpWidth, 30);
120+
int nameMax = Math.Max(24, hdName);
121+
int valueMax = Math.Max(30, hdValue);
122+
int requiredMax = Math.Max(2, hdRequired);
123+
int typeMax = Math.Max(18, hdType);
124+
int descMax = Math.Max(40, hdDesc);
125+
int defaultMax = Math.Max(20, hdDefault);
126+
int helpMax = Math.Max(30, hdHelp);
127+
128+
nameWidth = Limit(nameWidth, nameMax);
129+
valueWidth = Limit(valueWidth, valueMax);
130+
requiredWidth = Limit(requiredWidth, requiredMax);
131+
typeWidth = Limit(typeWidth, typeMax);
132+
descWidth = Limit(descWidth, descMax);
133+
defaultWidth = Limit(defaultWidth, defaultMax);
134+
helpWidth = Limit(helpWidth, helpMax);
135+
136+
// 记录各列最小宽度(不得压缩到小于表头显示宽度)
137+
int minNameWidth = hdName;
138+
int minValueWidth = hdValue;
139+
int minRequiredWidth = hdRequired;
140+
int minTypeWidth = hdType;
141+
int minDescWidth = hdDesc;
142+
int minDefaultWidth = hdDefault;
143+
int minHelpWidth = hdHelp;
125144

126145
// 根据控制台宽度自适应整体表格宽度,确保整齐对齐
127146
int columnsCount = 7;
@@ -139,42 +158,43 @@ public static void PrintParsedOptions<T>(T options) where T : class
139158
int maxTableWidth = Math.Max(60, consoleWidth - 1);
140159
while (CalculateTotalWidth() > maxTableWidth)
141160
{
142-
if (descWidth > 16)
161+
if (descWidth > minDescWidth)
143162
{
144163
descWidth--;
145164
continue;
146165
}
147166

148-
if (helpWidth > 14)
167+
if (helpWidth > minHelpWidth)
149168
{
150169
helpWidth--;
151170
continue;
152171
}
153172

154-
if (valueWidth > 14)
173+
if (valueWidth > minValueWidth)
155174
{
156175
valueWidth--;
157176
continue;
158177
}
159178

160-
if (nameWidth > 12)
179+
if (nameWidth > minNameWidth)
161180
{
162181
nameWidth--;
163182
continue;
164183
}
165184

166-
if (typeWidth > 12)
185+
if (typeWidth > minTypeWidth)
167186
{
168187
typeWidth--;
169188
continue;
170189
}
171190

172-
if (defaultWidth > 10)
191+
if (defaultWidth > minDefaultWidth)
173192
{
174193
defaultWidth--;
175194
continue;
176195
}
177196

197+
// 已无法继续压缩而不破坏表头完整展示,退出
178198
break;
179199
}
180200

0 commit comments

Comments
 (0)