11import atexit
22import logging
33import os
4- import random
54import sys
65import threading
76import time
87
8+ from rich .console import Console
9+ from rich .live import Live
10+ from rich .spinner import Spinner
11+ from rich .style import Style
12+ from rich .text import Text
13+
914from murainbot import paths
1015
11- is_done = False
16+ console = Console ()
1217
1318BANNER = r""" __ __ ____ _ ____ _ _____
1419| \/ |_ _| _ \ __ _(_)_ __ | __ ) ___ | |_|___ \
2126banner_end_color = (255 , 66 , 179 )
2227
2328
24- def color_text (text : str , text_color : tuple [int , int , int ] = None , bg_color : tuple [int , int , int ] = None ):
25- """
26- 富文本生成器
27- @param text: 文本
28- @param text_color: 文本颜色
29- @param bg_color: 背景颜色
30- @return: 富文本
31- """
32- text = text + "\033 [0m" if text_color is not None or bg_color is not None else text
33- if text_color is not None :
34- text = f"\033 [38;2;{ text_color [0 ]} ;{ text_color [1 ]} ;{ text_color [2 ]} m" + text
35- if bg_color is not None :
36- text = f"\033 [48;2;{ bg_color [0 ]} ;{ bg_color [1 ]} ;{ bg_color [2 ]} m" + text
37- return text
38-
39-
4029def get_gradient (start_color : tuple [int , int , int ], end_color : tuple [int , int , int ], length : float ):
4130 """
4231 渐变色生成
@@ -52,51 +41,38 @@ def get_gradient(start_color: tuple[int, int, int], end_color: tuple[int, int, i
5241 )
5342
5443
55- def print_loading ( wait_str ):
44+ def start ( work_path = os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ))) ):
5645 """
57- 输出加载动画
46+ 启动MRB2
47+ Args:
48+ work_path: MRB2实例的工作目录,默认为安装目录,谨慎填写
5849 """
59- loading_string_list = [r"|/-\\" , r"▁▂▃▄▅▆▇█▇▆▅▄▃▂" , "\u2801 \u2808 \u2810 \u2820 \u2880 \u2900 \u2804 \u2802 " , r"←↖↑↗→↘↓↙" ]
60- loading_string = random .choice (loading_string_list )
61- i = 0
62- while not is_done :
63- if i == len (loading_string ):
64- i = 0
65- print ("\r " + wait_str + color_text (loading_string [i ], banner_start_color ), end = "" )
66- time .sleep (0.07 )
67- i += 1
68-
69-
70- def start (work_path = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))):
71- global is_done
7250 paths .init_paths (work_path )
7351 paths .paths .ensure_all_dirs_exist ()
74- banner = BANNER .split ("\n " )
75- color_banner = ""
76- # 输出banner
77- for i in range (len (banner )):
78- for j in range (len (banner [i ])):
79- color_banner += color_text (
80- banner [i ][j ],
81- get_gradient (
82- banner_start_color ,
83- banner_end_color ,
84- ((j / (len (banner [i ]) - 1 ) + i / (len (banner ) - 1 )) / 2 )
85- )
86- )
87- color_banner += "\n "
88-
89- print (color_banner .strip ())
90-
91- # 输出项目链接
92- for c in color_text (BANNER_LINK , get_gradient (banner_start_color , banner_end_color , 0.5 )):
93- print (c , end = "" )
94-
95- wait_str = color_text ("正在加载 Lib, 首次启动可能需要几秒钟,请稍等..." , banner_start_color )
96- print ("\n " + wait_str , end = "" )
97-
98- threading .Thread (target = print_loading , daemon = True , args = (wait_str ,)).start ()
9952
53+ banner_lines = BANNER .split ("\n " )
54+ rich_banner = Text ()
55+ for i , line in enumerate (banner_lines ):
56+ for j , char in enumerate (line ):
57+ # 计算当前字符的渐变颜色
58+ gradient_pos = ((j / (len (line ) - 1 ) + i / (len (banner_lines ) - 1 )) / 2 )
59+ r , g , b = get_gradient (banner_start_color , banner_end_color , gradient_pos )
60+ # 使用 rich.style.Style 添加颜色
61+ style = Style (color = f"rgb({ r } ,{ g } ,{ b } )" )
62+ rich_banner .append (char , style = style )
63+ rich_banner .append ("\n " )
64+
65+ console .print (rich_banner , end = "" )
66+
67+ link_color = get_gradient (banner_start_color , banner_end_color , 0.5 )
68+ link_text = Text (BANNER_LINK , style = f"rgb({ link_color [0 ]} ,{ link_color [1 ]} ,{ link_color [2 ]} )" )
69+ link_text .stylize (f"link { BANNER_LINK } " )
70+ console .print (link_text )
71+
72+ loading_text = f"[rgb({ banner_start_color [0 ]} ,{ banner_start_color [1 ]} ,{ banner_start_color [2 ]} )]正在加载 Lib, 首次启动可能需要几秒钟,请稍等...[/]"
73+
74+ live = Live (Spinner ("dots" , text = loading_text ), console = console , transient = True )
75+ live .start ()
10076 # 开始加载
10177 start_loading = time .time ()
10278
@@ -114,14 +90,13 @@ def start(work_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11490 from .utils import AutoRestartOnebot
11591
11692 Logger .set_logger_level (logging .DEBUG if ConfigManager .GlobalConfig ().debug .enable else logging .INFO )
93+ live .stop ()
11794
118- is_done = True
119-
120- print ("\r " + color_text (
121- f"Lib 加载完成!耗时: { round (time .time () - start_loading , 2 )} s 正在启动 MuRainBot... " ,
122- banner_end_color
95+ # Live 动画结束后,打印加载完成信息
96+ end_color_str = f"rgb({ banner_end_color [0 ]} ,{ banner_end_color [1 ]} ,{ banner_end_color [2 ]} )"
97+ console .print (
98+ f"[{ end_color_str } ]Lib 加载完成!耗时: { round (time .time () - start_loading , 2 )} s 正在启动 MuRainBot...[/]"
12399 )
124- )
125100
126101 logger = Logger .get_logger ()
127102
0 commit comments