Module: RubyProgress::Utils
- Defined in:
- lib/ruby-progress/utils.rb
Overview
Universal terminal utilities shared between progress indicators.
This module provides common functionality for terminal manipulation, cursor control, and output formatting used across all progress indicator types.
Class Method Summary collapse
-
.clear_line(output_stream = :stderr) ⇒ void
Clears the current terminal line.
-
.clear_line_aggressive ⇒ void
Enhanced line clearing for daemon mode that handles output interruption.
-
.complete_with_clear(message, success: true, show_checkmark: false, output_stream: :warn, icons: {}) ⇒ void
Clears the current line and displays a completion message.
-
.display_completion(message, success: true, show_checkmark: false, output_stream: :warn, icons: {}) ⇒ void
Displays a completion message with optional icons and formatting.
-
.ends_valid?(ends_string) ⇒ Boolean
Validates an ends string for proper format.
-
.hide_cursor ⇒ void
Hides the terminal cursor.
-
.parse_ends(ends_string) ⇒ Array<String>
Parses start/end characters for animation wrapping.
-
.show_cursor ⇒ void
Shows the terminal cursor.
Class Method Details
.clear_line(output_stream = :stderr) ⇒ void
This method returns an undefined value.
Clears the current terminal line.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby-progress/utils.rb', line 35 def self.clear_line(output_stream = :stderr) io = case output_stream when :stdout $stdout when :stderr $stderr else # allow passing an IO-like object (e.g. StringIO) directly output_stream.respond_to?(:print) ? output_stream : $stderr end io.print "\r\e[K" end |
.clear_line_aggressive ⇒ void
This method returns an undefined value.
Enhanced line clearing for daemon mode that handles output interruption.
Clears the current line and the line above it, useful when daemon output has been interrupted by other command output.
57 58 59 60 61 |
# File 'lib/ruby-progress/utils.rb', line 57 def self.clear_line_aggressive $stderr.print "\r\e[2K" # Clear entire current line $stderr.print "\e[1A\e[2K" # Move up one line and clear it too $stderr.print "\r" # Return to start of line end |
.complete_with_clear(message, success: true, show_checkmark: false, output_stream: :warn, icons: {}) ⇒ void
This method returns an undefined value.
Clears the current line and displays a completion message.
Convenience method that combines line clearing with message display. Note: When output_stream is :warn, line clearing is already included in display_completion.
147 148 149 150 |
# File 'lib/ruby-progress/utils.rb', line 147 def self.complete_with_clear(, success: true, show_checkmark: false, output_stream: :warn, icons: {}) clear_line(output_stream) if output_stream != :warn # warn already includes clear in display_completion display_completion(, success: success, show_checkmark: show_checkmark, output_stream: output_stream, icons: icons) end |
.display_completion(message, success: true, show_checkmark: false, output_stream: :warn, icons: {}) ⇒ void
This method returns an undefined value.
Displays a completion message with optional icons and formatting.
Universal completion message display that handles success/error states, custom icons, and output stream selection.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby-progress/utils.rb', line 83 def self.display_completion(, success: true, show_checkmark: false, output_stream: :warn, icons: {}) return unless # Determine the mark to show. If checkmarks are enabled, prefer the # default icons but allow overrides via icons hash. If checkmarks are not # enabled, still show a custom icon when provided via CLI options. mark = '' if show_checkmark icon = success ? (icons[:success] || '✅') : (icons[:error] || '🛑') mark = "#{icon} " else custom_icon = success ? icons[:success] : icons[:error] mark = custom_icon ? "#{custom_icon} " : '' end = "#{mark}#{message}" # Resolve destination IO: support symbols (:stdout/:stderr/:warn) or an IO-like object dest_io = case output_stream when :stdout $stdout when :stderr $stderr when :warn $stderr else output_stream.respond_to?(:print) ? output_stream : $stderr end # Only treat explicit :stdout and :stderr as non-clearing requests. # For :warn and any other/custom stream, clear the current line first. unless i[stdout stderr].include?(output_stream) # Always include a leading carriage return when clearing to match # terminal behavior expected by the test-suite. dest_io.print "\r\e[2K" dest_io.flush if dest_io.respond_to?(:flush) end # Emit the message to the resolved destination IO. Use warn/puts when targeting # the standard streams to preserve familiar behavior (warn writes to $stderr). if dest_io == $stdout $stdout.puts elsif dest_io == $stderr warn else dest_io.puts end end |
.ends_valid?(ends_string) ⇒ Boolean
Validates an ends string for proper format.
Checks that the string is non-empty and has an even number of characters (handles multi-byte characters correctly).
195 196 197 198 199 200 |
# File 'lib/ruby-progress/utils.rb', line 195 def self.ends_valid?(ends_string) return false unless ends_string && !ends_string.empty? chars = ends_string.each_char.to_a !chars.empty? && (chars.length % 2).zero? end |
.hide_cursor ⇒ void
This method returns an undefined value.
Hides the terminal cursor.
14 15 16 |
# File 'lib/ruby-progress/utils.rb', line 14 def self.hide_cursor $stderr.print "\e[?25l" end |
.parse_ends(ends_string) ⇒ Array<String>
Parses start/end characters for animation wrapping.
Takes an even-length string and splits it in half to create start and end decorative characters for progress indicators. Handles multi-byte characters correctly.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruby-progress/utils.rb', line 168 def self.parse_ends(ends_string) return ['', ''] unless ends_string && !ends_string.empty? chars = ends_string.each_char.to_a return ['', ''] if chars.length.odd? || chars.empty? mid_point = chars.length / 2 start_chars = chars[0...mid_point].join end_chars = chars[mid_point..-1].join [start_chars, end_chars] end |
.show_cursor ⇒ void
This method returns an undefined value.
Shows the terminal cursor.
23 24 25 |
# File 'lib/ruby-progress/utils.rb', line 23 def self.show_cursor $stderr.print "\e[?25h" end |