Printing an ASCII spinning “cursor” in the console

前端 未结 3 570
抹茶落季
抹茶落季 2020-12-14 11:18

I have a Ruby script that does some long taking jobs. It is command-line only and I would like to show that the script is still running and not halted. I used to like the so

3条回答
  •  感动是毒
    2020-12-14 11:39

    Yes, this works on Windows, OS X, and Linux. Improving on Niklas' suggestion, you can make this more general like so:

    def show_wait_cursor(seconds,fps=10)
      chars = %w[| / - \\]
      delay = 1.0/fps
      (seconds*fps).round.times{ |i|
        print chars[i % chars.length]
        sleep delay
        print "\b"
      }
    end
    
    show_wait_cursor(3)
    

    If you don't know how long the process will take, you can do this in another thread:

    def show_wait_spinner(fps=10)
      chars = %w[| / - \\]
      delay = 1.0/fps
      iter = 0
      spinner = Thread.new do
        while iter do  # Keep spinning until told otherwise
          print chars[(iter+=1) % chars.length]
          sleep delay
          print "\b"
        end
      end
      yield.tap{       # After yielding to the block, save the return value
        iter = false   # Tell the thread to exit, cleaning up after itself…
        spinner.join   # …and wait for it to do so.
      }                # Use the block's return value as the method's
    end
    
    print "Doing something tricky..."
    show_wait_spinner{
      sleep rand(4)+2 # Simulate a task taking an unknown amount of time
    }
    puts "Done!"
    

    This one outputs:

    Doing something tricky...|
    Doing something tricky.../
    Doing something tricky...-
    Doing something tricky...\ 
    (et cetera)
    Doing something tricky...done!
    

提交回复
热议问题