Sha256: ea9099d0e6422c2194689448880af8dcd3d0cd75aa364b535f7cee23137be1d9

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'io/console'
module CapistranoMulticonfigParallel
  # class used to fetch cursor position before displaying terminal table
  class Cursor
    class << self

      def fetch_position
        res = ''
        $stdin.raw do |stdin|
          $stdout << "\e[6n"
          $stdout.flush
          while (line = stdin.getc) != 'R'
            res << line if line
          end
        end
        position = res.match(/(?<row>\d+);(?<column>\d+)/)
        { row: position[:row].to_i, column: position[:column].to_i }
      end


      def display_on_screen(string, options = {})
        options = options.is_a?(Hash) ? options.stringify_keys : {}
        position = options.fetch('position', nil)
        clear_scren =  options.fetch('clear_screen', false)
        handle_string_display(position, clear_scren, string)
      end

      def handle_string_display(position, clear_scren, string)
        if clear_scren.to_s == 'true'
          terminal_clear_display(string)
        elsif position.present?
          display_string_at_position(position, string)
        end
      end

      def terminal_clear_display(string)
        terminal_clear
        puts string
      end

      def display_string_at_position(position, string)
        position_cursor(position[:row], position[:column])
        erase_from_current_line_to_bottom
        position_cursor(position[:row], position[:column])
        puts string
      end

      def move_to_home!
        print "\e[2;1H"
        erase_from_current_line_to_bottom
      end

      def erase_from_current_line_to_bottom
        puts "\e[J"
      end

      def position_cursor(line, column)
        puts("\eD\e[#{line};#{column}H")
      end

      def terminal_clear
        system('cls') || system('clear') || puts("\e[H\e[2J")
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano_multiconfig_parallel-0.28.2 lib/capistrano_multiconfig_parallel/classes/cursor.rb