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 4, 2024
1 parent 7a257d5 commit 9377a00
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 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 = self.config.use_color?

Check failure on line 107 in lib/foreman_maintain.rb

View workflow job for this annotation

GitHub Actions / rubocop / Rubocop

Style/RedundantSelf: Redundant `self` detected.
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?
# TODO: read config option?
::ENV['TERM'] && !`which tput 2> /dev/null`.empty? && `tput colors`.to_i > 0
end

private

def load_log_configs
Expand Down

0 comments on commit 9377a00

Please sign in to comment.