-
Notifications
You must be signed in to change notification settings - Fork 209
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 #1589 from ksss/colored-diff
Colored diff
- Loading branch information
Showing
11 changed files
with
122 additions
and
106 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
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,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 |
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
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,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 |
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