-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1558 from ksss/collection-color
[Collection] Simple colorize collection text like Bundler
- Loading branch information
Showing
8 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module RBS | ||
module Collection | ||
class ColoredIO | ||
attr_reader :stdout | ||
|
||
def initialize(stdout:) | ||
@stdout = stdout | ||
end | ||
|
||
def puts_green(string) | ||
if can_display_colors? | ||
puts "\e[32m#{string}\e[m" | ||
else | ||
puts string | ||
end | ||
end | ||
|
||
def puts(string) | ||
stdout.puts(string) | ||
end | ||
|
||
private | ||
|
||
# https://github.com/rubygems/rubygems/blob/ed65279100234a17d65d71fe26de5083984ac5b8/bundler/lib/bundler/vendor/thor/lib/thor/shell/color.rb#L99-L109 | ||
def can_display_colors? | ||
are_colors_supported? && !are_colors_disabled? | ||
end | ||
|
||
def are_colors_supported? | ||
stdout.tty? && ENV["TERM"] != "dumb" | ||
end | ||
|
||
def are_colors_disabled? | ||
!ENV['NO_COLOR'].nil? && !ENV.fetch('NO_COLOR', '').empty? | ||
end | ||
end | ||
private_constant :ColoredIO | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module RBS | ||
module Collection | ||
class ColoredIO | ||
attr_reader stdout: CLI::_IO | ||
def initialize: (stdout: CLI::_IO) -> void | ||
def puts_green: (String) -> void | ||
def puts: (String) -> void | ||
|
||
private def can_display_colors?: () -> bool | ||
private def are_colors_supported?: () -> bool | ||
private def are_colors_disabled?: () -> bool | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters