In-place progress output in the terminal or console

后端 未结 5 440
我寻月下人不归
我寻月下人不归 2020-12-13 09:39

When you run git clone, it updates progress in place. For example, the percentage of the objects received changes in place.

user@athena:~/cloj/s         


        
5条回答
  •  离开以前
    2020-12-13 09:58

    I wrote little class for multiline output update:

    class ConsoleReset
      # Unix
      # Contains a string to clear the line in the shell
      CLR = "\e[0K"
      # ANSI escape sequence for hiding terminal cursor
      ESC_CURS_INVIS = "\e[?25l"
      # ANSI escape sequence for showing terminal cursor
      ESC_CURS_VIS   = "\e[?25h"
      # ANSI escape sequence for clearing line in terminal
      ESC_R_AND_CLR  = "\r#{CLR}"
      # ANSI escape sequence for going up a line in terminal
      ESC_UP_A_LINE = "\e[1A"
    
      def initialize
        @first_call = true
      end
    
      def reset_line(text = '')
        # Initialise ANSI escape string
        escape = ""
    
        # The number of lines the previous message spanned
        lines = text.strip.lines.count - 1
    
        # Clear and go up a line
        lines.times { escape += "#{ESC_R_AND_CLR}#{ESC_UP_A_LINE}" }
    
        # Clear the line that is to be printed on
        # escape += "#{ESC_R_AND_CLR}"
    
        # Console is clear, we can print!
        STDOUT.print escape if !@first_call
        @first_call = false
        print text
      end
    
      def hide_cursor
        STDOUT.print(ESC_CURS_INVIS)
      end
    
      def show_cursor
        STDOUT.print(ESC_CURS_VIS)
      end
    
      def test
        hide_cursor
    
        5.times do |i|
          line = ['===========================================']
          (1..10).each do |num|
            line << ["#{num}:\t#{rand_num}"]
          end
          line << ['===========================================']
          line = line.join("\n")
          reset_line(line)
          sleep 1
        end
    
        show_cursor
    
        puts ''
      end
    
      private
        def rand_num
          rand(10 ** rand(10))
        end
    end
    

    Inspired by prydonius/spinning_cursor. See test method for example usage.

提交回复
热议问题