-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced new presenter abstraction
- Loading branch information
Showing
5 changed files
with
102 additions
and
79 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,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 |
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,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| | ||
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), | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Ameba::Presenter | ||
class RulePresenter < BasePresenter | ||
def run(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 | ||
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 |