Skip to content

Commit

Permalink
Merge pull request #1558 from ksss/collection-color
Browse files Browse the repository at this point in the history
[Collection] Simple colorize collection text like Bundler
  • Loading branch information
soutaro authored Oct 17, 2023
2 parents e4b1a86 + 577a508 commit 88523a3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/rbs/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'yaml'
require 'bundler'

require_relative './collection/colored_io'
require_relative './collection/sources'
require_relative './collection/config'
require_relative './collection/config/lockfile'
Expand Down
41 changes: 41 additions & 0 deletions lib/rbs/collection/colored_io.rb
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
2 changes: 1 addition & 1 deletion lib/rbs/collection/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def install_from_lockfile
stdout: stdout
)
end
stdout.puts "It's done! #{selected.size} gems' RBSs now installed."
ColoredIO.new(stdout: stdout).puts_green("It's done! #{selected.size} gems' RBSs now installed.")
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/rbs/collection/sources/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,25 @@ def install(dest:, name:, version:, stdout:)

gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink?
stdout.puts "Updating to #{format_config_entry(name, version)} from a local source"
colored_io.puts_green("Updating to #{format_config_entry(name, version)} from a local source")
gem_dir.unlink
_install(dest: dest, name: name, version: version)
when gem_dir.directory?
prev = load_metadata(dir: gem_dir)

if prev == metadata_content(name: name, version: version)
stdout.puts "Using #{format_config_entry(name, version)}"
colored_io.puts "Using #{format_config_entry(name, version)}"
else
stdout.puts "Updating to #{format_config_entry(name, version)} from #{format_config_entry(prev["name"], prev["version"])}"
colored_io.puts_green("Updating to #{format_config_entry(name, version)} from #{format_config_entry(prev["name"], prev["version"])}")
FileUtils.remove_entry_secure(gem_dir.to_s)
_install(dest: dest, name: name, version: version)
end
when !gem_dir.exist?
stdout.puts "Installing #{format_config_entry(name, version)}"
colored_io.puts_green("Installing #{format_config_entry(name, version)}")
_install(dest: dest, name: name, version: version)
else
raise
Expand Down
12 changes: 7 additions & 5 deletions lib/rbs/collection/sources/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Local
include Base

attr_reader :path, :full_path

def initialize(path:, base_directory:)
# TODO: resolve relative path from dir of rbs_collection.yaml
@path = Pathname(path)
Expand All @@ -33,22 +33,24 @@ def install(dest:, name:, version:, stdout:)
from = @full_path.join(name, version)
gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink? && gem_dir.readlink == from
stdout.puts "Using #{name}:#{version} (#{from})"
colored_io.puts "Using #{name}:#{version} (#{from})"
when gem_dir.symlink?
prev = gem_dir.readlink
gem_dir.unlink
_install(from, dest.join(name, version))
stdout.puts "Updating #{name}:#{version} to #{from} from #{prev}"
colored_io.puts_green("Updating #{name}:#{version} to #{from} from #{prev}")
when gem_dir.directory?
# TODO: Show version of git source
FileUtils.remove_entry_secure(gem_dir.to_s)
_install(from, dest.join(name, version))
stdout.puts "Updating #{name}:#{version} from git source"
colored_io.puts_green("Updating #{name}:#{version} from git source")
when !gem_dir.exist?
_install(from, dest.join(name, version))
stdout.puts "Installing #{name}:#{version} (#{from})"
colored_io.puts_green("Installing #{name}:#{version} (#{from})")
else
raise
end
Expand Down
2 changes: 2 additions & 0 deletions sig/cli.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module RBS
interface _IO
def puts: (*untyped) -> void

def tty?: () -> bool

def print: (*untyped) -> void

def flush: () -> void
Expand Down
14 changes: 14 additions & 0 deletions sig/collection/colored_io.rbs
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
2 changes: 1 addition & 1 deletion test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def test_collection_install_gemspec

stdout, _ = run_rbs("collection", "install", bundler: true)

assert_match(/^Installing ast:(\d(\.\d)*)/, stdout)
assert_match(/Installing ast:(\d(\.\d)*)/, stdout)
refute_match(/^Using hola:(\d(\.\d)*)/, stdout)

assert dir.join('rbs_collection.lock.yaml').exist?
Expand Down

0 comments on commit 88523a3

Please sign in to comment.