ui.py~ 6.0 KB

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