From b6d4981cf94063141aabc89b2dbfc063000c5526 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 20 Aug 2023 22:14:33 +0800 Subject: [PATCH] 0.1.2 improve 0.1.2 --- docs/change_log.md | 7 ++++++ lib_not_dr/__init__.py | 2 +- lib_not_dr/types/options.py | 49 +++++++++++++++++++++++++++++++------ pyproject.toml | 2 +- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/docs/change_log.md b/docs/change_log.md index 16ca4e1..9b43ee4 100644 --- a/docs/change_log.md +++ b/docs/change_log.md @@ -1,5 +1,12 @@ # Change log / 更新日志 +## 0.1.2 + +- 改进了 `types.Options` + - 现在 `as_markdown` 如果没有指定最长长度 会自动适配输出所用终端的宽度 + - 同时如果指定最大长度, 也会更灵活的适配 + - 补全了一些文档 + ## 0.1.1 - 为 `CompilerHelper` 添加了 `remove_output` 的选项 diff --git a/lib_not_dr/__init__.py b/lib_not_dr/__init__.py index 995724f..7d535e9 100644 --- a/lib_not_dr/__init__.py +++ b/lib_not_dr/__init__.py @@ -4,5 +4,5 @@ # All rights reserved # ------------------------------- -__version__ = '0.1.1' +__version__ = '0.1.2' diff --git a/lib_not_dr/types/options.py b/lib_not_dr/types/options.py index 76a0476..6d4c69e 100644 --- a/lib_not_dr/types/options.py +++ b/lib_not_dr/types/options.py @@ -4,6 +4,7 @@ # All rights reserved # ------------------------------- +import shutil import traceback from io import StringIO from typing import get_type_hints, Type, List, Union, Dict, Any, Callable, Tuple, Optional, TYPE_CHECKING, Iterable @@ -171,15 +172,12 @@ class Options: self.cached_options = self.option() return self.cached_options - def option_with_len(self, longest: Optional[int] = None) -> Tuple[List[Tuple[str, Union[Any, Type], Type]], int, int, int]: + def option_with_len(self) -> Tuple[List[Tuple[str, Any, Type]], int, int, int]: """ 返回一个可以用于打印的 option 列表 :return: """ - if longest is None: - options = self.flush_option() - else: - options = self.str_option(longest) + options = self.flush_option() max_len_key = 1 max_len_value = 1 max_len_value_t = 1 @@ -189,19 +187,54 @@ class Options: max_len_key = max(max_len_key, len(key)) max_len_value = max(max_len_value, len(str(value))) max_len_value_t = max(max_len_value_t, len(str(value_t))) - option_list.append((key, value, value_t)) - return option_list, max_len_key, max_len_value, max_len_value_t + option_list.append([key, value, value_t]) + return [option_list, max_len_key, max_len_value, max_len_value_t] # noqa def as_markdown(self, longest: Optional[int] = None) -> str: """ 返回一个 markdown 格式的 option 字符串 + :param longest: 最长的输出长度 :return: markdown 格式的 option 字符串 """ - value = self.option_with_len(longest) + value = self.option_with_len() cache = StringIO() option_len = max(value[1], len('Option')) value_len = max(value[2], len('Value')) value_type_len = max(value[3], len('Value Type')) + + # | Option | Value | Value Type | + shortest = len('Option" "Value" "Value Type') + + if longest is not None: + console_width = max(longest, shortest) + else: + console_width = shutil.get_terminal_size(fallback=(100, 80)).columns + console_width = max(console_width, shortest) + + 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: + # 每一个部分的逻辑都是 + # 如果现在的输出长度小于原始长度 + # 并且长度 + 1 之后的总长度依然在允许范围内 + # 那么就 + 1 + if option_len < value[1] and option_len + value_len + value_type_len + 1 + 15 < console_width: + option_len += 1 + if value_len < value[2] and option_len + value_len + value_type_len + 1 + 15 < console_width: + value_len += 1 + if value_type_len < value[3] and option_len + value_len + value_type_len + 1 + 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] + '..' + cache.write( f"| Option{' ' * (option_len - 3)}| Value{' ' * (value_len - 2)}| Value Type{' ' * (value_type_len - 7)}|\n") cache.write(f'|:{"-" * (option_len + 3)}|:{"-" * (value_len + 3)}|:{"-" * (value_type_len + 3)}|\n') diff --git a/pyproject.toml b/pyproject.toml index a384aa7..f100ff1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -version = "0.1.1" +version = "0.1.2" name = "lib-not-dr" description = "A python lib created from Difficult Rocket development" readme = "README.md"