mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
72 lines
1.2 KiB
Ruby
72 lines
1.2 KiB
Ruby
![]() |
# frozen_string_literal: true
|
||
|
|
||
|
require "monitor"
|
||
|
|
||
|
module Context
|
||
|
extend MonitorMixin
|
||
|
|
||
|
def self.current=(context)
|
||
|
synchronize do
|
||
|
@current = context
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def self.current
|
||
|
if current_context = Thread.current[:context]
|
||
|
return current_context
|
||
|
end
|
||
|
|
||
|
synchronize do
|
||
|
@current ||= ContextStruct.new
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ContextStruct
|
||
|
def initialize(debug: nil, quiet: nil, verbose: nil)
|
||
|
@debug = debug
|
||
|
@quiet = quiet
|
||
|
@verbose = verbose
|
||
|
end
|
||
|
|
||
|
def debug?
|
||
|
@debug
|
||
|
end
|
||
|
|
||
|
def quiet?
|
||
|
@quiet
|
||
|
end
|
||
|
|
||
|
def verbose?
|
||
|
@verbose
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def debug?
|
||
|
Context.current.debug?
|
||
|
end
|
||
|
|
||
|
def quiet?
|
||
|
Context.current.quiet?
|
||
|
end
|
||
|
|
||
|
def verbose?
|
||
|
Context.current.verbose?
|
||
|
end
|
||
|
|
||
|
def with_context(**options)
|
||
|
old_context = Thread.current[:context]
|
||
|
|
||
|
new_context = ContextStruct.new(
|
||
|
debug: options.fetch(:debug, old_context&.debug?),
|
||
|
quiet: options.fetch(:quiet, old_context&.quiet?),
|
||
|
verbose: options.fetch(:verbose, old_context&.verbose?),
|
||
|
)
|
||
|
|
||
|
Thread.current[:context] = new_context
|
||
|
|
||
|
yield
|
||
|
ensure
|
||
|
Thread.current[:context] = old_context
|
||
|
end
|
||
|
end
|