From 22c15a5e89ef9e88a7160723e096120f5eb5567b Mon Sep 17 00:00:00 2001 From: Yuji Ueki Date: Thu, 27 Aug 2020 13:58:19 +0900 Subject: [PATCH] :recycle: Use own colorize method --- lib/circleci/cli.rb | 1 - .../command/watch_command/build_watcher.rb | 10 ++++--- lib/circleci/cli/printer.rb | 26 ++++++++++++++++++- lib/circleci/cli/printer/build_printer.rb | 2 +- lib/circleci/cli/printer/project_printer.rb | 2 +- lib/circleci/cli/printer/step_printer.rb | 10 +++---- lib/circleci/cli/response/build.rb | 8 +++--- 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/circleci/cli.rb b/lib/circleci/cli.rb index 72b745d..25bde71 100644 --- a/lib/circleci/cli.rb +++ b/lib/circleci/cli.rb @@ -5,7 +5,6 @@ require 'launchy' require 'terminal-table' require 'highline/import' -require 'colorize' require 'rugged' require 'circleci' require 'terminal-notifier' diff --git a/lib/circleci/cli/command/watch_command/build_watcher.rb b/lib/circleci/cli/command/watch_command/build_watcher.rb index c242bed..3a84b5e 100644 --- a/lib/circleci/cli/command/watch_command/build_watcher.rb +++ b/lib/circleci/cli/command/watch_command/build_watcher.rb @@ -46,9 +46,9 @@ def bind_event_handling(channel) # rubocop:disable Metrics/AbcSize, Metrics/Meth case json['log']['status'] when 'success' - puts "\e[2K\r#{json['log']['name'].green}" + puts "\e[2K\r#{Printer.colorize_green(json['log']['name'])}" when 'failed' - puts "\e[2K\r#{json['log']['name'].red}" + puts "\e[2K\r#{Printer.colorize_red(json['log']['name'])}" @messages[json['step']].each(&method(:say)) end end @@ -63,8 +63,10 @@ def notify_started def notify_stopped(status) text = case status - when 'success' then "🎉 #{@build.project_name} ##{@build.build_number} has succeeded!".green - when 'failed' then "😥 #{@build.project_name} ##{@build.build_number} has failed...".red + when 'success' + Printer.colorize_green("🎉 #{@build.project_name} ##{@build.build_number} has succeeded!") + when 'failed' + Printer.colorize_red("😥 #{@build.project_name} ##{@build.build_number} has failed...") end @verbose ? print_bordered(text) : say(text) diff --git a/lib/circleci/cli/printer.rb b/lib/circleci/cli/printer.rb index 21a6abf..c95abd3 100644 --- a/lib/circleci/cli/printer.rb +++ b/lib/circleci/cli/printer.rb @@ -6,6 +6,30 @@ module CircleCI module CLI - module Printer; end + module Printer + class << self + def colorize_red(string) + colorize(string, '0;31;49') + end + + def colorize_green(string) + colorize(string, '0;32;49') + end + + def colorize_yellow(string) + colorize(string, '0;33;49') + end + + def colorize_light_black(string) + colorize(string, '0;90;49') + end + + private + + def colorize(string, color_code) + "\e[#{color_code}m#{string}\e[0m" + end + end + end end end diff --git a/lib/circleci/cli/printer/build_printer.rb b/lib/circleci/cli/printer/build_printer.rb index e4273eb..a3b1304 100644 --- a/lib/circleci/cli/printer/build_printer.rb +++ b/lib/circleci/cli/printer/build_printer.rb @@ -37,7 +37,7 @@ def print_pretty def title build = @builds_to_show.first - "Recent Builds / #{build.project_name}".green + Printer.colorize_green("Recent Builds / #{build.project_name}") end def headings diff --git a/lib/circleci/cli/printer/project_printer.rb b/lib/circleci/cli/printer/project_printer.rb index f92f4b8..6d87652 100644 --- a/lib/circleci/cli/printer/project_printer.rb +++ b/lib/circleci/cli/printer/project_printer.rb @@ -27,7 +27,7 @@ def print_compact def print_pretty Terminal::Table.new( - title: 'Projects'.green, + title: Printer.colorize_green('Projects'), headings: ['User name', 'Repository name'], rows: @projects.map(&:information) ).to_s diff --git a/lib/circleci/cli/printer/step_printer.rb b/lib/circleci/cli/printer/step_printer.rb index e446cfb..f0b2f08 100644 --- a/lib/circleci/cli/printer/step_printer.rb +++ b/lib/circleci/cli/printer/step_printer.rb @@ -16,7 +16,7 @@ def to_s # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/Cyclom .group_by(&:type) .each do |key, steps| t << :separator - t << [{ value: key.green, alignment: :center, colspan: 2 }] + t << [{ value: Printer.colorize_green(key), alignment: :center, colspan: 2 }] steps.each { |s| print_actions(t, s) } end end.to_s @@ -35,10 +35,10 @@ def to_s # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/Cyclom def colorize_by_status(string, status) case status - when 'success', 'fixed' then string.green - when 'canceled' then string.yellow - when 'failed', 'timedout' then string.red - when 'no_tests', 'not_run' then string.light_black + when 'success', 'fixed' then Printer.colorize_green(string) + when 'canceled' then Printer.colorize_yellow(string) + when 'failed', 'timedout' then Printer.colorize_red(string) + when 'no_tests', 'not_run' then Printer.colorize_light_black(string) else string end end diff --git a/lib/circleci/cli/response/build.rb b/lib/circleci/cli/response/build.rb index ae9637f..7e608de 100644 --- a/lib/circleci/cli/response/build.rb +++ b/lib/circleci/cli/response/build.rb @@ -90,10 +90,10 @@ def steps def colorize_by_status(string, status) case status - when 'success', 'fixed' then string.green - when 'canceled' then string.yellow - when 'failed' then string.red - when 'no_tests', 'not_run' then string.light_black + when 'success', 'fixed' then Printer.colorize_green(string) + when 'canceled' then Printer.colorize_yellow(string) + when 'failed' then Printer.colorize_red(string) + when 'no_tests', 'not_run' then Printer.colorize_light_black(string) else string end end