Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-detect if colors are supported #898

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Comment on lines +43 to +46
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed. Probably because it now no longer generates color codes and the alignment has changed. That tells me the NO_COLOR bit works.

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
Loading