Module: Capistrano::DataPlaneApi::TerminalPrintLoop
- Defined in:
- lib/capistrano/data_plane_api/terminal_print_loop.rb
Overview
Provides a method that renders strings in a terminal with the given interval.
Class Method Summary collapse
-
.call(interval: 2, &_block) ⇒ Object
Calls the passed block in an endless loop with a given interval between calls.
Class Method Details
.call(interval: 2, &_block) ⇒ Object
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.
: (Integer) { (String) -> Object } -> void
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/capistrano/data_plane_api/terminal_print_loop.rb', line 19 def call(interval: 2, &_block) 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.empty? 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 |