mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Introduced new presenter abstraction
This commit is contained in:
parent
bede3f97a1
commit
3d3626accc
5 changed files with 102 additions and 79 deletions
|
@ -3,6 +3,7 @@ require "./ameba/ast/**"
|
||||||
require "./ameba/ext/**"
|
require "./ameba/ext/**"
|
||||||
require "./ameba/rule/**"
|
require "./ameba/rule/**"
|
||||||
require "./ameba/formatter/*"
|
require "./ameba/formatter/*"
|
||||||
|
require "./ameba/presenter/*"
|
||||||
require "./ameba/source/**"
|
require "./ameba/source/**"
|
||||||
|
|
||||||
# Ameba's entry module.
|
# Ameba's entry module.
|
||||||
|
|
|
@ -5,9 +5,6 @@ require "option_parser"
|
||||||
module Ameba::Cli
|
module Ameba::Cli
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
private ENABLED_MARK = "✓".colorize(:green)
|
|
||||||
private DISABLED_MARK = "x".colorize(:red)
|
|
||||||
|
|
||||||
def run(args = ARGV)
|
def run(args = ARGV)
|
||||||
opts = parse_args args
|
opts = parse_args args
|
||||||
location_to_explain = opts.location_to_explain
|
location_to_explain = opts.location_to_explain
|
||||||
|
@ -31,11 +28,14 @@ module Ameba::Cli
|
||||||
configure_rules(config, opts)
|
configure_rules(config, opts)
|
||||||
|
|
||||||
if opts.rules?
|
if opts.rules?
|
||||||
print_rules(config)
|
print_rules(config.rules)
|
||||||
end
|
end
|
||||||
|
|
||||||
if describe_rule = opts.describe
|
if describe_rule_name = opts.describe_rule
|
||||||
print_rule_description(describe_rule, config)
|
unless rule = config.rules.find(&.name.== describe_rule_name)
|
||||||
|
raise "Unknown rule"
|
||||||
|
end
|
||||||
|
describe_rule(rule)
|
||||||
end
|
end
|
||||||
|
|
||||||
runner = Ameba.run(config)
|
runner = Ameba.run(config)
|
||||||
|
@ -56,7 +56,7 @@ module Ameba::Cli
|
||||||
property globs : Array(String)?
|
property globs : Array(String)?
|
||||||
property only : Array(String)?
|
property only : Array(String)?
|
||||||
property except : Array(String)?
|
property except : Array(String)?
|
||||||
property describe : String?
|
property describe_rule : String?
|
||||||
property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)?
|
property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)?
|
||||||
property fail_level : Severity?
|
property fail_level : Severity?
|
||||||
property? skip_reading_config = false
|
property? skip_reading_config = false
|
||||||
|
@ -166,7 +166,7 @@ module Ameba::Cli
|
||||||
end
|
end
|
||||||
|
|
||||||
private def configure_describe_opts(rule_name, opts)
|
private def configure_describe_opts(rule_name, opts)
|
||||||
opts.describe = rule_name.presence
|
opts.describe_rule = rule_name.presence
|
||||||
opts.formatter = :silent
|
opts.formatter = :silent
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -201,80 +201,13 @@ module Ameba::Cli
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
private def print_rules(config)
|
private def describe_rule(rule)
|
||||||
rules = config.rules.to_h do |rule|
|
Presenter::RulePresenter.new.run(rule)
|
||||||
name = rule.name.split('/')
|
|
||||||
name = "%s/%s" % {
|
|
||||||
name[0...-1].join('/').colorize(:light_gray),
|
|
||||||
name.last.colorize(:white),
|
|
||||||
}
|
|
||||||
{name, rule}
|
|
||||||
end
|
|
||||||
longest_name = rules.max_of(&.first.size)
|
|
||||||
|
|
||||||
rules.group_by(&.last.group).each do |group, group_rules|
|
|
||||||
puts "— %s" % group.colorize(:light_blue).underline
|
|
||||||
puts
|
|
||||||
group_rules.each do |name, rule|
|
|
||||||
puts " %s [%s] %s %s" % {
|
|
||||||
rule.enabled? ? ENABLED_MARK : DISABLED_MARK,
|
|
||||||
rule.severity.symbol.to_s.colorize(:green),
|
|
||||||
name.ljust(longest_name),
|
|
||||||
rule.description.colorize(:dark_gray),
|
|
||||||
}
|
|
||||||
end
|
|
||||||
puts
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "Total rules: %s / %s enabled" % {
|
|
||||||
rules.size.to_s.colorize(:light_blue),
|
|
||||||
rules.count(&.last.enabled?).to_s.colorize(:light_blue),
|
|
||||||
}
|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
private def print_rule_description(rule_name, config)
|
private def print_rules(rules)
|
||||||
rule = config.rules.find(&.name.== rule_name)
|
Presenter::RuleCollectionPresenter.new.run(rules)
|
||||||
raise "Unknown rule" unless rule
|
|
||||||
|
|
||||||
puts
|
|
||||||
output_title "Rule info"
|
|
||||||
output_paragraph "%s of a %s severity [enabled: %s]" % {
|
|
||||||
rule.name.colorize(:magenta),
|
|
||||||
rule.severity.to_s.colorize(rule.severity.color),
|
|
||||||
rule.enabled? ? ENABLED_MARK : DISABLED_MARK,
|
|
||||||
}
|
|
||||||
if rule_description = colorize_code_fences(rule.description)
|
|
||||||
output_paragraph rule_description
|
|
||||||
end
|
|
||||||
|
|
||||||
if rule_doc = colorize_code_fences(rule.class.parsed_doc)
|
|
||||||
output_title "Detailed description"
|
|
||||||
output_paragraph rule_doc
|
|
||||||
end
|
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
private def output_title(title)
|
|
||||||
print "### %s\n\n" % title.upcase.colorize(:yellow)
|
|
||||||
end
|
|
||||||
|
|
||||||
private def output_paragraph(paragraph : String)
|
|
||||||
output_paragraph(paragraph.lines)
|
|
||||||
end
|
|
||||||
|
|
||||||
private def output_paragraph(paragraph : Array)
|
|
||||||
paragraph.each do |line|
|
|
||||||
puts " #{line}"
|
|
||||||
end
|
|
||||||
puts
|
|
||||||
end
|
|
||||||
|
|
||||||
private def colorize_code_fences(string)
|
|
||||||
return unless string
|
|
||||||
string
|
|
||||||
.gsub(/```(.+?)```/m, &.colorize(:dark_gray))
|
|
||||||
.gsub(/`(?!`)(.+?)`/, &.colorize(:dark_gray))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
12
src/ameba/presenter/base_presenter.cr
Normal file
12
src/ameba/presenter/base_presenter.cr
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
module Ameba::Presenter
|
||||||
|
private ENABLED_MARK = "✓".colorize(:green)
|
||||||
|
private DISABLED_MARK = "x".colorize(:red)
|
||||||
|
|
||||||
|
class BasePresenter
|
||||||
|
# TODO: allow other IOs
|
||||||
|
getter output : IO::FileDescriptor | IO::Memory
|
||||||
|
|
||||||
|
def initialize(@output = STDOUT)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
34
src/ameba/presenter/rule_collection_presenter.cr
Normal file
34
src/ameba/presenter/rule_collection_presenter.cr
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
module Ameba::Presenter
|
||||||
|
class RuleCollectionPresenter < BasePresenter
|
||||||
|
def run(rules)
|
||||||
|
rules = rules.to_h do |rule|
|
||||||
|
name = rule.name.split('/')
|
||||||
|
name = "%s/%s" % {
|
||||||
|
name[0...-1].join('/').colorize(:light_gray),
|
||||||
|
name.last.colorize(:white),
|
||||||
|
}
|
||||||
|
{name, rule}
|
||||||
|
end
|
||||||
|
longest_name = rules.max_of(&.first.size)
|
||||||
|
|
||||||
|
rules.group_by(&.last.group).each do |group, group_rules|
|
||||||
|
output.puts "— %s" % group.colorize(:light_blue).underline
|
||||||
|
output.puts
|
||||||
|
group_rules.each do |name, rule|
|
||||||
|
output.puts " %s [%s] %s %s" % {
|
||||||
|
rule.enabled? ? ENABLED_MARK : DISABLED_MARK,
|
||||||
|
rule.severity.symbol.to_s.colorize(:green),
|
||||||
|
name.ljust(longest_name),
|
||||||
|
rule.description.colorize(:dark_gray),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
output.puts
|
||||||
|
end
|
||||||
|
|
||||||
|
output.puts "Total rules: %s / %s enabled" % {
|
||||||
|
rules.size.to_s.colorize(:light_blue),
|
||||||
|
rules.count(&.last.enabled?).to_s.colorize(:light_blue),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
43
src/ameba/presenter/rule_presenter.cr
Normal file
43
src/ameba/presenter/rule_presenter.cr
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
module Ameba::Presenter
|
||||||
|
class RulePresenter < BasePresenter
|
||||||
|
def run(rule)
|
||||||
|
output.puts
|
||||||
|
output_title "Rule info"
|
||||||
|
output_paragraph "%s of a %s severity [enabled: %s]" % {
|
||||||
|
rule.name.colorize(:magenta),
|
||||||
|
rule.severity.to_s.colorize(rule.severity.color),
|
||||||
|
rule.enabled? ? ENABLED_MARK : DISABLED_MARK,
|
||||||
|
}
|
||||||
|
if rule_description = colorize_code_fences(rule.description)
|
||||||
|
output_paragraph rule_description
|
||||||
|
end
|
||||||
|
|
||||||
|
if rule_doc = colorize_code_fences(rule.class.parsed_doc)
|
||||||
|
output_title "Detailed description"
|
||||||
|
output_paragraph rule_doc
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def output_title(title)
|
||||||
|
output.print "### %s\n\n" % title.upcase.colorize(:yellow)
|
||||||
|
end
|
||||||
|
|
||||||
|
private def output_paragraph(paragraph : String)
|
||||||
|
output_paragraph(paragraph.lines)
|
||||||
|
end
|
||||||
|
|
||||||
|
private def output_paragraph(paragraph : Array)
|
||||||
|
paragraph.each do |line|
|
||||||
|
output.puts " #{line}"
|
||||||
|
end
|
||||||
|
output.puts
|
||||||
|
end
|
||||||
|
|
||||||
|
private def colorize_code_fences(string)
|
||||||
|
return unless string
|
||||||
|
string
|
||||||
|
.gsub(/```(.+?)```/m, &.colorize(:dark_gray))
|
||||||
|
.gsub(/`(?!`)(.+?)`/, &.colorize(:dark_gray))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue