Printing an ASCII spinning “cursor” in the console

前端 未结 3 541
抹茶落季
抹茶落季 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:31

    # First define your chars
    pinwheel = %w{| / - \\}
    
    # Rotate and print as often as needed to "spin"
    def spin_it
      print "\b" + pinwheel.rotate!.first
    end
    

    EDIT from peter: here a working version

    def spin_it(times)
      pinwheel = %w{| / - \\}
      times.times do
        print "\b" + pinwheel.rotate!.first
        sleep(0.1)
      end
    end
    
    spin_it 10
    

提交回复
热议问题