rfix 1.4.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. checksums.yaml +4 -4
  2. data/exe/rfix +78 -34
  3. data/lib/rfix.rb +17 -28
  4. data/lib/rfix/branch.rb +3 -25
  5. data/lib/rfix/branch/base.rb +27 -0
  6. data/lib/rfix/branch/head.rb +15 -0
  7. data/lib/rfix/branch/main.rb +33 -0
  8. data/lib/rfix/branch/name.rb +21 -0
  9. data/lib/rfix/branch/reference.rb +15 -0
  10. data/lib/rfix/branch/upstream.rb +17 -0
  11. data/lib/rfix/cli/command.rb +19 -0
  12. data/lib/rfix/cli/command/base.rb +61 -0
  13. data/lib/rfix/cli/command/branch.rb +13 -0
  14. data/lib/rfix/cli/command/config.rb +22 -0
  15. data/lib/rfix/cli/command/extension.rb +25 -0
  16. data/lib/rfix/cli/command/help.rb +11 -0
  17. data/lib/rfix/cli/command/info.rb +11 -0
  18. data/lib/rfix/cli/command/lint.rb +17 -0
  19. data/lib/rfix/cli/command/local.rb +11 -0
  20. data/lib/rfix/cli/command/origin.rb +11 -0
  21. data/lib/rfix/cli/command/setup.rb +11 -0
  22. data/lib/rfix/error.rb +5 -1
  23. data/lib/rfix/extension/offense.rb +79 -0
  24. data/lib/rfix/extension/pastel.rb +11 -0
  25. data/lib/rfix/extension/string.rb +12 -0
  26. data/lib/rfix/extension/strings.rb +9 -0
  27. data/lib/rfix/file.rb +6 -41
  28. data/lib/rfix/file/base.rb +73 -0
  29. data/lib/rfix/file/deleted.rb +17 -0
  30. data/lib/rfix/file/ignored.rb +17 -0
  31. data/lib/rfix/file/tracked.rb +42 -0
  32. data/lib/rfix/file/untracked.rb +20 -0
  33. data/lib/rfix/formatter.rb +125 -86
  34. data/lib/rfix/highlighter.rb +118 -0
  35. data/lib/rfix/indicator.rb +19 -0
  36. data/lib/rfix/log.rb +12 -121
  37. data/lib/rfix/rake/gemfile.rb +111 -0
  38. data/lib/rfix/rake/paths.rb +25 -23
  39. data/lib/rfix/rake/support.rb +72 -57
  40. data/lib/rfix/repository.rb +114 -164
  41. data/lib/rfix/types.rb +52 -0
  42. data/lib/rfix/version.rb +1 -1
  43. data/rfix.gemspec +28 -38
  44. data/vendor/dry-cli/CHANGELOG.md +191 -0
  45. data/vendor/dry-cli/CODEOWNERS +1 -0
  46. data/vendor/dry-cli/CODE_OF_CONDUCT.md +13 -0
  47. data/vendor/dry-cli/CONTRIBUTING.md +29 -0
  48. data/vendor/dry-cli/Gemfile +14 -0
  49. data/vendor/dry-cli/Gemfile.devtools +18 -0
  50. data/vendor/dry-cli/LICENSE +20 -0
  51. data/vendor/dry-cli/README.md +29 -0
  52. data/vendor/dry-cli/Rakefile +13 -0
  53. data/vendor/dry-cli/bin/console +15 -0
  54. data/vendor/dry-cli/bin/setup +8 -0
  55. data/vendor/dry-cli/changelog.yml +97 -0
  56. data/vendor/dry-cli/docsite/source/arguments.html.md +57 -0
  57. data/vendor/dry-cli/docsite/source/callbacks.html.md +51 -0
  58. data/vendor/dry-cli/docsite/source/commands-with-subcommands-and-params.md +86 -0
  59. data/vendor/dry-cli/docsite/source/commands.html.md +41 -0
  60. data/vendor/dry-cli/docsite/source/index.html.md +302 -0
  61. data/vendor/dry-cli/docsite/source/options.html.md +51 -0
  62. data/vendor/dry-cli/docsite/source/subcommands.html.md +38 -0
  63. data/vendor/dry-cli/docsite/source/variadic-arguments.html.md +45 -0
  64. data/vendor/dry-cli/dry-cli.gemspec +36 -0
  65. data/vendor/dry-cli/lib/dry/cli.rb +224 -0
  66. data/vendor/dry-cli/lib/dry/cli/banner.rb +135 -0
  67. data/vendor/dry-cli/lib/dry/cli/command.rb +387 -0
  68. data/vendor/dry-cli/lib/dry/cli/command_registry.rb +253 -0
  69. data/vendor/dry-cli/lib/dry/cli/errors.rb +37 -0
  70. data/vendor/dry-cli/lib/dry/cli/inflector.rb +17 -0
  71. data/vendor/dry-cli/lib/dry/cli/inline.rb +75 -0
  72. data/vendor/dry-cli/lib/dry/cli/option.rb +131 -0
  73. data/vendor/dry-cli/lib/dry/cli/parser.rb +138 -0
  74. data/vendor/dry-cli/lib/dry/cli/program_name.rb +21 -0
  75. data/vendor/dry-cli/lib/dry/cli/registry.rb +338 -0
  76. data/vendor/dry-cli/lib/dry/cli/usage.rb +94 -0
  77. data/vendor/dry-cli/lib/dry/cli/version.rb +8 -0
  78. data/vendor/dry-cli/project.yml +13 -0
  79. data/vendor/dry-cli/spec/integration/commands_spec.rb +14 -0
  80. data/vendor/dry-cli/spec/integration/inherited_commands_spec.rb +24 -0
  81. data/vendor/dry-cli/spec/integration/inline_spec.rb +43 -0
  82. data/vendor/dry-cli/spec/integration/processes_errors_spec.rb +29 -0
  83. data/vendor/dry-cli/spec/integration/rendering_spec.rb +31 -0
  84. data/vendor/dry-cli/spec/integration/single_command_spec.rb +81 -0
  85. data/vendor/dry-cli/spec/integration/subcommands_spec.rb +60 -0
  86. data/vendor/dry-cli/spec/integration/third_party_gems_spec.rb +18 -0
  87. data/vendor/dry-cli/spec/spec_helper.rb +15 -0
  88. data/vendor/dry-cli/spec/support/coverage.rb +15 -0
  89. data/vendor/dry-cli/spec/support/files.rb +13 -0
  90. data/vendor/dry-cli/spec/support/fixtures/based +65 -0
  91. data/vendor/dry-cli/spec/support/fixtures/baz +9 -0
  92. data/vendor/dry-cli/spec/support/fixtures/baz_command.rb +19 -0
  93. data/vendor/dry-cli/spec/support/fixtures/foo +588 -0
  94. data/vendor/dry-cli/spec/support/fixtures/infinites +31 -0
  95. data/vendor/dry-cli/spec/support/fixtures/inline +20 -0
  96. data/vendor/dry-cli/spec/support/fixtures/registry.rb +15 -0
  97. data/vendor/dry-cli/spec/support/fixtures/shared_commands.rb +596 -0
  98. data/vendor/dry-cli/spec/support/fixtures/with_block.rb +86 -0
  99. data/vendor/dry-cli/spec/support/fixtures/with_registry.rb +90 -0
  100. data/vendor/dry-cli/spec/support/fixtures/with_zero_arity_block.rb +87 -0
  101. data/vendor/dry-cli/spec/support/helpers.rb +37 -0
  102. data/vendor/dry-cli/spec/support/path.rb +24 -0
  103. data/vendor/dry-cli/spec/support/rspec.rb +26 -0
  104. data/vendor/dry-cli/spec/support/rspec_options.rb +16 -0
  105. data/vendor/dry-cli/spec/support/shared_examples/commands.rb +300 -0
  106. data/vendor/dry-cli/spec/support/shared_examples/inherited_commands.rb +197 -0
  107. data/vendor/dry-cli/spec/support/shared_examples/rendering.rb +181 -0
  108. data/vendor/dry-cli/spec/support/shared_examples/subcommands.rb +226 -0
  109. data/vendor/dry-cli/spec/support/shared_examples/third_party_gems.rb +49 -0
  110. data/vendor/dry-cli/spec/support/warnings.rb +10 -0
  111. data/vendor/dry-cli/spec/unit/dry/cli/cli_spec.rb +123 -0
  112. data/vendor/dry-cli/spec/unit/dry/cli/inflector_spec.rb +26 -0
  113. data/vendor/dry-cli/spec/unit/dry/cli/registry_spec.rb +78 -0
  114. data/vendor/dry-cli/spec/unit/dry/cli/version_spec.rb +7 -0
  115. data/vendor/strings-ansi/CHANGELOG.md +24 -0
  116. data/vendor/strings-ansi/CODE_OF_CONDUCT.md +74 -0
  117. data/vendor/strings-ansi/Gemfile +11 -0
  118. data/{LICENSE.txt → vendor/strings-ansi/LICENSE.txt} +1 -1
  119. data/vendor/strings-ansi/README.md +155 -0
  120. data/vendor/strings-ansi/Rakefile +8 -0
  121. data/vendor/strings-ansi/appveyor.yml +32 -0
  122. data/vendor/strings-ansi/bin/console +14 -0
  123. data/vendor/strings-ansi/bin/setup +8 -0
  124. data/vendor/strings-ansi/lib/strings-ansi.rb +1 -0
  125. data/vendor/strings-ansi/lib/strings/ansi.rb +84 -0
  126. data/vendor/strings-ansi/lib/strings/ansi/extensions.rb +23 -0
  127. data/vendor/strings-ansi/lib/strings/ansi/version.rb +7 -0
  128. data/vendor/strings-ansi/spec/fixtures/ansi_codes.yaml +194 -0
  129. data/vendor/strings-ansi/spec/spec_helper.rb +51 -0
  130. data/vendor/strings-ansi/spec/unit/ansi_spec.rb +15 -0
  131. data/vendor/strings-ansi/spec/unit/extensions_spec.rb +19 -0
  132. data/vendor/strings-ansi/spec/unit/only_ansi_spec.rb +36 -0
  133. data/vendor/strings-ansi/spec/unit/sanitize_spec.rb +53 -0
  134. data/vendor/strings-ansi/strings-ansi.gemspec +34 -0
  135. data/vendor/strings-ansi/tasks/console.rake +11 -0
  136. data/vendor/strings-ansi/tasks/coverage.rake +11 -0
  137. data/vendor/strings-ansi/tasks/spec.rake +29 -0
  138. metadata +274 -188
  139. data/.github/workflows/main.yml +0 -26
  140. data/.gitignore +0 -43
  141. data/.rspec +0 -2
  142. data/.rubocop.yml +0 -87
  143. data/.travis.yml +0 -35
  144. data/Gemfile +0 -2
  145. data/Gemfile.base +0 -14
  146. data/Gemfile.base.lock +0 -172
  147. data/Gemfile.lock +0 -188
  148. data/Guardfile +0 -16
  149. data/Makefile +0 -12
  150. data/README.md +0 -85
  151. data/Rakefile +0 -31
  152. data/bin/bundle +0 -114
  153. data/bin/console +0 -29
  154. data/bin/guard +0 -29
  155. data/bin/rake +0 -29
  156. data/bin/rfix +0 -29
  157. data/bin/rspec +0 -29
  158. data/bin/setup +0 -29
  159. data/ci/Gemfile.rubocop-0.80 +0 -2
  160. data/ci/Gemfile.rubocop-0.80.lock +0 -170
  161. data/ci/Gemfile.rubocop-0.81 +0 -2
  162. data/ci/Gemfile.rubocop-0.81.lock +0 -170
  163. data/ci/Gemfile.rubocop-0.82 +0 -2
  164. data/ci/Gemfile.rubocop-0.82.lock +0 -170
  165. data/ci/Gemfile.rubocop-0.83 +0 -2
  166. data/ci/Gemfile.rubocop-0.83.lock +0 -168
  167. data/ci/Gemfile.rubocop-0.84 +0 -2
  168. data/ci/Gemfile.rubocop-0.84.lock +0 -171
  169. data/ci/Gemfile.rubocop-0.85 +0 -2
  170. data/ci/Gemfile.rubocop-0.85.1 +0 -2
  171. data/ci/Gemfile.rubocop-0.85.1.lock +0 -173
  172. data/ci/Gemfile.rubocop-0.85.lock +0 -173
  173. data/lib/rfix/box.rb +0 -112
  174. data/lib/rfix/branches/base.rb +0 -15
  175. data/lib/rfix/branches/head.rb +0 -13
  176. data/lib/rfix/branches/main.rb +0 -28
  177. data/lib/rfix/branches/name.rb +0 -23
  178. data/lib/rfix/branches/reference.rb +0 -21
  179. data/lib/rfix/branches/upstream.rb +0 -13
  180. data/lib/rfix/cmd.rb +0 -39
  181. data/lib/rfix/commands/branch.rb +0 -15
  182. data/lib/rfix/commands/extensions/options.rb +0 -8
  183. data/lib/rfix/commands/help.rb +0 -7
  184. data/lib/rfix/commands/helper/args.rb +0 -141
  185. data/lib/rfix/commands/helper/help.rb +0 -6
  186. data/lib/rfix/commands/helper/loader.rb +0 -6
  187. data/lib/rfix/commands/helper/option.rb +0 -0
  188. data/lib/rfix/commands/helper/params.rb +0 -0
  189. data/lib/rfix/commands/helper/rubocop.rb +0 -17
  190. data/lib/rfix/commands/info.rb +0 -30
  191. data/lib/rfix/commands/lint.rb +0 -22
  192. data/lib/rfix/commands/local.rb +0 -12
  193. data/lib/rfix/commands/origin.rb +0 -19
  194. data/lib/rfix/commands/setup.rb +0 -29
  195. data/lib/rfix/commands/welcome.rb +0 -24
  196. data/lib/rfix/deleted.rb +0 -13
  197. data/lib/rfix/extensions/extensions.rb +0 -18
  198. data/lib/rfix/extensions/offense.rb +0 -78
  199. data/lib/rfix/extensions/string.rb +0 -8
  200. data/lib/rfix/file_cache.rb +0 -59
  201. data/lib/rfix/git_helper.rb +0 -59
  202. data/lib/rfix/indentation.rb +0 -39
  203. data/lib/rfix/loader/bundler.rb +0 -37
  204. data/lib/rfix/loader/env.rb +0 -33
  205. data/lib/rfix/loader/spec.rb +0 -41
  206. data/lib/rfix/no_file.rb +0 -13
  207. data/lib/rfix/rfix.rb +0 -34
  208. data/lib/rfix/tracked.rb +0 -72
  209. data/lib/rfix/tracked_file.rb +0 -16
  210. data/lib/rfix/untracked.rb +0 -13
  211. data/resources/ps.png +0 -0
  212. data/tasks/bump.rake +0 -11
  213. data/tasks/bundle.rake +0 -17
  214. data/tasks/complex.rake +0 -54
  215. data/tasks/execute.rake +0 -38
  216. data/tasks/libgit2.rake +0 -33
  217. data/tasks/simple.rake +0 -62
  218. data/tasks/travis.rake +0 -74
  219. 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
