Skip to content

Commit

Permalink
♻️ Use own colorize method
Browse files Browse the repository at this point in the history
  • Loading branch information
unhappychoice committed Aug 27, 2020
1 parent 55ea712 commit 22c15a5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
1 change: 0 additions & 1 deletion lib/circleci/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'launchy'
require 'terminal-table'
require 'highline/import'
require 'colorize'
require 'rugged'
require 'circleci'
require 'terminal-notifier'
Expand Down
10 changes: 6 additions & 4 deletions lib/circleci/cli/command/watch_command/build_watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion lib/circleci/cli/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/circleci/cli/printer/build_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/circleci/cli/printer/project_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/circleci/cli/printer/step_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/circleci/cli/response/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22c15a5

Please sign in to comment.