0.1.2 improve 0.1.2

This commit is contained in:
shenjack 2023-08-20 22:14:33 +08:00
parent bc7c11971a
commit b6d4981cf9
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 50 additions and 10 deletions

View File

@ -1,5 +1,12 @@
# Change log / 更新日志
## 0.1.2
- 改进了 `types.Options`
- 现在 `as_markdown` 如果没有指定最长长度 会自动适配输出所用终端的宽度
- 同时如果指定最大长度, 也会更灵活的适配
- 补全了一些文档
## 0.1.1
- 为 `CompilerHelper` 添加了 `remove_output` 的选项

View File

@ -4,5 +4,5 @@
# All rights reserved
# -------------------------------
__version__ = '0.1.1'
__version__ = '0.1.2'

View File

@ -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')

View File

@ -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"