From 2e9e3afc3917104734313780ffcd336ae134a039 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Mon, 21 Aug 2023 00:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20options=20=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E6=97=A0=E9=99=90=E5=BE=AA=E7=8E=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Difficult_Rocket/utils/options.py | 32 +++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Difficult_Rocket/utils/options.py b/Difficult_Rocket/utils/options.py index d5e1b02..fa24970 100644 --- a/Difficult_Rocket/utils/options.py +++ b/Difficult_Rocket/utils/options.py @@ -197,6 +197,7 @@ class Options: def as_markdown(self, longest: Optional[int] = None) -> str: """ 返回一个 markdown 格式的 option 字符串 + :param longest: 最长的输出长度 :return: markdown 格式的 option 字符串 """ value = self.option_with_len() @@ -214,29 +215,36 @@ class Options: console_width = shutil.get_terminal_size(fallback=(100, 80)).columns console_width = max(console_width, shortest) + # 为每一栏 预分配 1/3 或者 需要的宽度 (如果不需要 1/3) option_len = min(option_len, console_width // 3) value_len = min(value_len, console_width // 3) value_type_len = min(value_type_len, console_width // 3) + # 先指定每一个列的输出最窄宽度, 然后去尝试增加宽度 - while option_len + value_len + value_type_len + 16 < console_width: + # 循环分配新空间之前 首先检查是否已经不需要多分配 (and 后面) + while option_len + value_len + value_type_len + 16 < console_width\ + and (option_len < value[1] + or value_len < value[2] + or value_type_len < value[3]): # 每一个部分的逻辑都是 # 如果现在的输出长度小于原始长度 # 并且长度 + 1 之后的总长度依然在允许范围内 # 那么就 + 1 - if option_len < value[1] and option_len + value_len + value_type_len + 1 + 15 < console_width: + if option_len < value[1] and option_len + value_len + value_type_len + 15 < console_width: option_len += 1 - if value_len < value[2] and option_len + value_len + value_type_len + 1 + 15 < console_width: + if value_len < value[2] and option_len + value_len + value_type_len + 15 < console_width: value_len += 1 - if value_type_len < value[3] and option_len + value_len + value_type_len + 1 + 15 < console_width: + if value_type_len < value[3] and option_len + value_len + value_type_len + 15 < console_width: value_type_len += 1 - - for k in range(len(value[0])): - if len(str(value[0][k][0])) > option_len: - value[0][k][0] = str(value[0][k][0])[:value_len - 3] + '..' - if len(str(value[0][k][1])) > value_len: - value[0][k][1] = str(value[0][k][1])[:value_len - 3] + '..' - if len(str(value[0][k][2])) > value_type_len: - value[0][k][2] = str(value[0][k][2])[:value_len - 3] + '..' + # 实际上 对于列表(可变对象) for 出来的这个值是一个引用 + # 所以可以直接修改 string + for v in value[0]: + if len(str(v[0])) > option_len: + v[0] = f'{str(v[0])[:value_len - 3]}...' + if len(str(v[1])) > value_len: + v[1] = f'{str(v[1])[:value_len - 3]}...' + if len(str(v[2])) > value_type_len: + v[2] = f'{str(v[2])[:value_len - 3]}..' cache.write( f"| Option{' ' * (option_len - 3)}| Value{' ' * (value_len - 2)}| Value Type{' ' * (value_type_len - 7)}|\n")