How to create a spinning command line cursor?

≡放荡痞女 提交于 2019-12-17 03:25:10

问题


Is there a way to print a spinning cursor in a terminal using Python?


回答1:


Something like this, assuming your terminal handles \b

import sys
import time

def spinning_cursor():
    while True:
        for cursor in '|/-\\':
            yield cursor

spinner = spinning_cursor()
for _ in range(50):
    sys.stdout.write(next(spinner))
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\b')



回答2:


Easy to use API (this will run the spinner in a separate thread):

import sys
import time
import threading

class Spinner:
    busy = False
    delay = 0.1

    @staticmethod
    def spinning_cursor():
        while 1: 
            for cursor in '|/-\\': yield cursor

    def __init__(self, delay=None):
        self.spinner_generator = self.spinning_cursor()
        if delay and float(delay): self.delay = delay

    def spinner_task(self):
        while self.busy:
            sys.stdout.write(next(self.spinner_generator))
            sys.stdout.flush()
            time.sleep(self.delay)
            sys.stdout.write('\b')
            sys.stdout.flush()

    def __enter__(self):
        self.busy = True
        threading.Thread(target=self.spinner_task).start()

    def __exit__(self, exception, value, tb):
        self.busy = False
        time.sleep(self.delay)
        if exception is not None:
            return False

Now use it in a with block anywhere in the code:

with Spinner():
  # ... some long-running operations
  # time.sleep(3) 



回答3:


A nice pythonic way is to use itertools.cycle:

import itertools, sys
spinner = itertools.cycle(['-', '/', '|', '\\'])
while True:
    sys.stdout.write(next(spinner))   # write the next character
    sys.stdout.flush()                # flush stdout buffer (actual character display)
    sys.stdout.write('\b')            # erase the last written char

Also, you might want to use threading to display the spinner during a long function call, as in http://www.interclasse.com/scripts/spin.php




回答4:


A solution:

import sys
import time

print "processing...\\",
syms = ['\\', '|', '/', '-']
bs = '\b'

for _ in range(10):
    for sym in syms:
        sys.stdout.write("\b%s" % sym)
        sys.stdout.flush()
        time.sleep(.5)

The key is to use the backspace character '\b' and flush stdout.




回答5:


Sure, it's possible. It's just a question of printing the backspace character (\b) in between the four characters that would make the "cursor" look like it's spinning ( -, \, |, /).




回答6:


curses module. i'd have a look at the addstr() and addch() functions. Never used it though.




回答7:


For more advanced console manipulations, on unix you can use the curses python module, and on windows, you can use WConio which provides equivalent functionality of the curses library.




回答8:


Grab the awesome progressbar module - http://code.google.com/p/python-progressbar/ use RotatingMarker.




回答9:


Improved version from @Victor Moyseenko as the original version had few issues

  1. was leaving spinner's characters after spinning is complete
  2. and sometimes lead to removing following output's first character too
  3. avoids a rare race condition by putting threading.Lock() on output
  4. falls back to simpler output when no tty is available (no spinning)
import sys
import threading
import itertools
import time

class Spinner:

    def __init__(self, message, delay=0.1):
        self.spinner = itertools.cycle(['-', '/', '|', '\\'])
        self.delay = delay
        self.busy = False
        self.spinner_visible = False
        sys.stdout.write(message)

    def write_next(self):
        with self._screen_lock:
            if not self.spinner_visible:
                sys.stdout.write(next(self.spinner))
                self.spinner_visible = True
                sys.stdout.flush()

    def remove_spinner(self, cleanup=False):
        with self._screen_lock:
            if self.spinner_visible:
                sys.stdout.write('\b')
                self.spinner_visible = False
                if cleanup:
                    sys.stdout.write(' ')       # overwrite spinner with blank
                    sys.stdout.write('\r')      # move to next line
                sys.stdout.flush()

    def spinner_task(self):
        while self.busy:
            self.write_next()
            time.sleep(self.delay)
            self.remove_spinner()

    def __enter__(self):
        if sys.stdout.isatty():
            self._screen_lock = threading.Lock()
            self.busy = True
            self.thread = threading.Thread(target=self.spinner_task)
            self.thread.start()

    def __exit__(self, exception, value, tb):
        if sys.stdout.isatty():
            self.busy = False
            self.remove_spinner(cleanup=True)
        else:
            sys.stdout.write('\r')

