Sha256: 54053b8d24287b01895fca6fd4c3b45364f0697a323b005884c6167968d4c0b5

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'tty/cursor'

module Capistrano
  module DataPlaneApi
    module TerminalPrintLoop
      class << self
        # Calls the passed block in an endless loop with a given interval
        # between calls.
        # It prints the `String` returned from the block and clears it
        # before another frame is printed.
        #
        # @yieldparam content [String]
        # @param interval [Integer] Number of seconds between each screen refresh
        def call(interval: 2)
          previous_line_count = 0
          previous_max_line_length = 0
          loop do
            content = ::String.new
            yielded = yield(content)

            print ::TTY::Cursor.clear_lines(previous_line_count + 1)

            content = yielded if yielded.is_a?(::String) && content.length.zero?
            line_count = 0
            max_line_length = 0
            content.each_line do |line|
              line_count += 1
              max_line_length = line.length if line.length > max_line_length
            end
            previous_line_count = line_count
            previous_max_line_length = max_line_length

            puts(content)
            sleep(interval)
          end
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
capistrano-data_plane_api-0.1.5 lib/capistrano/data_plane_api/terminal_print_loop.rb
capistrano-data_plane_api-0.1.4 lib/capistrano/data_plane_api/terminal_print_loop.rb
capistrano-data_plane_api-0.1.3 lib/capistrano/data_plane_api/terminal_print_loop.rb
capistrano-data_plane_api-0.1.2 lib/capistrano/data_plane_api/terminal_print_loop.rb
capistrano-data_plane_api-0.1.1 lib/capistrano/data_plane_api/terminal_print_loop.rb
capistrano-data_plane_api-0.1.0 lib/capistrano/data_plane_api/terminal_print_loop.rb