ui.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # GPLv3 or later
  2. # Colors are used to make the
  3. clr = {
  4. "norm":"\033[00m", # Reset to normal
  5. "bold":"\033[01m", # Bold Text
  6. "ital":"\033[03m", # Italic Text
  7. "undr":"\033[04m", # Underlined
  8. "blnk":"\033[05m", # Blinking
  9. # Text
  10. "tdbl":"\033[30m", # Dark Black
  11. "tdrd":"\033[31m", # Dark Red
  12. "tdgr":"\033[32m", # Dark Green
  13. "tdyl":"\033[33m", # Dark Yellow
  14. "tdbu":"\033[34m", # Dark Blue
  15. "tdma":"\033[35m", # Dark Magenta
  16. "tdcy":"\033[36m", # Dark Cyan
  17. "tdwh":"\033[37m", # Dark White
  18. "tbbl":"\033[90m", # Bright Black
  19. "tbrd":"\033[91m", # Bright Red
  20. "tbgr":"\033[92m", # Bright Green
  21. "tbyl":"\033[93m", # Bright Yellow
  22. "tbbu":"\033[94m", # Bright Blue
  23. "tbma":"\033[95m", # Bright Magenta
  24. "tbcy":"\033[96m", # Bright Cyan
  25. "tbwh":"\033[97m", # Bright White
  26. # Background
  27. "bdbl":"\033[40m", # Dark Black
  28. "bdrd":"\033[41m", # Dark Red
  29. "bdgr":"\033[42m", # Dark Green
  30. "bdyl":"\033[43m", # Dark Yellow
  31. "bdbu":"\033[44m", # Dark Blue
  32. "bdma":"\033[45m", # Dark Magenta
  33. "bdcy":"\033[46m", # Dark Cyan
  34. "bdwh":"\033[47m", # Dark White
  35. "bbbl":"\033[100m", # Bright Black
  36. "bbrd":"\033[101m", # Bright Red
  37. "bbgr":"\033[102m", # Bright Green
  38. "bbyl":"\033[103m", # Bright Yellow
  39. "bbbu":"\033[104m", # Bright Blue
  40. "bbma":"\033[105m", # Bright Magenta
  41. "bbcy":"\033[106m", # Bright Cyan
  42. "bbwh":"\033[108m" # Bright White
  43. }
  44. # A function that insures a specific width of the printed part
  45. def wdth(x, n):
  46. # Convert Data to String
  47. mode = "normal"
  48. if type(x) == bool and x == True:
  49. x = "V"
  50. mode = "bdgr"
  51. elif type(x) == bool and x == False:
  52. x = "X"
  53. mode = "bdrd"
  54. else:
  55. x = str(x)
  56. # Turn emogis
  57. #x = emote(x)
  58. # Some characters are too wide. They do not obey the
  59. # monospace of the terminal, thus making it not pretty.
  60. # This is the string of characters which are checked to
  61. good = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'’()*+,-./:;<=>?@[\]^_`{|}~ йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮёЁ"
  62. # let's filter the string
  63. y = x
  64. x = ""
  65. for i in y:
  66. if i in good:
  67. x = x + i
  68. else:
  69. x = x + "▓"
  70. # Now let's print what we've got.
  71. if len(y) < n:
  72. fac = n-len(y)
  73. fac1 = int(round(fac/2))
  74. fac2 = fac1
  75. while fac1 + fac2 > fac:
  76. fac2 -=1
  77. while fac1 + fac2 < fac:
  78. fac2 +=1
  79. x = (" "*fac1)+x+(" "*fac2)
  80. elif len(y) > n:
  81. if n > 10:
  82. x = x[:n-3]+"..."
  83. else:
  84. x = x[:n]
  85. if mode == "normal":
  86. return x
  87. else:
  88. return clr[mode]+clr["bold"]+x+clr["norm"]
  89. def tsize():
  90. # This funtion will get the size of the terminal and
  91. # return it to the variables provided width, height
  92. # On some systems this may not work. So there is a
  93. # try function.
  94. try:
  95. # Getting the size of the terminal
  96. import os
  97. w, h = os.get_terminal_size()
  98. # Sometimes when the terminal width is either
  99. # even or odd. It breaks code for some other
  100. # thing written differenly. For example:
  101. # You may have an even width ( like 84 ) when
  102. # writing a function. And then it works on different
  103. # widths as well like 62 and 80 and 48. All of them
  104. # are still even. Then you scale the terminal to be
  105. # something off like 63 and the function breaks. You
  106. # have one character too much or one character too little.
  107. # This is why I do not want to have a difference. And
  108. # force width to be one less, if it's not divisible by 2.
  109. if not w % 2:
  110. w = w - 1
  111. return w, h
  112. except:
  113. # If, by any reason the terminal can't get it's size.
  114. # We want to return some size regardless.
  115. w = 60
  116. h = 20
  117. return w, h
  118. def table(data, number=True):
  119. # This function will present data in a pretty table thing.
  120. # So let's first of all get the size of the terminal
  121. w, h = tsize()
  122. if number:
  123. w = w - 4
  124. # Then let's draw the categories for this we need to extract
  125. # it's sizes. If there is no 'size' variable the sizes of
  126. # each category will be spread equally.
  127. size = [] # Here the size will go as pure character value.
  128. if "size" in data:
  129. for i in data["size"]:
  130. size.append(int(( w - 10 ) / sum(data["size"]) * i))
  131. while sum(size) < w - 10:
  132. size[-1] += 1
  133. # printing categories
  134. nb = ""
  135. if number:
  136. nb = " "
  137. s = " "+clr["bdma"]+" "+clr["tbwh"]+nb
  138. for n, item in enumerate(data["categories"]):
  139. s = s + wdth(item.upper(), size[n])
  140. print(s+clr["bdma"]+" "+clr["norm"])
  141. size[-1] += 1
  142. # printing items
  143. for b, i in enumerate(data["data"]):
  144. # dark bright sequence thingy
  145. if b % 2:
  146. d = "b"
  147. else:
  148. d = "d"
  149. nb = ""
  150. if number:
  151. nb = clr["tbwh"]+wdth(b,4)
  152. s = " "+clr["bdma"]+" "+nb+clr["norm"]+clr["b"+d+"bu"]#+clr["tbwh"]
  153. for n, item in enumerate(i):
  154. s = s +clr["b"+d+"bu"]+ clr["tbwh"]+wdth(item, size[n]-1)+clr["bdma"]+" "
  155. print(s+clr["norm"])
  156. def center(line, c="bdma", blink=False):
  157. # This funtiocn will bring a given string of text
  158. # in the center of the terminal with a nice backgroud
  159. # around it.
  160. w, h = tsize()
  161. if blink:
  162. blink = clr["blnk"]
  163. else:
  164. blink = ""
  165. if len(line) % 2:
  166. line = line + " "
  167. if len(line) < w - 11:
  168. print(" "+clr[c],
  169. wdth(" ", int((w-10)/2 - (len(line)/2))),
  170. clr["bold"]+clr["tbwh"]+blink+line,
  171. wdth(" ", int((w-10)/2 - (len(line)/2))-1),
  172. clr["norm"])
  173. else:
  174. print(" "+clr[c],
  175. clr["bold"]+clr["tbwh"]+blink+wdth(line,w-10),
  176. clr["norm"])
  177. def timestring(tleft):
  178. # This crazy function will convert the microsecond into something
  179. # a bit more usefull. Like 03:20:90.06 Kind a thing.
  180. tleftX = tleft
  181. tleft = int(tleftX)
  182. addend = tleftX - tleft
  183. valt = str(tleft)
  184. if tleft > 60 :
  185. le = tleft
  186. tleft = int(tleft / 60)
  187. le = le - int(tleft * 60)
  188. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  189. sle = "0"*(2-len(str(le)))+str(le)
  190. valt = stleft+":"+ sle
  191. if tleft > 60 :
  192. lele = le
  193. le = tleft
  194. tleft = int(tleft / 60)
  195. le = le - int(tleft * 60)
  196. lele = (lele - le)
  197. if lele < 0:
  198. lele = int(lele * -1)
  199. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  200. sle = "0"*(2-len(str(le)))+str(le)
  201. slele = "0"*(2-len(str(lele)))+str(lele)
  202. valt = stleft+":"+ sle + ":" + slele
  203. if tleft > 24 :
  204. le = tleft
  205. tleft = int(tleft / 24)
  206. le = le - int(tleft * 24)
  207. valt = str(tleft)+" DAYS AND "+ str(le) + " HRS"
  208. return valt + "." + str(int(addend*100))
  209. def progress_bar(now, total, name=""):
  210. # This function will draw a pretty progress bar that fills up
  211. # one problem. It requires an empty print line after it. Or it
  212. # will start printing whatever in the same line as the progress
  213. # bar.
  214. # dimensions
  215. w, h = tsize()
  216. fullw = w - 8
  217. # string
  218. string = " "+str(int(round(now/total*100)))+"% "+str(now)+" / "+str(total)+" "+name
  219. #string = string+" "*(fullw-len(string))
  220. string = wdth(string, fullw)
  221. howfar = int(round(fullw / total * now))
  222. pstring = clr["tbwh"]+clr["bold"]+clr["bdcy"]+string[:howfar]+clr["bdma"]+string[howfar:]
  223. print("\r "+pstring, end=clr["norm"])