brew/Library/Homebrew/messages.rb

62 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

2024-05-13 08:48:41 -04:00
# typed: strict
# frozen_string_literal: true
# A {Messages} object collects messages that may need to be displayed together
# at the end of a multi-step `brew` command run.
class Messages
2024-05-13 08:48:41 -04:00
sig { returns(T::Array[T::Hash[Symbol, Symbol]]) }
attr_reader :caveats
sig { returns(Integer) }
attr_reader :package_count
sig { returns(T::Array[T::Hash[String, Float]]) }
attr_reader :install_times
2020-10-20 12:03:48 +02:00
sig { void }
def initialize
2024-05-13 08:48:41 -04:00
@caveats = T.let([], T::Array[T::Hash[Symbol, Symbol]])
@package_count = T.let(0, Integer)
@install_times = T.let([], T::Array[T::Hash[String, Float]])
end
2024-05-13 08:48:41 -04:00
sig { params(package: String, caveats: T.any(String, Caveats)).void }
def record_caveats(package, caveats)
2024-03-07 16:20:20 +00:00
@caveats.push(package:, caveats:)
end
2024-05-13 08:48:41 -04:00
sig { params(package: String, elapsed_time: Float).void }
def package_installed(package, elapsed_time)
@package_count += 1
2024-03-07 16:20:20 +00:00
@install_times.push(package:, time: elapsed_time)
end
2024-05-13 08:48:41 -04:00
sig { params(force_caveats: T::Boolean, display_times: T::Boolean).void }
def display_messages(force_caveats: false, display_times: false)
display_caveats(force: force_caveats)
display_install_times if display_times
end
2024-05-13 08:48:41 -04:00
sig { params(force: T::Boolean).void }
def display_caveats(force: false)
return if @package_count.zero?
return if @package_count == 1 && !force
return if @caveats.empty?
2018-09-17 02:45:00 +02:00
oh1 "Caveats"
@caveats.each do |c|
ohai c[:package], c[:caveats]
end
end
2024-05-13 08:48:41 -04:00
sig { void }
def display_install_times
return if install_times.empty?
2018-09-17 02:45:00 +02:00
oh1 "Installation times"
install_times.each do |t|
puts format("%<package>-20s %<time>10.3f s", t)
end
end
end