example of usage of the Spinner class above:


with Spinner("just waiting a bit.. "):

        time.sleep(3)

uploaded code to https://github.com/Tagar/stuff/blob/master/spinner.py




回答10:


Nice, simple, and clean...

while True:
    for i in '|\\-/':
        print('\b' + i, end='')



回答11:


#!/usr/bin/env python

import sys

chars = '|/-\\'

for i in xrange(1,1000):
    for c in chars:
        sys.stdout.write(c)
        sys.stdout.write('\b')
        sys.stdout.flush()

CAVEATS: In my experience this doesn't work in all terminals. A more robust way to do this under Unix/Linux, be it more complicated is to use the curses module, which doesn't work under Windows. You probably want to slow it down some how with actual processing that is going on in the background.




回答12:


import sys
def DrowWaitCursor(self, counter):
    if counter % 4 == 0:
        print("/",end = "")
    elif counter % 4 == 1:
        print("-",end = "")
    elif counter % 4 == 2:
        print("\\",end = "")
    elif counter % 4 == 3:
        print("|",end = "")
    sys.stdout.flush()
    sys.stdout.write('\b') 

This can be also another solution using a function with a parameter.




回答13:


Here ya go - simple and clear.

import sys
import time

idx = 0
cursor = ['|','/','-','\\'] #frames of an animated cursor

while True:
    sys.stdout.write(cursor[idx])
    sys.stdout.write('\b')
    idx = idx + 1

    if idx > 3:
        idx = 0

    time.sleep(.1)



回答14:


Crude but simple solution:

import sys
import time
cursor = ['|','/','-','\\']
for count in range(0,1000):
  sys.stdout.write('\b{}'.format(cursor[count%4]))
  sys.stdout.flush()
  # replace time.sleep() with some logic
  time.sleep(.1)

There are obvious limitations, but again, crude.




回答15:


I built a generic Singleton, shared by the entire application

from itertools import cycle
import threading
import time


class Spinner:
    __default_spinner_symbols_list = ['|-----|', '|#----|', '|-#---|', '|--#--|', '|---#-|', '|----#|']

def __init__(self, spinner_symbols_list: [str] = None):
    spinner_symbols_list = spinner_symbols_list if spinner_symbols_list else Spinner.__default_spinner_symbols_list
    self.__screen_lock = threading.Event()
    self.__spinner = cycle(spinner_symbols_list)
    self.__stop_event = False
    self.__thread = None

def get_spin(self):
    return self.__spinner

def start(self, spinner_message: str):
    self.__stop_event = False
    time.sleep(0.3)

    def run_spinner(message):
        while not self.__stop_event:
            print("\r{message} {spinner}".format(message=message, spinner=next(self.__spinner)), end="")
            time.sleep(0.3)

        self.__screen_lock.set()

    self.__thread = threading.Thread(target=run_spinner, args=(spinner_message,), daemon=True)
    self.__thread.start()

def stop(self):
    self.__stop_event = True
    if self.__screen_lock.is_set():
        self.__screen_lock.wait()
        self.__screen_lock.clear()
        print("\r", end="")

    print("\r", end="")

if __name__ == '__main__':
    import time
    # Testing
    spinner = Spinner()
    spinner.start("Downloading")
    # Make actions
    time.sleep(5) # Simulate a process
    #
    spinner.stop()


来源:https://stackoverflow.com/questions/4995733/how-to-create-a-spinning-command-line-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!