ui.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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, to_good=True):
  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. if to_good:
  65. x = ""
  66. for i in y:
  67. if i in good:
  68. x = x + i
  69. else:
  70. x = x + "▓"
  71. # Now let's print what we've got.
  72. if len(y) < n:
  73. fac = n-len(y)
  74. fac1 = int(round(fac/2))
  75. fac2 = fac1
  76. while fac1 + fac2 > fac:
  77. fac2 -=1
  78. while fac1 + fac2 < fac:
  79. fac2 +=1
  80. x = (" "*fac1)+x+(" "*fac2)
  81. elif len(y) > n:
  82. if n > 10:
  83. x = x[:n-3]+"..."
  84. else:
  85. x = x[:n]
  86. if mode == "normal":
  87. return x
  88. else:
  89. return clr[mode]+clr["bold"]+x+clr["norm"]
  90. def tsize():
  91. # This funtion will get the size of the terminal and
  92. # return it to the variables provided width, height
  93. # On some systems this may not work. So there is a
  94. # try function.
  95. try:
  96. # Getting the size of the terminal
  97. import os
  98. w, h = os.get_terminal_size()
  99. # Sometimes when the terminal width is either
  100. # even or odd. It breaks code for some other
  101. # thing written differenly. For example:
  102. # You may have an even width ( like 84 ) when
  103. # writing a function. And then it works on different
  104. # widths as well like 62 and 80 and 48. All of them
  105. # are still even. Then you scale the terminal to be
  106. # something off like 63 and the function breaks. You
  107. # have one character too much or one character too little.
  108. # This is why I do not want to have a difference. And
  109. # force width to be one less, if it's not divisible by 2.
  110. if not w % 2:
  111. w = w - 1
  112. return w, h
  113. except:
  114. # If, by any reason the terminal can't get it's size.
  115. # We want to return some size regardless.
  116. w = 60
  117. h = 20
  118. return w, h
  119. def table(data, number=True):
  120. # This function will present data in a pretty table thing.
  121. # So let's first of all get the size of the terminal
  122. w, h = tsize()
  123. if number:
  124. w = w - 4
  125. # Then let's draw the categories for this we need to extract
  126. # it's sizes. If there is no 'size' variable the sizes of
  127. # each category will be spread equally.
  128. size = [] # Here the size will go as pure character value.
  129. if "size" in data:
  130. for i in data["size"]:
  131. size.append(int(( w - 10 ) / sum(data["size"]) * i))
  132. while sum(size) < w - 10:
  133. size[-1] += 1
  134. # printing categories
  135. nb = ""
  136. if number:
  137. nb = " "
  138. s = " "+clr["bdma"]+" "+clr["tbwh"]+nb
  139. for n, item in enumerate(data["categories"]):
  140. s = s + wdth(item.upper(), size[n])
  141. print(s+clr["bdma"]+" "+clr["norm"])
  142. size[-1] += 1
  143. # printing items
  144. for b, i in enumerate(data["data"]):
  145. # dark bright sequence thingy
  146. if b % 2:
  147. d = "b"
  148. else:
  149. d = "d"
  150. nb = ""
  151. if number:
  152. nb = clr["tbwh"]+wdth(b,4)
  153. s = " "+clr["bdma"]+" "+nb+clr["norm"]+clr["b"+d+"bu"]#+clr["tbwh"]
  154. for n, item in enumerate(i):
  155. s = s +clr["b"+d+"bu"]+ clr["tbwh"]+wdth(item, size[n]-1)+clr["bdma"]+" "
  156. print(s+clr["norm"])
  157. def center(line, c="bdma", blink=False):
  158. # This funtiocn will bring a given string of text
  159. # in the center of the terminal with a nice backgroud
  160. # around it.
  161. w, h = tsize()
  162. if blink:
  163. blink = clr["blnk"]
  164. else:
  165. blink = ""
  166. if len(line) % 2:
  167. line = line + " "
  168. if len(line) < w - 11:
  169. print(" "+clr[c],
  170. wdth(" ", int((w-10)/2 - (len(line)/2))),
  171. clr["bold"]+clr["tbwh"]+blink+line,
  172. wdth(" ", int((w-10)/2 - (len(line)/2))-1),
  173. clr["norm"])
  174. else:
  175. print(" "+clr[c],
  176. clr["bold"]+clr["tbwh"]+blink+wdth(line,w-10),
  177. clr["norm"])
  178. def timestring(tleft):
  179. # This crazy function will convert the microsecond into something
  180. # a bit more usefull. Like 03:20:90.06 Kind a thing.
  181. tleftX = tleft
  182. tleft = int(tleftX)
  183. addend = tleftX - tleft
  184. valt = str(tleft)
  185. if tleft > 60 :
  186. le = tleft
  187. tleft = int(tleft / 60)
  188. le = le - int(tleft * 60)
  189. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  190. sle = "0"*(2-len(str(le)))+str(le)
  191. valt = stleft+":"+ sle
  192. if tleft > 60 :
  193. lele = le
  194. le = tleft
  195. tleft = int(tleft / 60)
  196. le = le - int(tleft * 60)
  197. lele = (lele - le)
  198. if lele < 0:
  199. lele = int(lele * -1)
  200. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  201. sle = "0"*(2-len(str(le)))+str(le)
  202. slele = "0"*(2-len(str(lele)))+str(lele)
  203. valt = stleft+":"+ sle + ":" + slele
  204. if tleft > 24 :
  205. le = tleft
  206. tleft = int(tleft / 24)
  207. le = le - int(tleft * 24)
  208. valt = str(tleft)+" DAYS AND "+ str(le) + " HRS"
  209. return valt# + "." + str(int(addend*100))
  210. def progress_bar(now, total, name=""):
  211. # This function will draw a pretty progress bar that fills up
  212. # one problem. It requires an empty print line after it. Or it
  213. # will start printing whatever in the same line as the progress
  214. # bar.
  215. # dimensions
  216. w, h = tsize()
  217. fullw = w - 8
  218. # string
  219. string = " "+str(int(round(now/total*100)))+"% "+str(now)+" / "+str(total)+" "+name
  220. #string = string+" "*(fullw-len(string))
  221. string = wdth(string, fullw)
  222. howfar = int(round(fullw / total * now))
  223. pstring = clr["tbwh"]+clr["bold"]+clr["bdcy"]+string[:howfar]+clr["bdma"]+string[howfar:]
  224. print("\r "+pstring, end=clr["norm"])
  225. def user_emoji(user, stuff):
  226. import datetime
  227. new_date_format = "%Y/%m/%d"
  228. today = datetime.datetime.strftime(datetime.datetime.today(), new_date_format)
  229. for d in range(400):
  230. yesterday = datetime.datetime.strftime(datetime.datetime.today()-datetime.timedelta(days=d+1), new_date_format)
  231. if yesterday in stuff.get("volume", {}):
  232. break
  233. try:
  234. v = stuff["volume"][yesterday]["average"]
  235. except:
  236. v = 20
  237. try:
  238. v = stuff["volume"][today]["average"]
  239. except:
  240. pass
  241. # Prints username with emoji and everything.get
  242. vs = "😭😢🥺😕🙂😀🤣" #emojis from sad to happy
  243. evil = "😇🥰😊🤔🤨😒😖😡😈" #level of evil emoji
  244. # getting average volume
  245. average = 0
  246. for i in stuff.get("volume", {}):
  247. for b in stuff.get("volume", {})[i].get("users", {}):
  248. if user in b:
  249. if not average:
  250. average = stuff.get("volume", {})[i].get("users", {})[b]
  251. else:
  252. average = ( average + stuff.get("volume", {})[i].get("users", {})[b] ) / 2
  253. average = v - average
  254. if average < 0:
  255. average = average * -1
  256. average = (average * -1) + 100
  257. take = int(round((len(vs)-1)*(average/100)))
  258. evilness = stuff.get("users", {}).get(user, {}).get("evil", 50)
  259. eviltake = int(round((len(evil)-1)*(evilness/100)))
  260. name = stuff.get("users", {}).get(user, {}).get("display_name", user)
  261. return evil[eviltake]+vs[take]+" "+name