<commit_before># -*- coding: UTF-8 -*-

from colorconsole.terminal import get_terminal
import time
import sys
import threading
if sys.version_info[0] <= 2:
    import Queue as queue
else:
    import queue

class Loading(threading.Thread):
    def __init__(self, frames=None):
        # Frames takes each frame seperated by a space with the very last frame
        # being the "done" frame.

        # Initialize self into a threading object and start running it.
        super(Loading, self).__init__()
        self.text_queue = queue.Queue()
        self.setDaemon(True)

        # Internal variables
        if frames == None:
            self.change_frames("snake")
        else:
            self.frames = frames.split(" ")
            self.time = 0.03
        self.t = get_terminal()
        self.text = ""
        self.frame = 0

    def change_frames(self, key):
        # Valid types: dots, circles
        switch = {
            "snake":    ("⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ ⠿", 0.03),
            "fatsnake": ("⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷ ⣿", 0.1),
            "drumming": ("⠋ ⠙ ⠚ ⠞ ⠖ ⠦ ⠴ ⠲ ⠳ ⠓ ⠿", 0.03),
            "pouring":  ("⠄ ⠆ ⠇ ⠋ ⠙ ⠸ ⠰ ⠠ ⠰ ⠸ ⠙ ⠋ ⠇ ⠆ ⠿", 0.05),
            "curls":    ("⠋ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠿", 0.05),
            "jumping":  ("⢄ ⢂ ⢁ ⡁ ⡈ ⡐ ⡠ ⠿", 0.05),
            "flash":     ("◯ ◉ ● ◉ ●", 0.2),
            "circles":  ("◜ ◠ ◝ ◞ ◡ ◟ ◯", 0.1),
            "bars":     ("▁ ▃ ▄ ▅ ▆ ▇ █ ▇ ▆ ▅ ▄ ▃ █", 0.1),
            "wheel":    ("| / - \\ |", 0.3),
            "pulse":    ("▉ ▊ ▋ ▌ ▍ ▎ ▏ ▎ ▍ ▌ ▋ ▊ ▉", 0.03),
            "arrows":   ("← ↖ ↑ ↗ → ↘ ↓ ↙ ↑", 0.1),
            "pipes":    ("┤ ┘ ┴ └ ├ ┌ ┬ ┐ ─", 0.1),
            "grow":     (". o O ° O o O", 0.1),
            "evolve":   (". o O @ * @ O o *", 0.1),
            "eyes":     ("◡◡ ⊙⊙ ◠◠ ⊙⊙ ⊙⊙", 0.3),
            "trigram":  ("☰ ☱ ☳ ☷ ☶ ☴ ☰ ☰ ☰", 0.1),
            "sphere":   ("🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘 🌑", 0.1),
            "dot":      ("⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈ .", 0.05)
        }

        if switch.get(key) == None:
            raise KeyError("Not a valid type. Must be of type: %s" % switch.keys())
        else:
            self.frames, self.time = switch.get(key)
            self.frames = self.frames.split(" ")


    def color_frames(self, n):
        # cyan = 36; purple = 35; blue = 34; green = 32; yewllow = 33; red = 31
        self.frames = ["\x1b[" + str(n) + "m\x1b[1m" + s + "\x1b[0m" for s in load.frames]

    def log(self, text):
        self.text_queue.put(text)

    def end(self, text=None):
        if text == None: text = self.text
        self.text_queue.put("quit")
        sys.stdout.write("\x1b[2K")
        print (self.frames[-1] + " " + text)

    def run(self):
        while True:
            if not self.text_queue.empty():
                self.text = self.text_queue.get()
                sys.stdout.write("\x1b[2K")
            if self.text == "quit":
                break

            if self.frame > len(self.frames) - 2:
                self.frame = 0
            sys.stdout.write(self.frames[self.frame] + " ")
            print (self.text)
            self.t.move_up()
            time.sleep(self.time)
            self.frame += 1


#import urllib2

load = Loading()
load.color_frames(36)
load.start()

load.log('Pinging "https://google.com"')
#urllib2.urlopen("https://google.com")
time.sleep(3)
load.log("Parsing results")
time.sleep(3)
load.end("Done pinging!")

load.join<commit_msg>Can accept `%s` as where the animation goes in the string.<commit_after># -*- coding: UTF-8 -*-

from colorconsole.terminal import get_terminal
import time
import sys
import threading
if sys.version_info[0] <= 2:
    import Queue as queue
else:
    import queue