@@ -1,128 +1,167 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rubocop"
4
- require "rouge"
5
- require "rainbow"
6
- require "shellwords"
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
- include Rfix::Log
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
- theme = Rouge::Themes::Gruvbox.new
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
- def truncate(path)
24
- path.sub(::File.join(Dir.getwd, "/"), "")
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
- def render_files(files)
28
- return unless Rfix.test?
55
+ # @file [File]
56
+ # @offenses [Array<Offence>]
57
+ def file_finished(*, offenses)
58
+ @reported_offenses += offenses
29
59
 
30
- files.each do |file|
31
- offenses = @files.fetch(file)
32
- corrected = offenses.select(&:corrected?)
60
+ # @indicator.stop if offenses?
33
61
 
34
- if offenses.empty?
35
- say truncate(file)
36
- elsif offenses.count == corrected.count
37
- say truncate(file)
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
- def finished(files)
45
- render_files(files)
72
+ private
46
73
 
47
- files.each do |file|
48
- render_file(file, @files.fetch(file))
74
+ using(Module.new do
75
+ refine String do
76
+ def surround(value)
77
+ value + self + value
78
+ end
49
79
  end
50
-
51
- offenses = @files.values.flatten
52
- corrected = offenses.select(&:corrected?)
53
- out("\n") unless @total.zero?
54
- report_summary(files.size, offenses.count, corrected.count)
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 render_file(file, offenses)
58
- return if offenses.empty?
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 mark
69
- CLI::UI::ANSI::ESC + "]1337;SetMark" + "\x07"
98
+ def arity
99
+ method(:report_summary).super_method.arity
70
100
  end
71
101
 
72
- def file_finished(file, offenses)
73
- out("\n") if @current == 0.0
74
- @current += 1.0
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 out(msg, format: true)
82
- CLI::UI.puts(msg, to: output, format: format)
107
+ def report(msg, format: true)
108
+ msg
83
109
  end
84
110
 
85
- def fmt(msg)
86
- CLI::UI.fmt(msg, enable_color: true)
111
+ def newline(amount = 1)
112
+ report("\n" * amount)
87
113
  end
88
114
 
89
- def dim(value)
90
- Rainbow(value).lightgray
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 highlighted_source_line(offense)
94
- source_before_highlight(offense) +
95
- hightlight_source_tag(offense) +
96
- source_after_highlight(offense)
97
- end
147
+ def corrected
148
+ reported_offenses.select(&:corrected?)
149
+ end
98
150
 
99
- def hightlight_source_tag(offense)
100
- offense.highlighted_area.source
101
- end
151
+ def correctable
152
+ reported_offenses.select(&:correctable?)
153
+ end
102
154
 
103
- def source_before_highlight(offense)
104
- source_line = offense.location.source_line
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 source_after_highlight(offense)
109
- source_line = offense.location.source_line
110
- source_line[offense.highlighted_area.end_pos..-1]
159
+ def offenses?
160
+ reported_offenses.any?
111
161
  end
112
162
 
113
- def report_line(_file, offense, _location, highlighted_area)
114
- extra = " "
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
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rfix
4
+ class Indicator
5
+ include Dry::Core::Constants
6
+
7
+ def initialize
8
+ @spinners = []
9
+ end
10
+
11
+ def start(title)
12
+
13
+ end
14
+
15
+ def stop
16
+
17
+ end
18
+ end
19
+ end
data/lib/rfix/log.rb CHANGED
@@ -1,132 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rfix"
4
- require "cli/ui"
3
+ require "tty/box"
4
+ require "tty/prompt"
5
+ require "tty/screen"
5
6
 
6
- module Rfix::Log
7
- extend self
8
- def say(message)
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
- def say_test(message)
30
- prt("{{i}} #{strip(message)}")
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
- def debug_box(title)
64
- unless_debug do
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
- def log_items(items, title:)
80
- box("#{title} (#{items.count})") do
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