From 9377a00c056b08e0a8997ac4482987c5408ab045 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 4 Jul 2024 18:38:48 +0200 Subject: [PATCH] Auto-detect if colors are supported Not all output supports colors. For example, cron doesn't. This copies the autodetection from Kafo to disable colors if needed. --- lib/foreman_maintain.rb | 5 +++++ lib/foreman_maintain/config.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/foreman_maintain.rb b/lib/foreman_maintain.rb index 9b31a2fe5..70ba87aa8 100644 --- a/lib/foreman_maintain.rb +++ b/lib/foreman_maintain.rb @@ -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 @@ -102,6 +103,10 @@ def load_definitions end end + def configure_highline + HighLine.use_color = self.config.use_color? + end + def cache ObjectCache.instance end diff --git a/lib/foreman_maintain/config.rb b/lib/foreman_maintain/config.rb index 0aa2b0088..a69f99e71 100644 --- a/lib/foreman_maintain/config.rb +++ b/lib/foreman_maintain/config.rb @@ -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