variables.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. # This file is a set of variables used by different files. It's needed
  30. # so I will not need to hard code the values each time. Stuff like
  31. # basic translations of the LBRY data into human language. And a more
  32. # complex functions like translating size in bytes into something more
  33. # readable.
  34. # Colors are used to make the
  35. clr = {
  36. "norm":"\033[00m", # Reset to normal
  37. "bold":"\033[01m", # Bold Text
  38. "ital":"\033[03m", # Italic Text
  39. "undr":"\033[04m", # Underlined
  40. "blnk":"\033[05m", # Blinking
  41. # Text
  42. "tdbl":"\033[30m", # Dark Black
  43. "tdrd":"\033[31m", # Dark Red
  44. "tdgr":"\033[32m", # Dark Green
  45. "tdyl":"\033[33m", # Dark Yellow
  46. "tdbu":"\033[34m", # Dark Blue
  47. "tdma":"\033[35m", # Dark Magenta
  48. "tdcy":"\033[36m", # Dark Cyan
  49. "tdwh":"\033[37m", # Dark White
  50. "tbbl":"\033[90m", # Bright Black
  51. "tbrd":"\033[91m", # Bright Red
  52. "tbgr":"\033[92m", # Bright Green
  53. "tbyl":"\033[93m", # Bright Yellow
  54. "tbbu":"\033[94m", # Bright Blue
  55. "tbma":"\033[95m", # Bright Magenta
  56. "tbcy":"\033[96m", # Bright Cyan
  57. "tbwh":"\033[97m", # Bright White
  58. # Background
  59. "bdbl":"\033[40m", # Dark Black
  60. "bdrd":"\033[41m", # Dark Red
  61. "bdgr":"\033[42m", # Dark Green
  62. "bdyl":"\033[43m", # Dark Yellow
  63. "bdbu":"\033[44m", # Dark Blue
  64. "bdma":"\033[45m", # Dark Magenta
  65. "bdcy":"\033[46m", # Dark Cyan
  66. "bdwh":"\033[47m", # Dark White
  67. "bbbl":"\033[100m", # Bright Black
  68. "bbrd":"\033[101m", # Bright Red
  69. "bbgr":"\033[102m", # Bright Green
  70. "bbyl":"\033[103m", # Bright Yellow
  71. "bbbu":"\033[104m", # Bright Blue
  72. "bbma":"\033[105m", # Bright Magenta
  73. "bbcy":"\033[106m", # Bright Cyan
  74. "bbwh":"\033[108m" # Bright White
  75. }
  76. # A function that insures a specific width of the printed part
  77. def wdth(x, n):
  78. # Just in case...
  79. x = str(x)
  80. # Some characters are too wide. They do not obey the
  81. # monospace of the terminal, thus making it not pretty.
  82. # This is the string of characters which are checked to
  83. good = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮёЁ"
  84. # let's filter the string
  85. y = x
  86. x = ""
  87. for i in y:
  88. if i in good:
  89. x = x + i
  90. else:
  91. x = x + "▓"
  92. # Now let's print what we've got.
  93. if len(y) < n:
  94. x = (" "*(n-len(y)))+x
  95. elif len(y) > n:
  96. if n > 10:
  97. x = x[:n-3]+"..."
  98. else:
  99. x = x[:n]
  100. return x
  101. # A dictionary for translations of things from the SDK into
  102. # readable thing
  103. what = {
  104. "stream":"FILE",
  105. "repost":"SHARED",
  106. "channel": "CHANNEL",
  107. "collection": "PLAYLIST",
  108. "video":"VIDEO",
  109. "audio":"SOUND",
  110. "document":"TEXT",
  111. "binary":"FILE",
  112. "image":"PICTURE"
  113. }
  114. # This function will take a list and present in a pretty
  115. # way.
  116. def tgz(x):
  117. # Just in case
  118. if type(x) != list:
  119. x = x.split()
  120. y = ""
  121. for i in x:
  122. y = y + i + ", "
  123. return y[:-2]
  124. # This function will convert bites into readable data making sense
  125. def csize(x):
  126. x = float(x)
  127. l = ["B","KB", "MB", "GB", "TB"]
  128. for i in range(5):
  129. if x > 1024:
  130. x = x / 1024
  131. else:
  132. return str(round(x, 2))+" "+l[i]
  133. return str(round(x, 2))+" "+l[i]
  134. # This next function will select the amount of ::: for a given
  135. # input.
  136. def typing_dots():
  137. import inspect
  138. depth = len(inspect.stack()) # This is the depth of the stack
  139. # since this function call adds 1 to the stack we need
  140. # to decrease the number by one
  141. depth -= 1
  142. # Now I want to select a sequence of colors.
  143. x = ["bdma","bdbu", "bdrd", "bdgr", "bdcy", "bdyl"]
  144. ret = " "+clr["bold"]
  145. for i in reversed(range(depth)):
  146. ret = ret + clr["tbwh"] + clr[x[i % len(x)]] + ":"
  147. ret = ret + clr["norm"]+" "
  148. return ret
  149. def tsize():
  150. # This funtion will get the size of the terminal and
  151. # return it to the variables provided width, height
  152. # On some systems this may not work. So there is a
  153. # try function.
  154. try:
  155. # Getting the size of the terminal
  156. import os
  157. w, h = os.get_terminal_size()
  158. # Sometimes when the terminal width is either
  159. # even or odd. It breaks code for some other
  160. # thing written differenly. For example:
  161. # You may have an even width ( like 84 ) when
  162. # writing a function. And then it works on different
  163. # widths as well like 62 and 80 and 48. All of them
  164. # are still even. Then you scale the terminal to be
  165. # something off like 63 and the function breaks. You
  166. # have one character too much or one character too little.
  167. # This is why I do not want to have a difference. And
  168. # force width to be one less, if it's not divisible by 2.
  169. if not w % 2:
  170. w = w - 1
  171. return w, h
  172. except:
  173. # If, by any reason the terminal can't get it's size.
  174. # We want to return some size regardless.
  175. w = 60
  176. h = 20
  177. return w, h
  178. def logo():
  179. # This function will draw a pretty FastLBRY logo to the user
  180. # at startup.
  181. # Getting terminal size
  182. w, h = tsize()
  183. if w > 50:
  184. l = []
  185. l.append( "█▓▓█▓▓▓▓▓▓█▓██▓▓▓▓▓▓▓█▓▓▓▓▓▓█▓▓▓▓██▓▓▓▓█▓▓▓▓█" )
  186. l.append( "▓▒▒▒▒▒▒▒▓▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▓▒▒▒▒▒▒▒▒▒▓▒▒▒▒▓▒▓" )
  187. l.append( "██░░░▒░░░░░░▒░░░░░░░▒░░░░░░░░░▒░░▒░░░░░░░░▒▓▓" )
  188. l.append( "▓▓░ ░ ░ ░ ■ ■░░▒▓" )
  189. l.append( "▓▒▒ ╔════════■ ░ ╔════╗ ╔════╗ ║ ░ ║ ░▓█" )
  190. l.append( "▓▒░ ║ ░ ║ ║ ║ ║ ║ ║ ║ ░▒▓" )
  191. l.append( "█▓░░║ ║ ╠════╣ ╠═╦══╝ ╚══╦══╝ ▒▒▓" )
  192. l.append( "▓▒▒ ╠══ AST ■║ ║ ║ ║ ╚══╗ ║ ░░▒█" )
  193. l.append( "█▒░ ║ ║ ║ ║ ║ ║ ║ ░▓▓" )
  194. l.append( "▓▓░ ║ ░ ╚═════■╚════╝ ■ ■ ░ ║ ░ ░▒▓" )
  195. l.append( "▓▒░░║ ░ THE TERMINAL CLIENT ║ ▒▒█" )
  196. l.append( "█▒▒ ■ ░ ░ ■ ▒░ ░▓▓" )
  197. l.append( "▓▒░░░░░░░▒░░░░▓░░░░░▒░░░░░░░░░▒░░░░▒░░░░░░░▒█" )
  198. l.append( "▓▓▒▒▒▒▓▒▒▒▒▒▓▒▒▒▒▓▒▒▓▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▓▒▒▒▒▒▓▓▓" )
  199. l.append( "█▓▓█▓▓▓▓▓████▓▓▓▓█▓▓▓▓█▓▓▓▓██▓▓▓█▓▓▓▓▓█▓▓▓▓██" )
  200. print(" "+clr["bdma"]+(" "*(w-8))+clr["norm"])
  201. # I want to fill the width of the terminal around the logo
  202. # with a texture. But since it's text based I will need to
  203. # code a texture into it. I can use the blocks seen below
  204. # and select randomly between them.
  205. # You can see I included multiple of darkest block and
  206. # almost non bright blocks. This will increase the
  207. # probability of the dark blocks.
  208. block = "████████▓▓▓▒" #for the random to choose
  209. import random
  210. # Now let's output
  211. for i in l:
  212. f = "" # Texture fill string.
  213. # Fill the f string with random blocks
  214. for g in range(int(w/2-27)):
  215. f = f + random.choice(block)
  216. # Print a line with random filler and the line it self.
  217. print (" "+clr["bdma"]+" "+clr["norm"]+f+i+f+clr["bdma"]+" "+clr["norm"])
  218. else:
  219. center( "FastLBRY")
  220. center( "terminal")
  221. center("---type 'help' to see list of features---")
  222. def table(data, number=True):
  223. # This function will present data in a pretty table thing.
  224. # So let's first of all get the size of the terminal
  225. w, h = tsize()
  226. if number:
  227. w = w - 4
  228. # Then let's draw the categories for this we need to extract
  229. # it's sizes. If there is no 'size' variable the sizes of
  230. # each category will be spread equally.
  231. size = [] # Here the size will go as pure character value.
  232. if "size" in data:
  233. for i in data["size"]:
  234. size.append(int(( w - 10 ) / sum(data["size"]) * i))
  235. while sum(size) < w - 10:
  236. size[-1] += 1
  237. # printing categories
  238. nb = ""
  239. if number:
  240. nb = " "
  241. s = " "+clr["bdma"]+" "+clr["tbwh"]+nb
  242. for n, item in enumerate(data["categories"]):
  243. s = s + wdth(item.upper(), size[n])
  244. print(s+clr["bdma"]+" "+clr["norm"])
  245. size[-1] += 1
  246. # printing items
  247. for b, i in enumerate(data["data"]):
  248. # dark bright sequence thingy
  249. if b % 2:
  250. d = "b"
  251. else:
  252. d = "d"
  253. nb = ""
  254. if number:
  255. nb = clr["tbwh"]+wdth(b,4)
  256. s = " "+clr["bdma"]+" "+nb+clr["norm"]+clr["b"+d+"bu"]+clr["tbwh"]
  257. for n, item in enumerate(i):
  258. s = s +clr["b"+d+"bu"]+ wdth(item, size[n]-1)+clr["bdma"]+" "
  259. print(s+clr["norm"])
  260. def center(line, c="bdma", blink=False):
  261. # This funtiocn will bring a given string of text
  262. # in the center of the terminal with a nice backgroud
  263. # around it.
  264. w, h = tsize()
  265. if blink:
  266. blink = clr["blnk"]
  267. else:
  268. blink = ""
  269. if len(line) % 2:
  270. line = line + " "
  271. if len(line) < w - 11:
  272. print(" "+clr[c],
  273. wdth(" ", int((w-10)/2 - (len(line)/2))),
  274. clr["bold"]+clr["tbwh"]+blink+line,
  275. wdth(" ", int((w-10)/2 - (len(line)/2))-1),
  276. clr["norm"])
  277. else:
  278. print(" "+clr[c],
  279. clr["bold"]+clr["tbwh"]+blink+wdth(line,w-10),
  280. clr["norm"])
  281. def timestring(tleft):
  282. # This crazy function will convert the microsecond into something
  283. # a bit more usefull. Like 03:20:90.06 Kind a thing.
  284. tleftX = tleft
  285. tleft = int(tleftX)
  286. addend = tleftX - tleft
  287. valt = str(tleft)
  288. if tleft > 60 :
  289. le = tleft
  290. tleft = int(tleft / 60)
  291. le = le - int(tleft * 60)
  292. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  293. sle = "0"*(2-len(str(le)))+str(le)
  294. valt = stleft+":"+ sle
  295. if tleft > 60 :
  296. lele = le
  297. le = tleft
  298. tleft = int(tleft / 60)
  299. le = le - int(tleft * 60)
  300. lele = (lele - le)
  301. if lele < 0:
  302. lele = int(lele * -1)
  303. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  304. sle = "0"*(2-len(str(le)))+str(le)
  305. slele = "0"*(2-len(str(lele)))+str(lele)
  306. valt = stleft+":"+ sle + ":" + slele
  307. if tleft > 24 :
  308. le = tleft
  309. tleft = int(tleft / 24)
  310. le = le - int(tleft * 24)
  311. valt = str(tleft)+" DAYS AND "+ str(le) + " HRS"
  312. return valt + "." + str(int(addend*100))
  313. # This a list of known licenses, info a and links
  314. licenses = [
  315. # NAME , URL , COMMENT
  316. ["GNU General Public License Version 3 (or later)",
  317. "https://www.gnu.org/licenses/gpl-3.0.html",
  318. "Strong Copyleft. Recommended for Software."],
  319. ["GNU General Public License Version 3 (only)",
  320. "https://www.gnu.org/licenses/gpl-3.0.html",
  321. "Strong Copyleft."],
  322. ["GNU Free Documentation License",
  323. "https://www.gnu.org/licenses/fdl-1.3.html",
  324. "Strong Copyleft. Recommended for books."],
  325. ["Creative Commons Attribution-ShareAlike 4.0 International",
  326. "https://creativecommons.org/licenses/by-sa/4.0/",
  327. "Copylefted, Recommended for Art."],
  328. ["Creative Commons Attribution 4.0 International",
  329. "https://creativecommons.org/licenses/by/4.0/",
  330. "Non Copylefted, Free License."],
  331. ["Creative Commons Zero 1.0 International",
  332. "https://creativecommons.org/publicdomain/zero/1.0/",
  333. "Public Domain"],
  334. ["Creative Commons Attribution-NoDerivatives 4.0 International",
  335. "https://creativecommons.org/licenses/by-nd/4.0/",
  336. "Does not allow changes. Recommended for opinion pieces."]
  337. ]
  338. def progress_bar(now, total, name=""):
  339. # This function will draw a pretty progress bar that fills up
  340. # one problem. It requires an empty print line after it. Or it
  341. # will start printing whatever in the same line as the progress
  342. # bar.
  343. # dimensions
  344. w, h = tsize()
  345. fullw = w - 8
  346. # string
  347. string = " "+str(int(round(now/total*100)))+"% "+str(now)+" / "+str(total)+" "+name
  348. string = string+" "*(fullw-len(string))
  349. howfar = int(round(fullw / total * now))
  350. pstring = clr["tbwh"]+clr["bold"]+clr["bdcy"]+string[:howfar]+clr["bdma"]+string[howfar:]
  351. print("\r "+pstring, end=clr["norm"])
  352. # We need a system of auto-filling commands. As well as a history type thing.
  353. # so people could come back to a previous command by pressing the up arrow.
  354. # Now all systems will have readline since it's a GNU package
  355. def complete(commands):
  356. try:
  357. import readline
  358. # Then we need to specify a function for completion
  359. def completer(text, state):
  360. options = [i for i in commands if i.startswith(text)]
  361. if state < len(options):
  362. return options[state]
  363. else:
  364. return None
  365. # And we need to setup the completer
  366. readline.parse_and_bind("tab: complete")
  367. readline.set_completer(completer)
  368. except Exception as e:
  369. center("Auto-completer error: "+str(e), "bdrd")
  370. # List of web instances of LBRY
  371. web_instances = [
  372. ["Odysee", "https://odysee.com/", "LBRY Inc.", "JavaScript"],
  373. ["Madiator", "https://madiator.com/", "Madiator2011", "JavaScript"],
  374. ["Spee.ch", "https://spee.ch/", "LBRY Inc.", "Direct Files"]
  375. ]
  376. def file_or_editor(args, comment):
  377. # We gonna check if dunring typing reply the user added
  378. # anything after the word reply.
  379. # reply emacs
  380. # reply gedit
  381. # reply vim
  382. # Or something like
  383. # reply /home/username/filename.txt
  384. # reply /home/username/filename.md
  385. # That might be useful to input multiline text.
  386. import os
  387. a = args.split()[0]
  388. a = os.path.expanduser(a)
  389. try:
  390. text = open(a, "r")
  391. text = text.read()
  392. except:
  393. text = open("/tmp/fastlbrycommentwriter.txt", "w")
  394. text.write(comment)
  395. text.close()
  396. os.system(a+" /tmp/fastlbrycommentwriter.txt")
  397. center("Press Enter when the file is ready and saved.")
  398. input()
  399. text = open("/tmp/fastlbrycommentwriter.txt", "r")
  400. text = text.read()
  401. return text.rstrip()