Printing an ASCII spinning “cursor” in the console

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

    I wrote a gem spin_to_win that displays a spinner while yielding a block. For example:

    SpinToWin.with_spinner('Zzzz') do |spinner| 
      spinner.banner('sleepy')
      sleep 1 
    end
    
    Zzzz \ [sleepy]
    

    It can also track work pending vs. work completed:

    SpinToWin.with_spinner('Zzzz') do |spinner|
        spinner.increment_todo!(3)
    
        spinner.banner('snore')
        sleep 1
        spinner.increment_done!
    
        spinner.banner('dream')
        sleep 1
        spinner.increment_done!
    
        spinner.banner('wake up!')
        sleep 1
        spinner.increment_done!
    end
    
    Zzzz \ 3 of 3 [wake up!]
    

提交回复
热议问题