| 47 | return "{:,}{} {}".format(int(a),"" if not b else "."+b,biggest) |
| 48 | |
| 49 | def _process_hook(queue, total_size, bytes_so_far=0, update_interval=1.0, max_packets=0): |
| 50 | packets = [] |
| 51 | speed = remaining = "" |
| 52 | last_update = time.time() |
| 53 | while True: |
| 54 | # Write our info first so we have *some* status while |
| 55 | # waiting for packets |
| 56 | if total_size > 0: |
| 57 | percent = float(bytes_so_far) / total_size |
| 58 | percent = round(percent*100, 2) |
| 59 | t_s = get_size(total_size) |
| 60 | try: |
| 61 | b_s = get_size(bytes_so_far, t_s.split(" ")[1]) |
| 62 | except: |
| 63 | b_s = get_size(bytes_so_far) |
| 64 | perc_str = " {:.2f}%".format(percent) |
| 65 | bar_width = (TERMINAL_WIDTH // 3)-len(perc_str) |
| 66 | progress = "=" * int(bar_width * (percent/100)) |
| 67 | sys.stdout.write("\r\033[K{}/{} | {}{}{}{}{}".format( |
| 68 | b_s, |
| 69 | t_s, |
| 70 | progress, |
| 71 | " " * (bar_width-len(progress)), |
| 72 | perc_str, |
| 73 | speed, |
| 74 | remaining |
| 75 | )) |
| 76 | else: |
| 77 | b_s = get_size(bytes_so_far) |
| 78 | sys.stdout.write("\r\033[K{}{}".format(b_s, speed)) |
| 79 | sys.stdout.flush() |
| 80 | # Now we gather the next packet |
| 81 | try: |
| 82 | packet = queue.get(timeout=update_interval) |
| 83 | # Packets should be formatted as a tuple of |
| 84 | # (timestamp, len(bytes_downloaded)) |
| 85 | # If "DONE" is passed, we assume the download |
| 86 | # finished - and bail |
| 87 | if packet == "DONE": |
| 88 | print("") # Jump to the next line |
| 89 | return |
| 90 | # Append our packet to the list and ensure we're not |
| 91 | # beyond our max. |
| 92 | # Only check max if it's > 0 |
| 93 | packets.append(packet) |
| 94 | if max_packets > 0: |
| 95 | packets = packets[-max_packets:] |
| 96 | # Increment our bytes so far as well |
| 97 | bytes_so_far += packet[1] |
| 98 | except q.Empty: |
| 99 | # Didn't get anything - reset the speed |
| 100 | # and packets |
| 101 | packets = [] |
| 102 | speed = " | 0 B/s" |
| 103 | remaining = " | ?? left" if total_size > 0 else "" |
| 104 | except KeyboardInterrupt: |
| 105 | print("") # Jump to the next line |
| 106 | return |