rfix 1.4.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/rfix +78 -34
- data/lib/rfix.rb +17 -28
- data/lib/rfix/branch.rb +3 -25
- data/lib/rfix/branch/base.rb +27 -0
- data/lib/rfix/branch/head.rb +15 -0
- data/lib/rfix/branch/main.rb +33 -0
- data/lib/rfix/branch/name.rb +21 -0
- data/lib/rfix/branch/reference.rb +15 -0
- data/lib/rfix/branch/upstream.rb +17 -0
- data/lib/rfix/cli/command.rb +19 -0
- data/lib/rfix/cli/command/base.rb +61 -0
- data/lib/rfix/cli/command/branch.rb +13 -0
- data/lib/rfix/cli/command/config.rb +22 -0
- data/lib/rfix/cli/command/extension.rb +25 -0
- data/lib/rfix/cli/command/help.rb +11 -0
- data/lib/rfix/cli/command/info.rb +11 -0
- data/lib/rfix/cli/command/lint.rb +17 -0
- data/lib/rfix/cli/command/local.rb +11 -0
- data/lib/rfix/cli/command/origin.rb +11 -0
- data/lib/rfix/cli/command/setup.rb +11 -0
- data/lib/rfix/error.rb +5 -1
- data/lib/rfix/extension/offense.rb +79 -0
- data/lib/rfix/extension/pastel.rb +11 -0
- data/lib/rfix/extension/string.rb +12 -0
- data/lib/rfix/extension/strings.rb +9 -0
- data/lib/rfix/file.rb +6 -41
- data/lib/rfix/file/base.rb +73 -0
- data/lib/rfix/file/deleted.rb +17 -0
- data/lib/rfix/file/ignored.rb +17 -0
- data/lib/rfix/file/tracked.rb +42 -0
- data/lib/rfix/file/untracked.rb +20 -0
- data/lib/rfix/formatter.rb +125 -86
- data/lib/rfix/highlighter.rb +118 -0
- data/lib/rfix/indicator.rb +19 -0
- data/lib/rfix/log.rb +12 -121
- data/lib/rfix/rake/gemfile.rb +111 -0
- data/lib/rfix/rake/paths.rb +25 -23
- data/lib/rfix/rake/support.rb +72 -57
- data/lib/rfix/repository.rb +114 -164
- data/lib/rfix/types.rb +52 -0
- data/lib/rfix/version.rb +1 -1
- data/rfix.gemspec +28 -38
- data/vendor/dry-cli/CHANGELOG.md +191 -0
- data/vendor/dry-cli/CODEOWNERS +1 -0
- data/vendor/dry-cli/CODE_OF_CONDUCT.md +13 -0
- data/vendor/dry-cli/CONTRIBUTING.md +29 -0
- data/vendor/dry-cli/Gemfile +14 -0
- data/vendor/dry-cli/Gemfile.devtools +18 -0
- data/vendor/dry-cli/LICENSE +20 -0
- data/vendor/dry-cli/README.md +29 -0
- data/vendor/dry-cli/Rakefile +13 -0
- data/vendor/dry-cli/bin/console +15 -0
- data/vendor/dry-cli/bin/setup +8 -0
- data/vendor/dry-cli/changelog.yml +97 -0
- data/vendor/dry-cli/docsite/source/arguments.html.md +57 -0
- data/vendor/dry-cli/docsite/source/callbacks.html.md +51 -0
- data/vendor/dry-cli/docsite/source/commands-with-subcommands-and-params.md +86 -0
- data/vendor/dry-cli/docsite/source/commands.html.md +41 -0
- data/vendor/dry-cli/docsite/source/index.html.md +302 -0
- data/vendor/dry-cli/docsite/source/options.html.md +51 -0
- data/vendor/dry-cli/docsite/source/subcommands.html.md +38 -0
- data/vendor/dry-cli/docsite/source/variadic-arguments.html.md +45 -0
- data/vendor/dry-cli/dry-cli.gemspec +36 -0
- data/vendor/dry-cli/lib/dry/cli.rb +224 -0
- data/vendor/dry-cli/lib/dry/cli/banner.rb +135 -0
- data/vendor/dry-cli/lib/dry/cli/command.rb +387 -0
- data/vendor/dry-cli/lib/dry/cli/command_registry.rb +253 -0
- data/vendor/dry-cli/lib/dry/cli/errors.rb +37 -0
- data/vendor/dry-cli/lib/dry/cli/inflector.rb +17 -0
- data/vendor/dry-cli/lib/dry/cli/inline.rb +75 -0
- data/vendor/dry-cli/lib/dry/cli/option.rb +131 -0
- data/vendor/dry-cli/lib/dry/cli/parser.rb +138 -0
- data/vendor/dry-cli/lib/dry/cli/program_name.rb +21 -0
- data/vendor/dry-cli/lib/dry/cli/registry.rb +338 -0
- data/vendor/dry-cli/lib/dry/cli/usage.rb +94 -0
- data/vendor/dry-cli/lib/dry/cli/version.rb +8 -0
- data/vendor/dry-cli/project.yml +13 -0
- data/vendor/dry-cli/spec/integration/commands_spec.rb +14 -0
- data/vendor/dry-cli/spec/integration/inherited_commands_spec.rb +24 -0
- data/vendor/dry-cli/spec/integration/inline_spec.rb +43 -0
- data/vendor/dry-cli/spec/integration/processes_errors_spec.rb +29 -0
- data/vendor/dry-cli/spec/integration/rendering_spec.rb +31 -0
- data/vendor/dry-cli/spec/integration/single_command_spec.rb +81 -0
- data/vendor/dry-cli/spec/integration/subcommands_spec.rb +60 -0
- data/vendor/dry-cli/spec/integration/third_party_gems_spec.rb +18 -0
- data/vendor/dry-cli/spec/spec_helper.rb +15 -0
- data/vendor/dry-cli/spec/support/coverage.rb +15 -0
- data/vendor/dry-cli/spec/support/files.rb +13 -0
- data/vendor/dry-cli/spec/support/fixtures/based +65 -0
- data/vendor/dry-cli/spec/support/fixtures/baz +9 -0
- data/vendor/dry-cli/spec/support/fixtures/baz_command.rb +19 -0
- data/vendor/dry-cli/spec/support/fixtures/foo +588 -0
- data/vendor/dry-cli/spec/support/fixtures/infinites +31 -0
- data/vendor/dry-cli/spec/support/fixtures/inline +20 -0
- data/vendor/dry-cli/spec/support/fixtures/registry.rb +15 -0
- data/vendor/dry-cli/spec/support/fixtures/shared_commands.rb +596 -0
- data/vendor/dry-cli/spec/support/fixtures/with_block.rb +86 -0
- data/vendor/dry-cli/spec/support/fixtures/with_registry.rb +90 -0
- data/vendor/dry-cli/spec/support/fixtures/with_zero_arity_block.rb +87 -0
- data/vendor/dry-cli/spec/support/helpers.rb +37 -0
- data/vendor/dry-cli/spec/support/path.rb +24 -0
- data/vendor/dry-cli/spec/support/rspec.rb +26 -0
- data/vendor/dry-cli/spec/support/rspec_options.rb +16 -0
- data/vendor/dry-cli/spec/support/shared_examples/commands.rb +300 -0
- data/vendor/dry-cli/spec/support/shared_examples/inherited_commands.rb +197 -0
- data/vendor/dry-cli/spec/support/shared_examples/rendering.rb +181 -0
- data/vendor/dry-cli/spec/support/shared_examples/subcommands.rb +226 -0
- data/vendor/dry-cli/spec/support/shared_examples/third_party_gems.rb +49 -0
- data/vendor/dry-cli/spec/support/warnings.rb +10 -0
- data/vendor/dry-cli/spec/unit/dry/cli/cli_spec.rb +123 -0
- data/vendor/dry-cli/spec/unit/dry/cli/inflector_spec.rb +26 -0
- data/vendor/dry-cli/spec/unit/dry/cli/registry_spec.rb +78 -0
- data/vendor/dry-cli/spec/unit/dry/cli/version_spec.rb +7 -0
- data/vendor/strings-ansi/CHANGELOG.md +24 -0
- data/vendor/strings-ansi/CODE_OF_CONDUCT.md +74 -0
- data/vendor/strings-ansi/Gemfile +11 -0
- data/{LICENSE.txt → vendor/strings-ansi/LICENSE.txt} +1 -1
- data/vendor/strings-ansi/README.md +155 -0
- data/vendor/strings-ansi/Rakefile +8 -0
- data/vendor/strings-ansi/appveyor.yml +32 -0
- data/vendor/strings-ansi/bin/console +14 -0
- data/vendor/strings-ansi/bin/setup +8 -0
- data/vendor/strings-ansi/lib/strings-ansi.rb +1 -0
- data/vendor/strings-ansi/lib/strings/ansi.rb +84 -0
- data/vendor/strings-ansi/lib/strings/ansi/extensions.rb +23 -0
- data/vendor/strings-ansi/lib/strings/ansi/version.rb +7 -0
- data/vendor/strings-ansi/spec/fixtures/ansi_codes.yaml +194 -0
- data/vendor/strings-ansi/spec/spec_helper.rb +51 -0
- data/vendor/strings-ansi/spec/unit/ansi_spec.rb +15 -0
- data/vendor/strings-ansi/spec/unit/extensions_spec.rb +19 -0
- data/vendor/strings-ansi/spec/unit/only_ansi_spec.rb +36 -0
- data/vendor/strings-ansi/spec/unit/sanitize_spec.rb +53 -0
- data/vendor/strings-ansi/strings-ansi.gemspec +34 -0
- data/vendor/strings-ansi/tasks/console.rake +11 -0
- data/vendor/strings-ansi/tasks/coverage.rake +11 -0
- data/vendor/strings-ansi/tasks/spec.rake +29 -0
- metadata +274 -188
- data/.github/workflows/main.yml +0 -26
- data/.gitignore +0 -43
- data/.rspec +0 -2
- data/.rubocop.yml +0 -87
- data/.travis.yml +0 -35
- data/Gemfile +0 -2
- data/Gemfile.base +0 -14
- data/Gemfile.base.lock +0 -172
- data/Gemfile.lock +0 -188
- data/Guardfile +0 -16
- data/Makefile +0 -12
- data/README.md +0 -85
- data/Rakefile +0 -31
- data/bin/bundle +0 -114
- data/bin/console +0 -29
- data/bin/guard +0 -29
- data/bin/rake +0 -29
- data/bin/rfix +0 -29
- data/bin/rspec +0 -29
- data/bin/setup +0 -29
- data/ci/Gemfile.rubocop-0.80 +0 -2
- data/ci/Gemfile.rubocop-0.80.lock +0 -170
- data/ci/Gemfile.rubocop-0.81 +0 -2
- data/ci/Gemfile.rubocop-0.81.lock +0 -170
- data/ci/Gemfile.rubocop-0.82 +0 -2
- data/ci/Gemfile.rubocop-0.82.lock +0 -170
- data/ci/Gemfile.rubocop-0.83 +0 -2
- data/ci/Gemfile.rubocop-0.83.lock +0 -168
- data/ci/Gemfile.rubocop-0.84 +0 -2
- data/ci/Gemfile.rubocop-0.84.lock +0 -171
- data/ci/Gemfile.rubocop-0.85 +0 -2
- data/ci/Gemfile.rubocop-0.85.1 +0 -2
- data/ci/Gemfile.rubocop-0.85.1.lock +0 -173
- data/ci/Gemfile.rubocop-0.85.lock +0 -173
- data/lib/rfix/box.rb +0 -112
- data/lib/rfix/branches/base.rb +0 -15
- data/lib/rfix/branches/head.rb +0 -13
- data/lib/rfix/branches/main.rb +0 -28
- data/lib/rfix/branches/name.rb +0 -23
- data/lib/rfix/branches/reference.rb +0 -21
- data/lib/rfix/branches/upstream.rb +0 -13
- data/lib/rfix/cmd.rb +0 -39
- data/lib/rfix/commands/branch.rb +0 -15
- data/lib/rfix/commands/extensions/options.rb +0 -8
- data/lib/rfix/commands/help.rb +0 -7
- data/lib/rfix/commands/helper/args.rb +0 -141
- data/lib/rfix/commands/helper/help.rb +0 -6
- data/lib/rfix/commands/helper/loader.rb +0 -6
- data/lib/rfix/commands/helper/option.rb +0 -0
- data/lib/rfix/commands/helper/params.rb +0 -0
- data/lib/rfix/commands/helper/rubocop.rb +0 -17
- data/lib/rfix/commands/info.rb +0 -30
- data/lib/rfix/commands/lint.rb +0 -22
- data/lib/rfix/commands/local.rb +0 -12
- data/lib/rfix/commands/origin.rb +0 -19
- data/lib/rfix/commands/setup.rb +0 -29
- data/lib/rfix/commands/welcome.rb +0 -24
- data/lib/rfix/deleted.rb +0 -13
- data/lib/rfix/extensions/extensions.rb +0 -18
- data/lib/rfix/extensions/offense.rb +0 -78
- data/lib/rfix/extensions/string.rb +0 -8
- data/lib/rfix/file_cache.rb +0 -59
- data/lib/rfix/git_helper.rb +0 -59
- data/lib/rfix/indentation.rb +0 -39
- data/lib/rfix/loader/bundler.rb +0 -37
- data/lib/rfix/loader/env.rb +0 -33
- data/lib/rfix/loader/spec.rb +0 -41
- data/lib/rfix/no_file.rb +0 -13
- data/lib/rfix/rfix.rb +0 -34
- data/lib/rfix/tracked.rb +0 -72
- data/lib/rfix/tracked_file.rb +0 -16
- data/lib/rfix/untracked.rb +0 -13
- data/resources/ps.png +0 -0
- data/tasks/bump.rake +0 -11
- data/tasks/bundle.rake +0 -17
- data/tasks/complex.rake +0 -54
- data/tasks/execute.rake +0 -38
- data/tasks/libgit2.rake +0 -33
- data/tasks/simple.rake +0 -62
- data/tasks/travis.rake +0 -74
- data/tasks/vendor.rake +0 -34
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/types"
|
4
|
+
|
5
|
+
module Rfix
|
6
|
+
module File
|
7
|
+
class Untracked < Base
|
8
|
+
attribute :status, Types::Status::Untracked
|
9
|
+
|
10
|
+
def untracked?
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def include?(*)
|
15
|
+
true
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/rfix/formatter.rb
CHANGED
@@ -1,128 +1,167 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
4
|
-
require "
|
5
|
-
require "
|
6
|
-
require "
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
require "rubocop/formatter/simple_text_formatter"
|
5
|
+
require "rubocop/cop/offense"
|
6
|
+
require "dry/core/constants"
|
7
|
+
require "dry/initializer"
|
8
|
+
require "tty/screen"
|
9
|
+
require "tty/prompt"
|
10
|
+
require "tty/box"
|
11
|
+
|
12
|
+
RuboCop::Cop::Offense.prepend(Rfix::Extension::Offense)
|
7
13
|
|
8
14
|
module Rfix
|
9
15
|
class Formatter < RuboCop::Formatter::SimpleTextFormatter
|
10
|
-
|
16
|
+
attr_reader :indicator
|
17
|
+
|
18
|
+
include Dry::Core::Constants
|
19
|
+
extend Dry::Initializer
|
20
|
+
include Log
|
21
|
+
|
22
|
+
option :indicator, default: -> { Indicator.new }
|
23
|
+
option :reported_offenses
|
24
|
+
option :options
|
25
|
+
option :output
|
26
|
+
|
27
|
+
PROMPT = TTY::Prompt.new(symbols: { marker: ">" })
|
28
|
+
SPACE = " "
|
29
|
+
|
30
|
+
delegate :say, to: :PROMPT
|
31
|
+
|
32
|
+
class NullRepository
|
33
|
+
include Singleton
|
34
|
+
|
35
|
+
def include_file?(*)
|
36
|
+
true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(output, options = EMPTY_HASH)
|
41
|
+
super(output: output, options: options, reported_offenses: EMPTY_ARRAY.dup)
|
42
|
+
end
|
11
43
|
|
12
44
|
def started(files)
|
13
|
-
|
14
|
-
@formatter = Rouge::Formatters::TerminalTruecolor.new(theme)
|
15
|
-
@current = 0
|
16
|
-
@total = files.count
|
17
|
-
@files = {}
|
18
|
-
@lexer = Rouge::Lexers::Ruby.new
|
19
|
-
@pg = CLI::UI::Progress.new
|
20
|
-
@all_files = files
|
45
|
+
# indicator.start("{{italic:rfix}} is linting {{bold:#{files.count}}} files, hold on ...")
|
21
46
|
end
|
22
47
|
|
23
|
-
|
24
|
-
|
48
|
+
# @files [Array<File>]
|
49
|
+
def finished(files)
|
50
|
+
# @indicator.stop
|
51
|
+
mark_command_line
|
52
|
+
report_summary(files)
|
25
53
|
end
|
26
54
|
|
27
|
-
|
28
|
-
|
55
|
+
# @file [File]
|
56
|
+
# @offenses [Array<Offence>]
|
57
|
+
def file_finished(*, offenses)
|
58
|
+
@reported_offenses += offenses
|
29
59
|
|
30
|
-
|
31
|
-
offenses = @files.fetch(file)
|
32
|
-
corrected = offenses.select(&:corrected?)
|
60
|
+
# @indicator.stop if offenses?
|
33
61
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
else
|
39
|
-
say_error truncate(file)
|
62
|
+
length = offenses.length - 1
|
63
|
+
offenses.each_with_index do |offense, _index|
|
64
|
+
framed(offense) do
|
65
|
+
report_line_with_highlight(offense)
|
40
66
|
end
|
67
|
+
|
68
|
+
puts
|
41
69
|
end
|
42
70
|
end
|
43
71
|
|
44
|
-
|
45
|
-
render_files(files)
|
72
|
+
private
|
46
73
|
|
47
|
-
|
48
|
-
|
74
|
+
using(Module.new do
|
75
|
+
refine String do
|
76
|
+
def surround(value)
|
77
|
+
value + self + value
|
78
|
+
end
|
49
79
|
end
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
80
|
+
end)
|
81
|
+
|
82
|
+
def framed(offense, &block)
|
83
|
+
puts TTY::Box.frame({
|
84
|
+
width: TTY::Screen.width,
|
85
|
+
padding: [1, 1, 0, 1],
|
86
|
+
title: {
|
87
|
+
top_left: "#{offense.icon} #{offense.msg}".surround(SPACE),
|
88
|
+
bottom_left: offense.clickable_severity&.surround(SPACE),
|
89
|
+
bottom_right: offense.clickable_path&.surround(SPACE)
|
90
|
+
}.compact
|
91
|
+
}, &block)
|
55
92
|
end
|
56
93
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
60
|
-
offenses.each do |offense|
|
61
|
-
out("\n\n")
|
62
|
-
CLI::UI::Frame.open("#{offense.icon} #{offense.msg}", color: :reset)
|
63
|
-
report_line(file, offense, offense.location, offense.highlighted_area)
|
64
|
-
CLI::UI::Frame.close("#{offense.clickable_severity} » #{offense.clickable_path}", color: :reset)
|
65
|
-
end
|
94
|
+
def report_summary(files)
|
95
|
+
super(*stats.insert(0, files.count).take(arity))
|
66
96
|
end
|
67
97
|
|
68
|
-
def
|
69
|
-
|
98
|
+
def arity
|
99
|
+
method(:report_summary).super_method.arity
|
70
100
|
end
|
71
101
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
75
|
-
unless Rfix.test?
|
76
|
-
@pg.tick(set_percent: (@current / @total))
|
77
|
-
end
|
78
|
-
@files[file] = offenses
|
102
|
+
def mark_command_line
|
103
|
+
# "#{ESC}]1337;SetMark\a"
|
104
|
+
"" # TODO: Activate
|
79
105
|
end
|
80
106
|
|
81
|
-
def
|
82
|
-
|
107
|
+
def report(msg, format: true)
|
108
|
+
msg
|
83
109
|
end
|
84
110
|
|
85
|
-
def
|
86
|
-
|
111
|
+
def newline(amount = 1)
|
112
|
+
report("\n" * amount)
|
87
113
|
end
|
88
114
|
|
89
|
-
def
|
90
|
-
|
115
|
+
def report_line_with_highlight(offense)
|
116
|
+
location = offense.location
|
117
|
+
|
118
|
+
unless location.respond_to?(:source_buffer)
|
119
|
+
return "Source not found"
|
120
|
+
end
|
121
|
+
|
122
|
+
buffer = location.source_buffer
|
123
|
+
|
124
|
+
source = buffer.source
|
125
|
+
line = location.line
|
126
|
+
last_line = buffer.last_line
|
127
|
+
surrounding_lines = 2
|
128
|
+
|
129
|
+
min_line = [line - surrounding_lines * 2, 1].max
|
130
|
+
max_line = [line + surrounding_lines * 2, last_line].min
|
131
|
+
|
132
|
+
begin_index = buffer.line_range(min_line).begin_pos
|
133
|
+
end_index = buffer.line_range(max_line).end_pos
|
134
|
+
|
135
|
+
visible = begin_index...end_index
|
136
|
+
highlight = location.to_range
|
137
|
+
|
138
|
+
highlighter = Highlighter.new(
|
139
|
+
visible_lines: (min_line..max_line),
|
140
|
+
highlight: highlight,
|
141
|
+
visible: visible
|
142
|
+
)
|
143
|
+
|
144
|
+
(method(:report) << highlighter).call(source)
|
91
145
|
end
|
92
146
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
source_after_highlight(offense)
|
97
|
-
end
|
147
|
+
def corrected
|
148
|
+
reported_offenses.select(&:corrected?)
|
149
|
+
end
|
98
150
|
|
99
|
-
def
|
100
|
-
|
101
|
-
|
151
|
+
def correctable
|
152
|
+
reported_offenses.select(&:correctable?)
|
153
|
+
end
|
102
154
|
|
103
|
-
def
|
104
|
-
|
105
|
-
source_line[0...offense.highlighted_area.begin_pos]
|
155
|
+
def stats
|
156
|
+
[reported_offenses.count, corrected.count, correctable.count]
|
106
157
|
end
|
107
158
|
|
108
|
-
def
|
109
|
-
|
110
|
-
source_line[offense.highlighted_area.end_pos..-1]
|
159
|
+
def offenses?
|
160
|
+
reported_offenses.any?
|
111
161
|
end
|
112
162
|
|
113
|
-
def
|
114
|
-
|
115
|
-
src = highlighted_source_line(offense).lines.map { |line| extra + line }.join("\n")
|
116
|
-
indent = Indentation.new(src, extra_indentation: 2)
|
117
|
-
src = indent.call
|
118
|
-
lines = @formatter.format(@lexer.lex(src)).gsub('\\e', CLI::UI::ANSI::ESC).lines.map(&:chomp)
|
119
|
-
out("\n\n")
|
120
|
-
out(lines.join("\n"), format: false)
|
121
|
-
b_pos = highlighted_area.begin_pos + extra.length * 2 - indent.min_indentation
|
122
|
-
e_pos = highlighted_area.end_pos + extra.length * 2 - indent.min_indentation
|
123
|
-
size = e_pos - b_pos
|
124
|
-
out((" " * b_pos) + Rainbow((" " * [size, 0].max)).underline.bold)
|
125
|
-
out("\n\n")
|
163
|
+
def repository
|
164
|
+
NullRepository.instance
|
126
165
|
end
|
127
166
|
end
|
128
167
|
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/core/constants"
|
4
|
+
require "dry/initializer"
|
5
|
+
require "dry/types"
|
6
|
+
require "rouge"
|
7
|
+
require "pastel"
|
8
|
+
|
9
|
+
module Rfix
|
10
|
+
class Highlighter < Rouge::Formatters::TerminalTruecolor
|
11
|
+
tag "highlighter"
|
12
|
+
|
13
|
+
TEXT = Rouge::Token::Tokens::Text
|
14
|
+
NEWLINE = "\n"
|
15
|
+
SPACE = " "
|
16
|
+
|
17
|
+
include Dry::Core::Constants
|
18
|
+
extend Dry::Initializer
|
19
|
+
|
20
|
+
module Types
|
21
|
+
include Dry::Types()
|
22
|
+
end
|
23
|
+
|
24
|
+
param :theme, default: -> { Rouge::Themes::Gruvbox.new }
|
25
|
+
param :lexer, default: -> { Rouge::Lexers::Ruby.new }
|
26
|
+
option :visible_lines, type: Types::Range
|
27
|
+
option :highlight, type: Types::Range
|
28
|
+
option :visible, type: Types::Range
|
29
|
+
|
30
|
+
def call(source)
|
31
|
+
unless source.is_a?(String)
|
32
|
+
raise Error, "Source must be a string"
|
33
|
+
end
|
34
|
+
|
35
|
+
unless source.end_with?(NEWLINE)
|
36
|
+
return call(source + NEWLINE)
|
37
|
+
end
|
38
|
+
|
39
|
+
format(lexer.lex(source))
|
40
|
+
end
|
41
|
+
|
42
|
+
def stream(tokens, &block)
|
43
|
+
max_with = TTY::Screen.width
|
44
|
+
lines = token_lines(tokens)
|
45
|
+
pastel = Pastel.new
|
46
|
+
prefix_spaces = 2
|
47
|
+
|
48
|
+
indentation = lines.map.with_index(1) do |tokens, lineno|
|
49
|
+
next unless visible_lines.include?(lineno)
|
50
|
+
|
51
|
+
text = tokens.map(&:last).join
|
52
|
+
|
53
|
+
next if text.empty?
|
54
|
+
|
55
|
+
text.chars.take_while do |char|
|
56
|
+
char.strip.empty?
|
57
|
+
end.length
|
58
|
+
end.compact.min || 0
|
59
|
+
|
60
|
+
is_h = lines.reduce([0, 1, EMPTY_HASH]) do |(position, lineno, lookup), tokens|
|
61
|
+
tokens.reduce([position, lineno, lookup.dup]) do |(index, lineno, lookup), (_, value)|
|
62
|
+
[index + value.length, lineno, lookup].tap do
|
63
|
+
if highlight.include?(index)
|
64
|
+
lookup[lineno] = true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end.then do |index, lineno, lookup|
|
68
|
+
[index.succ, lineno.succ, lookup]
|
69
|
+
end
|
70
|
+
end.last
|
71
|
+
|
72
|
+
lines.reduce([0, 1]) do |(position, lineno), tokens|
|
73
|
+
print_line_number = lambda do
|
74
|
+
block.call(SPACE * 2)
|
75
|
+
|
76
|
+
style = is_h[lineno] ? pastel.yellow.detach : pastel.dim.detach
|
77
|
+
(block << style).call(lineno.to_s.ljust(4, SPACE) + SPACE)
|
78
|
+
end
|
79
|
+
|
80
|
+
tokens.reduce(position) do |index, (token, value)|
|
81
|
+
(index + value.length).tap do
|
82
|
+
if index == position
|
83
|
+
value = value.chars.drop(indentation).join
|
84
|
+
|
85
|
+
if visible_lines.include?(lineno)
|
86
|
+
print_line_number.call
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
if highlight.include?(index) && visible_lines.include?(lineno)
|
91
|
+
if index == position
|
92
|
+
head = value.chars.take_while(&:blank?).join
|
93
|
+
tail = value.chars.drop_while(&:blank?).join
|
94
|
+
|
95
|
+
super([[TEXT, head]], &block)
|
96
|
+
super([[token, tail]], &block)
|
97
|
+
else
|
98
|
+
super([[token, value]], &block)
|
99
|
+
end
|
100
|
+
elsif visible_lines.include?(lineno)
|
101
|
+
super([[token, value]], &block)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end.tap do |new_position|
|
105
|
+
if visible_lines.include?(lineno)
|
106
|
+
if position == new_position
|
107
|
+
print_line_number.call
|
108
|
+
end
|
109
|
+
|
110
|
+
super([[TEXT, NEWLINE]], &block)
|
111
|
+
end
|
112
|
+
end.then do |position|
|
113
|
+
[position.succ, lineno.succ]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/rfix/log.rb
CHANGED
@@ -1,132 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
4
|
-
require "
|
3
|
+
require "tty/box"
|
4
|
+
require "tty/prompt"
|
5
|
+
require "tty/screen"
|
5
6
|
|
6
|
-
module Rfix
|
7
|
-
|
8
|
-
|
9
|
-
prt("{{v}} #{message}")
|
10
|
-
end
|
11
|
-
|
12
|
-
def say_error(message)
|
13
|
-
prt("{{x}} #{message}")
|
14
|
-
end
|
15
|
-
|
16
|
-
def say_error_sub(message)
|
17
|
-
prt(message.to_s)
|
18
|
-
end
|
19
|
-
|
20
|
-
def error_box(title)
|
21
|
-
box(title, color: :red) { yield }
|
22
|
-
end
|
23
|
-
|
24
|
-
def abort_box(title)
|
25
|
-
error_box(title) { yield }
|
26
|
-
exit 1
|
27
|
-
end
|
7
|
+
module Rfix
|
8
|
+
module Log
|
9
|
+
module_function
|
28
10
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def say_debug(message)
|
34
|
-
if debug? || test?
|
35
|
-
prt("{{i}} #{strip(message)}", to: $stderr)
|
11
|
+
def prompt
|
12
|
+
@prompt ||= TTY::Prompt.new
|
36
13
|
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def say_abort(message)
|
40
|
-
prt("{{x}} #{message}")
|
41
|
-
exit 1
|
42
|
-
end
|
43
|
-
|
44
|
-
def debug?
|
45
|
-
return false unless defined?(RSpec)
|
46
|
-
|
47
|
-
return RSpec.configuration.debug?
|
48
|
-
end
|
49
|
-
|
50
|
-
def test?
|
51
|
-
Rfix.test?
|
52
|
-
end
|
53
|
-
|
54
|
-
def say_exit(message)
|
55
|
-
prt("{{v}} #{message}")
|
56
|
-
exit 0
|
57
|
-
end
|
58
|
-
|
59
|
-
def say_plain(message)
|
60
|
-
prt(message)
|
61
|
-
end
|
62
14
|
|
63
|
-
|
64
|
-
|
65
|
-
box(title) { yield }
|
15
|
+
def say(message)
|
16
|
+
prompt.ok(message)
|
66
17
|
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def prt(*args)
|
70
|
-
CLI::UI.puts(*args)
|
71
|
-
end
|
72
|
-
|
73
|
-
def fmt(*args)
|
74
|
-
CLI::UI.fmt(*args)
|
75
|
-
end
|
76
|
-
|
77
|
-
alias ftm fmt
|
78
18
|
|
79
|
-
|
80
|
-
|
81
|
-
return margin(2) do
|
82
|
-
prt "{{warning:No items found}}"
|
83
|
-
end if items.empty?
|
84
|
-
|
85
|
-
items.each do |item|
|
86
|
-
if block_given?
|
87
|
-
say strip(yield item)
|
88
|
-
else
|
89
|
-
say strip(item.to_s)
|
90
|
-
end
|
91
|
-
end
|
19
|
+
def say!(message)
|
20
|
+
prompt.warn(message)
|
92
21
|
end
|
93
22
|
end
|
94
|
-
|
95
|
-
def box(title, color: :reset)
|
96
|
-
margin do
|
97
|
-
CLI::UI::Frame.open(title, color: color) do
|
98
|
-
margin(2) do
|
99
|
-
yield
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def strip(msg)
|
106
|
-
msg
|
107
|
-
# msg.gsub(current_path, "").gsub(Dir.pwd, ".").chomp
|
108
|
-
end
|
109
|
-
|
110
|
-
def current_path
|
111
|
-
File.join(Dir.pwd, "/")
|
112
|
-
end
|
113
|
-
|
114
|
-
def div(title, **args)
|
115
|
-
CLI::UI::Frame.divider(title, **args)
|
116
|
-
margin { yield }
|
117
|
-
end
|
118
|
-
|
119
|
-
def margin(n = 1)
|
120
|
-
new_line(n)
|
121
|
-
yield
|
122
|
-
new_line(n)
|
123
|
-
end
|
124
|
-
|
125
|
-
def new_line(n = 1)
|
126
|
-
say_plain("\n" * n)
|
127
|
-
end
|
128
|
-
|
129
|
-
def unless_debug
|
130
|
-
yield unless Rfix.debug?
|
131
|
-
end
|
132
23
|
end
|