Skip to content

Commit

Permalink
Merge pull request #1589 from ksss/colored-diff
Browse files Browse the repository at this point in the history
Colored diff
  • Loading branch information
soutaro committed Oct 31, 2023
2 parents a4993be + eb886aa commit a77932c
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 106 deletions.
5 changes: 4 additions & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

module RBS
class CLI
autoload :ColoredIO, 'rbs/cli/colored_io'
autoload :Diff, 'rbs/cli/diff'

class LibraryOptions
attr_accessor :core_root
attr_accessor :config_path
Expand Down Expand Up @@ -1389,7 +1392,7 @@ def run_subtract(args, _)
end

def run_diff(argv, library_options)
Diff::CLI.new(argv: argv, library_options: library_options, stdout: stdout, stderr: stderr).run
Diff.new(argv: argv, library_options: library_options, stdout: stdout, stderr: stderr).run
end
end
end
15 changes: 11 additions & 4 deletions lib/rbs/collection/colored_io.rb → lib/rbs/cli/colored_io.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
# frozen_string_literal: true

module RBS
module Collection
class CLI
class ColoredIO
attr_reader :stdout

def initialize(stdout:)
@stdout = stdout
end

def puts_red(string)
if can_display_colors?
puts "\e[31m#{string}\e[m"
else
puts string
end
end

def puts_green(string)
if can_display_colors?
puts "\e[32m#{string}\e[m"
Expand All @@ -17,8 +25,8 @@ def puts_green(string)
end
end

def puts(string)
stdout.puts(string)
def puts(...)
stdout.puts(...)
end

private
Expand All @@ -36,6 +44,5 @@ def are_colors_disabled?
!ENV['NO_COLOR'].nil? && !ENV.fetch('NO_COLOR', '').empty?
end
end
private_constant :ColoredIO
end
end
80 changes: 80 additions & 0 deletions lib/rbs/cli/diff.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

module RBS
class CLI
class Diff
def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
@format = nil
@stdout = stdout
@stderr = stderr

# @type var type_name: String?
type_name = nil
library_options = library_options
before_path = []
after_path = []

opt = OptionParser.new do |o|
o.banner = <<~HELP
[Experimental] This command is experimental. API and output compatibility is not guaranteed.
Usage:
rbs diff --format markdown --type-name Foo --before before_sig --after after_sig
Print diff for rbs environment dir
Examples:
# Diff dir1 and dir2 for Foo
$ rbs diff --format markdown --type-name Foo --before dir1 --after dir2
# Confirmation of methods related to Time class added by including stdlib/time
$ rbs diff --format diff --type-name Time --after stdlib/time
HELP
o.on("--format NAME") { |arg| @format = arg }
o.on("--type-name NAME") { |arg| type_name = arg }
o.on("--before DIR") { |arg| before_path << arg }
o.on("--after DIR") { |arg| after_path << arg }
end
opt.parse!(argv)

unless @format && type_name && ["markdown", "diff"].include?(@format)
@stderr.puts opt.banner
exit 1
end

@diff = RBS::Diff.new(
type_name: TypeName(type_name).absolute!,
library_options: library_options,
after_path: after_path,
before_path: before_path
)
end

def run
public_send("run_#{@format}")
end

def run_diff
first = true
io = RBS::CLI::ColoredIO.new(stdout: @stdout)
@diff.each_diff do |before, after|
io.puts if !first
io.puts_red "- #{before}"
io.puts_green "+ #{after}"
first = false
end
end

def run_markdown
@stdout.puts "| before | after |"
@stdout.puts "| --- | --- |"
@diff.each_diff do |before, after|
before.gsub!("|", "\\|")
after.gsub!("|", "\\|")
@stdout.puts "| `#{before}` | `#{after}` |"
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/rbs/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'yaml'
require 'bundler'

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

gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)
colored_io = CLI::ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink?
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/collection/sources/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def install(dest:, name:, version:, stdout:)
from = @full_path.join(name, version)
gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)
colored_io = CLI::ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink? && gem_dir.readlink == from
Expand Down
73 changes: 0 additions & 73 deletions lib/rbs/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,6 @@

module RBS
class Diff
class CLI
def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
@format = nil
@stdout = stdout
@stderr = stderr

type_name = nil
library_options = library_options
before_path = []
after_path = []

opt = OptionParser.new do |o|
o.banner = <<~HELP
[Experimental] This command is experimental. API and output compatibility is not guaranteed.
Usage:
rbs diff --format markdown --type-name Foo --before before_sig --after after_sig
Print diff for rbs environment dir
Examples:
# Diff dir1 and dir2 for Foo
$ rbs diff --format markdown --type-name Foo --before dir1 --after dir2
# Confirmation of methods related to Time class added by including stdlib/time
$ rbs diff --format diff --type-name Time --after stdlib/time
HELP
o.on("--format NAME") { |arg| @format = arg }
o.on("--type-name NAME") { |arg| type_name = arg }
o.on("--before DIR") { |arg| before_path << arg }
o.on("--after DIR") { |arg| after_path << arg }
end
opt.parse!(argv)

unless @format && type_name && ["markdown", "diff"].include?(@format)
@stderr.puts opt.banner
exit 1
end

@diff = Diff.new(
type_name: TypeName(type_name).absolute!,
library_options: library_options,
after_path: after_path,
before_path: before_path
)
end

def run
public_send("run_#{@format}")
end

def run_diff
first = true
@diff.each_diff do |before, after|
@stdout.puts if !first
@stdout.puts "- #{before}"
@stdout.puts "+ #{after}"
first = false
end
end

def run_markdown
@stdout.puts "| before | after |"
@stdout.puts "| --- | --- |"
@diff.each_diff do |before, after|
before.gsub!("|", "\\|")
after.gsub!("|", "\\|")
@stdout.puts "| `#{before}` | `#{after}` |"
end
end
end

def initialize(type_name:, library_options:, after_path: [], before_path: [])
@type_name = type_name
@library_options = library_options
Expand Down
5 changes: 3 additions & 2 deletions sig/collection/colored_io.rbs → sig/cli/colored_io.rbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module RBS
module Collection
class CLI
class ColoredIO
attr_reader stdout: CLI::_IO
def initialize: (stdout: CLI::_IO) -> void
def puts_green: (String) -> void
def puts: (String) -> void
def puts_red: (String) -> void
def puts: (?String) -> void

private def can_display_colors?: () -> bool
private def are_colors_supported?: () -> bool
Expand Down
21 changes: 21 additions & 0 deletions sig/cli/diff.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module RBS
class CLI
class Diff
@format: String
@stdout: _IO
@stderr: _IO
@diff: RBS::Diff

def initialize: (
argv: Array[String],
library_options: RBS::CLI::LibraryOptions,
?stdout: _IO,
?stderr: _IO,
) -> void

def run: () -> void
def run_diff: () -> void
def run_markdown: () -> void
end
end
end
22 changes: 0 additions & 22 deletions sig/diff.rbs
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
module RBS
class Diff
class CLI
interface _Puts
def puts: (*untyped) -> void
end

@format: String
@stdout: _Puts
@stderr: _Puts
@diff: Diff

def initialize: (
argv: Array[String],
library_options: RBS::CLI::LibraryOptions,
?stdout: _Puts,
?stderr: _Puts,
) -> void

def run: () -> void
def run_diff: () -> void
def run_markdown: () -> void
end

@type_name: TypeName
@library_options: RBS::CLI::LibraryOptions
@after_path: Array[String]
Expand Down

0 comments on commit a77932c

Please sign in to comment.