class Loading(threading.Thread):
    def __init__(self, frames=None):
        # Frames takes each frame seperated by a space with the very last frame
        # being the "done" frame.

        # Initialize self into a threading object and start running it.
        super(Loading, self).__init__()
        self.text_queue = queue.Queue()
        self.setDaemon(True)

        # Internal variables
        if frames == None:
            self.change_frames("snake")
        else:
            self.frames = frames.split(" ")
            self.time = 0.03
        self.t = get_terminal()
        self.text = ""
        self.frame = 0

    def change_frames(self, key):
        # Valid types: dots, circles
        switch = {
            "snake":    ("⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ ⠿", 0.03),
            "fatsnake": ("⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷ ⣿", 0.1),
            "drumming": ("⠋ ⠙ ⠚ ⠞ ⠖ ⠦ ⠴ ⠲ ⠳ ⠓ ⠿", 0.03),
            "pouring":  ("⠄ ⠆ ⠇ ⠋ ⠙ ⠸ ⠰ ⠠ ⠰ ⠸ ⠙ ⠋ ⠇ ⠆ ⠿", 0.05),
            "curls":    ("⠋ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠿", 0.05),
            "jumping":  ("⢄ ⢂ ⢁ ⡁ ⡈ ⡐ ⡠ ⠿", 0.05),
            "flash":     ("◯ ◉ ● ◉ ●", 0.2),
            "circles":  ("◜ ◠ ◝ ◞ ◡ ◟ ◯", 0.1),
            "bars":     ("▁ ▃ ▄ ▅ ▆ ▇ █ ▇ ▆ ▅ ▄ ▃ █", 0.1),
            "wheel":    ("| / - \\ |", 0.3),
            "pulse":    ("▉ ▊ ▋ ▌ ▍ ▎ ▏ ▎ ▍ ▌ ▋ ▊ ▉", 0.03),
            "arrows":   ("← ↖ ↑ ↗ → ↘ ↓ ↙ ↑", 0.1),
            "pipes":    ("┤ ┘ ┴ └ ├ ┌ ┬ ┐ ─", 0.1),
            "grow":     (". o O ° O o O", 0.1),
            "evolve":   (". o O @ * @ O o *", 0.1),
            "eyes":     ("◡◡ ⊙⊙ ◠◠ ⊙⊙ ⊙⊙", 0.3),
            "trigram":  ("☰ ☱ ☳ ☷ ☶ ☴ ☰ ☰ ☰", 0.1),
            "sphere":   ("🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘 🌑", 0.1),
            "dot":      ("⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈ .", 0.05)
        }

        if switch.get(key) == None:
            raise KeyError("Not a valid type. Must be of type: %s" % switch.keys())
        else:
            self.frames, self.time = switch.get(key)
            self.frames = self.frames.split(" ")


    def color_frames(self, n):
        # cyan = 36; purple = 35; blue = 34; green = 32; yewllow = 33; red = 31
        self.frames = ["\x1b[" + str(n) + "m\x1b[1m" + s + "\x1b[0m" for s in load.frames]


# TODO: Make `log` accept with a %s in it so that the frame can be parsed in.

    def log(self, text):
        self.text_queue.put(text)

    def end(self, text=None):
        if text == None: text = self.text
        self.text_queue.put("quit")
        sys.stdout.write("\x1b[2K")
        print (text % self.frames[-1])
        self.join()

    def run(self):
        while True:
            if not self.text_queue.empty():
                self.text = self.text_queue.get()
                sys.stdout.write("\x1b[2K")
            if self.text == "quit":
                break

            if self.text:
                if self.frame > len(self.frames) - 2:
                    self.frame = 0
                print (self.text % self.frames[self.frame])
                #sys.stdout.write(self.frames[self.frame] + " ")
                #print (self.text)
                self.t.move_up()
                time.sleep(self.time)
                self.frame += 1


#import urllib2

load = Loading()
load.color_frames(36)
load.start()

load.log('%s Pinging "https://google.com"')
#urllib2.urlopen("https://google.com")
time.sleep(3)
load.log("%s Parsing results")
time.sleep(3)
load.end("%s Done pinging!")
{'fraction_non_alphanumeric': 0.19696754995040386, 'fraction_numerical': 0.023522743375371972, 'mean_word_length': 1.560957910014514, 'pattern_counts': {'":': 40, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '875116', 'n_tokens_mistral': 3394, 'n_tokens_neox': 2954, 'n_words': 826}