Skip to content

Commit

Permalink
Auto-detect if colors are supported
Browse files Browse the repository at this point in the history
Not all output supports colors. For example, cron doesn't. This copies
the autodetection from Kafo to disable colors if needed.
  • Loading branch information
ekohl committed Jul 9, 2024
1 parent 7a257d5 commit 804fa51
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/foreman_maintain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def setup(options = {})

# using a queue, we can log the messages which are generated before initializing logger
self.config = Config.new(options)
configure_highline
load_definitions
init_logger
update_path
Expand Down Expand Up @@ -102,6 +103,10 @@ def load_definitions
end
end

def configure_highline
HighLine.use_color = config.use_color?
end

def cache
ObjectCache.instance
end
Expand Down
5 changes: 5 additions & 0 deletions lib/foreman_maintain/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def initialize(options)
@foreman_port = @options.fetch(:foreman_port, 443)
end

def use_color?
ENV['TERM'] && ENV.fetch('NO_COLOR', '') == '' && \
system('command -v tput', out: File.open('/dev/null')) && `tput colors`.to_i > 0
end

private

def load_log_configs
Expand Down
8 changes: 4 additions & 4 deletions test/lib/cli/health_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ module ForemanMaintain
[dummy-check-fail-skipwhitelist] Check that ends up with fail
[dummy-check-success] Check that ends up with success
[dummy-check-warn] Check that ends up with warning
[external-service-is-accessible] External_service_is_accessible [pre-upgrade-check]
[present-service-is-running] Present service run check [default]
[service-is-stopped] Service not running check [default]
[upgrade-post-upgrade-check] Procedures::Upgrade::PostUpgradeCheck [post-upgrade-checks]
[external-service-is-accessible] External_service_is_accessible [pre-upgrade-check]
[present-service-is-running] Present service run check [default]
[service-is-stopped] Service not running check [default]
[upgrade-post-upgrade-check] Procedures::Upgrade::PostUpgradeCheck [post-upgrade-checks]
OUTPUT
end
end
Expand Down
1 change: 0 additions & 1 deletion test/lib/reporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ def captured_out(simulate_terminal = true)
# simulate carriage returns to get the output as user would see it
out = capture.read
out = simulate_carriage_returns(out) if simulate_terminal
out = remove_colors(out)
capture.rewind
out
end
Expand Down
7 changes: 2 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENV['NO_COLOR'] = '1'
require 'foreman_maintain'
require 'minitest/spec'
require 'minitest/autorun'
Expand All @@ -19,7 +20,7 @@ def assert_cmd(expected_output, args = [], ignore_whitespace: false)
expected_output = expected_output.gsub(/\s+/, ' ')
output = output.gsub(/\s+/, ' ')
end
assert_equal expected_output, remove_colors(simulate_carriage_returns(output))
assert_equal expected_output, simulate_carriage_returns(output)
end

def capture_io_with_stderr
Expand Down Expand Up @@ -48,10 +49,6 @@ def run_cmd(args = [])
def simulate_carriage_returns(output)
output.gsub(/^.*\r/, '')
end

def remove_colors(output)
output.gsub(/\e.*?m/, '')
end
end

class FakePackageManager < ForemanMaintain::PackageManager::Base
Expand Down

0 comments on commit 804fa51

Please sign in to comment.