-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_utils.py
More file actions
641 lines (524 loc) · 22 KB
/
ui_utils.py
File metadata and controls
641 lines (524 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
"""
UI工具模块 - 使用Rich库美化命令行界面
"""
from typing import List, Dict, Any, Optional
import json
import sys
import os
import subprocess
import tempfile
import shutil
try:
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.text import Text
from rich.markdown import Markdown
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn
from rich.prompt import Prompt, Confirm
from rich.syntax import Syntax
from rich.tree import Tree
from rich.columns import Columns
from rich.align import Align
RICH_AVAILABLE = True
except ImportError:
RICH_AVAILABLE = False
class Console:
"""简化版控制台,保证在缺少 rich 时程序仍可运行"""
def __init__(self):
self.width = shutil.get_terminal_size((80, 24)).columns
def print(self, *args, **kwargs):
text = " ".join(str(arg) for arg in args)
print(text)
def clear(self):
try:
os.system("cls" if os.name == "nt" else "clear")
except Exception:
pass
def input(self, prompt=""):
return input(prompt)
class _BaseRenderable:
def __str__(self):
return getattr(self, "text", "")
def __repr__(self):
return str(self)
class Text(_BaseRenderable):
def __init__(self, text="", style=None, justify=None):
self.text = str(text)
def stylize(self, *_args, **_kwargs):
return self
@classmethod
def from_markup(cls, text):
return cls(text)
def join(self, renderables):
joined = str(self).join(str(r) for r in renderables)
return Text(joined)
class Panel(_BaseRenderable):
def __init__(self, content, title="", style=""):
self.text = f"{title} {content}" if title else str(content)
class Markdown(_BaseRenderable):
def __init__(self, content):
self.text = str(content)
class Syntax(_BaseRenderable):
def __init__(self, content, *_args, **_kwargs):
self.text = str(content)
class Table:
def __init__(self, title="", show_header=True, header_style=None):
self.title = title
self.columns = []
self.rows = []
def add_column(self, column, style=None, no_wrap=True):
self.columns.append(str(column))
def add_row(self, *values):
self.rows.append(tuple(str(v) for v in values))
def __str__(self):
lines = []
if self.title:
lines.append(self.title)
if self.columns:
header = " | ".join(self.columns)
lines.append(header)
lines.append("-" * len(header))
for row in self.rows:
lines.append(" | ".join(row))
return "\n".join(lines)
class Columns(list):
pass
class Tree(_BaseRenderable):
pass
class Align:
@staticmethod
def center(content):
return content
class SpinnerColumn:
pass
class TextColumn:
def __init__(self, *_args, **_kwargs):
pass
class BarColumn:
pass
class TaskProgressColumn:
pass
class Progress:
def __init__(self, *args, **kwargs):
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def add_task(self, description, total=1):
return 0
def update(self, task_id, advance=1, description=None):
pass
class Prompt:
@staticmethod
def ask(message, default=None, choices=None, show_choices=True, show_default=True):
prompt_parts = [str(message)]
if choices and show_choices:
prompt_parts.append(f"选项: {', '.join(choices)}")
if default is not None and show_default:
prompt_parts.append(f"默认: {default}")
prompt_text = " ".join(prompt_parts) + "\n> "
response = input(prompt_text).strip()
if not response and default is not None:
response = default
return response
class Confirm:
@staticmethod
def ask(message, default=True):
suffix = "[Y/n]" if default else "[y/N]"
response = input(f"{message} {suffix} ").strip().lower()
if not response:
return default
return response in {"y", "yes"}
# 创建全局console实例
console = Console()
# 颜色主题
class Colors:
SUCCESS = "green"
ERROR = "red"
WARNING = "yellow"
INFO = "blue"
HIGHLIGHT = "magenta"
MUTED = "dim"
ACCENT = "cyan"
class UIUtils:
"""UI工具类"""
@staticmethod
def print_success(message: str):
"""打印成功消息"""
console.print(f"✅ {message}", style=Colors.SUCCESS)
@staticmethod
def print_error(message: str):
"""打印错误消息"""
console.print(f"❌ {message}", style=Colors.ERROR)
@staticmethod
def print_warning(message: str):
"""打印警告消息"""
console.print(f"⚠️ {message}", style=Colors.WARNING)
@staticmethod
def print_info(message: str, center: bool = False):
"""打印信息消息"""
# 取消居中,直接左对齐显示
console.print(f"ℹ️ {message}", style=Colors.INFO)
@staticmethod
def print_highlight(message: str):
"""打印高亮消息"""
console.print(message, style=Colors.HIGHLIGHT)
@staticmethod
def print_muted(message: str):
"""打印灰色消息"""
console.print(message, style=Colors.MUTED)
@staticmethod
def print_panel(content: str, title: str = "", style: str = ""):
"""打印面板"""
panel = Panel(content, title=title, style=style)
console.print(panel)
@staticmethod
def print_markdown(content: str):
"""打印Markdown内容"""
md = Markdown(content)
console.print(md)
@staticmethod
def print_json(data: Dict[str, Any], title: str = ""):
"""美化打印JSON数据"""
json_str = json.dumps(data, ensure_ascii=False, indent=2)
syntax = Syntax(json_str, "json", theme="monokai", line_numbers=True)
if title:
UIUtils.print_panel(syntax, title=title)
else:
console.print(syntax)
@staticmethod
def create_table(title: str, columns: List[str]) -> Table:
"""创建表格"""
table = Table(title=title, show_header=True, header_style="bold magenta")
for column in columns:
table.add_column(column, style="cyan", no_wrap=True)
return table
@staticmethod
def print_characters_table(characters: Dict[str, Dict[str, Any]]):
"""打印角色表格"""
if not characters:
UIUtils.print_warning("暂无角色数据")
return
table = UIUtils.create_table("🎭 角色列表", ["序号", "角色名", "描述预览", "创建时间"])
for i, (name, char_data) in enumerate(characters.items(), 1):
description = char_data.get("description", "")
# 截取描述的前50个字符
desc_preview = (description[:50] + "...") if len(description) > 50 else description
created_at = char_data.get("created_at", "未知")
table.add_row(
str(i),
f"[bold]{name}[/bold]",
desc_preview,
created_at
)
console.print(table)
@staticmethod
def print_locations_table(locations: Dict[str, Dict[str, Any]]):
"""打印场景表格"""
if not locations:
UIUtils.print_warning("暂无场景数据")
return
table = UIUtils.create_table("🏞️ 场景列表", ["序号", "场景名", "描述预览", "创建时间"])
for i, (name, loc_data) in enumerate(locations.items(), 1):
description = loc_data.get("description", "")
desc_preview = (description[:50] + "...") if len(description) > 50 else description
created_at = loc_data.get("created_at", "未知")
table.add_row(
str(i),
f"[bold]{name}[/bold]",
desc_preview,
created_at
)
console.print(table)
@staticmethod
def print_items_table(items: Dict[str, Dict[str, Any]]):
"""打印道具表格"""
if not items:
UIUtils.print_warning("暂无道具数据")
return
table = UIUtils.create_table("⚔️ 道具列表", ["序号", "道具名", "描述预览", "创建时间"])
for i, (name, item_data) in enumerate(items.items(), 1):
description = item_data.get("description", "")
desc_preview = (description[:50] + "...") if len(description) > 50 else description
created_at = item_data.get("created_at", "未知")
table.add_row(
str(i),
f"[bold]{name}[/bold]",
desc_preview,
created_at
)
console.print(table)
@staticmethod
def print_chapters_table(chapters: List[Dict[str, Any]]):
"""打印章节表格"""
if not chapters:
UIUtils.print_warning("暂无章节数据")
return
table = UIUtils.create_table("📚 章节列表", ["章节", "标题", "大纲预览", "创建时间"])
for chapter in chapters:
title = chapter.get("title", "")
outline = chapter.get("outline", "")
outline_preview = (outline[:60] + "...") if len(outline) > 60 else outline
created_at = chapter.get("created_at", "未知")
order = chapter.get("order", 0)
table.add_row(
f"第{order}章",
f"[bold]{title}[/bold]",
outline_preview,
created_at
)
console.print(table)
@staticmethod
def print_project_status(status_details: Dict[str, Dict]):
"""打印项目状态"""
table = UIUtils.create_table("📊 项目进度", ["步骤", "状态", "详细信息"])
steps = {
"theme_one_line": "1. 确立一句话主题",
"theme_paragraph": "2. 扩展成一段话主题",
"world_settings": "3. 世界设定",
"story_outline": "4. 编辑故事大纲",
"chapter_outline": "5. 编辑分章细纲",
"chapter_summaries": "6. 编辑章节概要",
"novel_chapters": "7. 生成小说正文"
}
completed_count = 0
total_count = len(steps)
for key, step_name in steps.items():
status_info = status_details.get(key, {"completed": False, "details": "状态未知"})
is_complete = status_info.get("completed", False)
details = status_info.get("details", "状态未知")
if is_complete:
completed_count += 1
status_icon = "✅"
status_style = Colors.SUCCESS
else:
status_icon = "⏳"
status_style = Colors.WARNING
table.add_row(
step_name,
f"[{status_style}]{status_icon}[/{status_style}]",
details
)
console.print(Align.center(table))
@staticmethod
def display_menu(title: str, options: List[str], default_choice: str = "1") -> Optional[str]:
"""
显示菜单并获取用户选择。
根据规则,"返回"或"退出"类的选项应使用序号 "0" 并显示在末尾。
此函数现在将自动查找这类选项,将其作为 "0" 选项,并调整显示。
"""
if not options:
UIUtils.print_warning("菜单选项为空")
return None
console.print(Panel(Text(title, justify="center"), border_style="bold magenta"))
exit_keywords = ["返回", "退出", "取消"]
exit_option = None
regular_options = []
# 1. 分离常规选项和退出选项
for opt in options:
is_exit_opt = any(keyword in opt for keyword in exit_keywords)
if is_exit_opt and not exit_option: # 只处理第一个匹配的退出选项
exit_option = opt
else:
regular_options.append(opt)
# 2. 构建菜单项文本
menu_items = []
valid_choices = []
for i, option in enumerate(regular_options, 1):
menu_items.append(f"[cyan]{i}[/cyan]. {option}")
valid_choices.append(str(i))
if exit_option:
menu_items.append(f"[cyan]0[/cyan]. {exit_option}")
valid_choices.append("0")
# 如果默认选项是退出选项,则更新
if default_choice.lower() in [o.lower() for o in options if any(k in o for k in exit_keywords)]:
default_choice = "0"
# 3. 计算对齐和显示
if not menu_items:
UIUtils.print_warning("没有可显示的菜单项。")
return None
# 创建一个Renderable的列表
renderables = [Text.from_markup(item) for item in menu_items]
# 将所有菜单项放入一个垂直堆栈(通过换行符),然后居中
menu_text = Text("\n").join(renderables)
console.print(Align.center(menu_text))
console.print() # 添加空行
# 4. 获取用户输入
# 计算左边距以对齐提示
max_width = max(r.cell_len for r in renderables) if renderables else 0
terminal_width = console.width
padding_size = (terminal_width - max_width) // 2
padding_str = " " * padding_size
prompt_text = f"{padding_str}[{Colors.ACCENT}]请选择[/]"
while True:
try:
choice = Prompt.ask(
prompt_text,
show_choices=False,
show_default=False, # 我们自己处理默认值
default=default_choice if default_choice in valid_choices else None
)
if choice in valid_choices:
return choice
else:
# 使用我们自定义的、更友好的错误提示
error_text = Text("请输入菜单项对应的数字。", style=Colors.ERROR)
# 使用与菜单项相同的左边距进行对齐
console.print(f"{padding_str}{error_text}")
console.print() # 添加空行
except KeyboardInterrupt:
# 重新抛出 KeyboardInterrupt 让上层处理
raise
@staticmethod
def pause(message: str = "按回车键继续..."):
"""暂停程序,等待用户输入"""
try:
console.input(f"[dim]{message}[/dim]")
except KeyboardInterrupt:
# 重新抛出 KeyboardInterrupt 让上层处理
raise
@staticmethod
def get_user_input(prompt_message: str, default: str = "") -> str:
"""获取用户输入,左对齐显示"""
try:
return Prompt.ask(
f"[{Colors.ACCENT}]{prompt_message}[/]",
default=default if default else None,
show_default=bool(default)
)
except KeyboardInterrupt:
# 重新抛出 KeyboardInterrupt 让上层处理
raise
@staticmethod
def print_separator(char: str = "─", length: int = 50):
"""打印分隔线"""
console.print(f"[{Colors.MUTED}]{char * length}[/{Colors.MUTED}]")
@staticmethod
def print_title(title: str):
"""打印标题"""
title_text = Text(title, style="bold magenta")
title_text.stylize("underline")
console.print(title_text, justify="center")
@staticmethod
def print_subtitle(subtitle: str):
"""打印副标题"""
console.print(f"\n{subtitle}", style="bold cyan")
@staticmethod
def confirm(message: str, default: bool = True) -> bool:
"""确认对话框"""
try:
return Confirm.ask(message, default=default)
except KeyboardInterrupt:
# 重新抛出 KeyboardInterrupt 让上层处理
raise
@staticmethod
def prompt(prompt_text: str, default: Optional[str] = None, choices: Optional[List[str]] = None, multiline: bool = False) -> Optional[str]:
"""获取用户输入,支持单行和多行模式。多行模式会打开系统默认编辑器。"""
try:
if multiline:
# --- Multi-line editing using external editor ---
if choices:
raise ValueError("多行模式不支持'choices'参数")
# 查找一个可用的文本编辑器
editor = None
# 1. 检查环境变量
env_editor = os.environ.get('VISUAL') or os.environ.get('EDITOR')
if env_editor and shutil.which(env_editor):
editor = env_editor
# 2. 如果环境变量中没有,或者编辑器不存在,则根据操作系统尝试预设列表
if not editor:
if sys.platform == "win32":
common_editors = ['notepad']
else:
common_editors = ['nvim', 'nano', 'vim', 'vi']
for e in common_editors:
if shutil.which(e):
editor = e
break
# 3. 如果还是没有找到,就报错
if not editor:
if sys.platform == "win32":
error_msg = "错误:未找到默认编辑器 (notepad)。请确保其在系统路径中。"
else:
error_msg = "错误:未找到任何可用的文本编辑器 (如 nvim, nano, vim, vi)。请安装其中一个,或设置您的 VISUAL/EDITOR 环境变量。"
console.print(f"\\n[red]{error_msg}[/red]")
return None
# 创建一个临时文件用于编辑
fd, path = tempfile.mkstemp(suffix=".txt", text=True)
try:
# 将初始内容写入临时文件
with os.fdopen(fd, 'w', encoding='utf-8') as tmpfile:
if default:
tmpfile.write(default)
# 打印操作指南
console.print(f"[bold cyan]{prompt_text}[/bold cyan]")
console.print(f"[dim]正在调用外部编辑器 ({editor})。请在编辑器中修改内容,保存并关闭以继续...[/dim]")
# 打开编辑器,并等待其关闭
subprocess.run([editor, path], check=True)
# 从文件中读回修改后的内容
with open(path, 'r', encoding='utf-8') as tmpfile:
edited_content = tmpfile.read()
return edited_content
finally:
# 确保临时文件总是被删除
os.remove(path)
else:
# --- 原始的单行输入逻辑 ---
return Prompt.ask(prompt_text, choices=choices, default=default)
except KeyboardInterrupt:
# 重新抛出 KeyboardInterrupt 让上层处理
raise
except EOFError:
console.print("\\n[yellow]操作已取消。[/yellow]")
return None
except FileNotFoundError:
console.print(f"\\n[red]错误:编辑器 '{editor}' 未找到。[/red]")
console.print("[red]请安装该编辑器,或设置您的 VISUAL/EDITOR 环境变量。[/red]")
return None
except Exception as e:
console.print(f"\\n[red]输入处理时发生错误: {e}[/red]")
return None
@staticmethod
def create_progress() -> Progress:
"""创建进度条"""
return Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TaskProgressColumn(),
console=console
)
@staticmethod
def print_welcome():
"""打印欢迎信息"""
welcome_text = """
# 🎨 MetaNovel Engine
欢迎使用MetaNovel Engine!这是一个强大的AI辅助小说创作工具,
帮助您从一句话主题开始,逐步完成完整的小说创作。
"""
UIUtils.print_markdown(welcome_text)
@staticmethod
def print_goodbye():
"""打印再见信息"""
goodbye_text = """
# 👋 感谢使用 MetaNovel Engine
期待您的下次使用!如果您有任何问题或建议,
请访问项目GitHub页面:https://github.com/hahagood/MetaNovel-Engine
祝您创作愉快!✨
"""
UIUtils.print_markdown(goodbye_text)
@staticmethod
def print_error_details(error: Exception, context: str = ""):
"""打印详细错误信息"""
error_panel = Panel(
f"[red]错误类型:[/red] {type(error).__name__}\n"
f"[red]错误信息:[/red] {str(error)}\n"
f"[red]上下文:[/red] {context}" if context else f"[red]错误信息:[/red] {str(error)}",
title="❌ 错误详情",
style="red"
)
console.print(error_panel)
# 全局UI工具实例
ui = UIUtils()