summaryrefslogtreecommitdiff
path: root/stdlib.py
blob: d4276c70549810502b2aaec2c73494c4fe70dd46 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# text color code
class color:
    ### ASCII
    #\33[0m   # remove all attribute
    #\33[1m   # control brightness
    #\33[4m   # underline
    #\33[5m   # blink
    #\33[7m   # reverse
    #\33[8m   # disappear
    #\33[30m -- \33[37m   # foreground
    #\33[40m -- \33[47m   # background
    #\33[nA   # upper cursor n line
    #\33[nB   # lower cursor n line
    #\33[nC   # righter cursor n line
    #\33[nD   # lefter cursor n line
    #\33[y;xH # set cursor x/y
    #\33[2J   # clear screen
    #\33[K    # clear from cursor to end
    #\33[s    # save cursor pos
    #\33[u    # recover saved cursor pos
    #\33[?25l # hide cursor
    #\33[?25h # show cursor
    #\033[1m  # bold
    #\033[0m  # un-bold
    #\033[0m  # reset \033[2m 

    black = "\033[30m" # black
    red = "\033[31m" # red
    green = "\033[32m" # green
    yellow = "\033[33m" # yellow
    blue = "\033[34m" # blue
    purple = "\033[35m" # purple
    cyan = "\033[36m" # cyan
    gray = "\033[37m" # gray

    lblack = "\033[1;30m" # light-black
    lred = "\033[1;31m" # light-red
    lgreen = "\033[1;32m" # light-green
    lyellow = "\033[1;33m" # light-yellow
    lblue = "\033[1;34m" # light-blue
    lpurple = "\033[1;35m" # light-purple
    lcyan = "\033[1;36m" # light-cyan
    white = "\033[1;37m" # white

# print but formatly without \n and forcfully flush stream
def printf(str):
    print(str, end='', flush=True)

# mixly(shuffle) join a list to another
def shufflejoin(str1: str, str2: str, *:
    from random import shuffle
    if *:
        tmpstr = *
    else:
        tmpstr = str1 + str2

    tmpstrlst = list(tmpstr)
    shuffle(tmpstrlst)
    return ''.join(tmpstrlst)

# limit string to how much charactor it can use, and delete other then that
def limitString(string: str, limit: int):
    if len(string) > limit:
        return string[0:limit]
    else:
        return string

# animated
def aprintf(str, end=None):
    from time import sleep
    waitime = 0.01
    str = list(str)
    for i in str:
        printf(i)
        sleep(waitime)
    if end == None:
        printf('\n')
    else:
        printf(end)

# animated countdown from @timeoutsec
def countdown(timeoutsec: int):
    from time import sleep
    timeoutsec = timeoutsec
    for _ in range(timeoutsec-1):
        printf(timeoutsec)
        for _ in range(timeoutsec):
            printf('.')
            sleep(0.8/3)
        timeoutsec -= 1
        sleep(0.2)
    print(1)

# animated loading with @msg
def aloading(msg="Loading...", stop=False):
    global stop
    from itertools import cycle
    from time import sleep
    for c in cycle(['|', '/', '-', '\\']):
        if stop:
            break
        printf(f"\r[{c}] {msg}")
        sleep(0.1)

# automated execute @func in background with @arg
def bgexec(func, arg=False, stop=False):
    from threading import Thread
    if stop:
        try:
            t.join()
        except AttributeError:
            raise AttributeError("Thread is not started")
        except UnboundLocalError:
            raise UnboundLocalError("Thread is not started")
    else:
        if arg:
            t = Thread(target=func, args=arg).start()
        else:
            t = Thread(target=func).start()