File size: 2,816 Bytes
88435ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import os
from typing import Any

from typing_extensions import TypedDict

from neollm.types import PrintColor


class CPrintParam(TypedDict, total=False):
    text: Any
    color: PrintColor | None
    background: bool
    light: bool
    bold: bool
    italic: bool
    underline: bool
    kwargs: dict[str, Any]


def cprint(
    *text: Any,
    color: PrintColor | None = None,
    background: bool = False,
    light: bool = False,
    bold: bool = False,
    italic: bool = False,
    underline: bool = False,
    kwargs: dict[str, Any] = {},
) -> None:
    """
    色付けなどリッチにprint

    Args:
        *text: 表示するテキスト。
        color (PrintColor): テキストの色: 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'。
        background (bool): 背景色
        light (bool): 淡い色にするか
        bold (bool): 太字
        italic (bool): 斜体
        underline (bool): 下線
        **kwargs: printの引数
    """
    # ANSIエスケープシーケンスを使用して、テキストを書式設定して表示する
    format_string = ""

    # 色の設定
    color2code: dict[PrintColor, int] = {
        "black": 30,
        "red": 31,
        "green": 32,
        "yellow": 33,
        "blue": 34,
        "magenta": 35,
        "cyan": 36,
        "white": 37,
    }
    if color is not None and color in color2code:
        code = color2code[color]
        if background:
            code += 10
        elif light:
            code += 60
        format_string += f"\033[{code}m"
    if bold:
        format_string += "\033[1m"
    if italic:
        format_string += "\033[3m"
    if underline:
        format_string += "\033[4m"

    # テキストの表示
    for text_i in text:
        print(format_string + str(text_i) + "\033[0m", **kwargs)


def ensure_env_var(var_name: str | None = None, default: str | None = None) -> str:
    if var_name is None:
        return ""
    if os.environ.get(var_name, "") == "":
        if default is None:
            raise ValueError(f"{var_name}をenvで設定しよう")
        cprint(f"WARNING: {var_name}が設定されていません。{default}を使用します。", color="yellow", background=True)
        os.environ[var_name] = default
    return os.environ[var_name]


def suport_unrecomended_env_var(old_key: str, new_key: str) -> None:
    """非推奨の環境変数をサポートする

    Args:
        old_key (str): 非推奨の環境変数名
        new_key (str): 推奨の環境変数名
    """
    if os.getenv(old_key) is not None and os.getenv(new_key) is None:
        cprint(f"WARNING: {old_key}ではなく、{new_key}にしてね", color="yellow", background=True)
        os.environ[new_key] = os.environ[old_key]