Skip to content

Commit

Permalink
Move escape codes to Tty.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Aug 15, 2024
1 parent 5948aa0 commit f3f58eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Library/Homebrew/cmd/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def run

finished_downloads.each do |downloadable, future|
previous_pending_line_count -= 1
print "\033[K"
print Tty.clear_to_end
$stdout.flush
output_message.call(downloadable, future)
end
Expand All @@ -284,13 +284,13 @@ def run
remaining_downloads.each do |downloadable, future|
break if previous_pending_line_count >= [concurrency, (Tty.height - 1)].min

print "\033[K"
print Tty.clear_to_end
$stdout.flush
previous_pending_line_count += output_message.call(downloadable, future)
end

if previous_pending_line_count.positive?
$stdout.print "\033[#{previous_pending_line_count}A"
$stdout.print Tty.move_cursor_up(previous_pending_line_count)
$stdout.flush
end

Expand Down
10 changes: 10 additions & 0 deletions Library/Homebrew/utils/tty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def strip_ansi(string)
string.gsub(/\033\[\d+(;\d+)*m/, "")
end

sig { returns(String) }
def move_cursor_up(line_count)
"\033[#{line_count}A"
end

sig { returns(String) }
def clear_to_end
"\033[K"
end

sig { returns(String) }
def hide_cursor
"\033[?25l"
Expand Down

0 comments on commit f3f58eb

Please sign in to comment.