diff --git a/src/ameba/formatter/dot_formatter.cr b/src/ameba/formatter/dot_formatter.cr index e038f0b98..38abb0183 100644 --- a/src/ameba/formatter/dot_formatter.cr +++ b/src/ameba/formatter/dot_formatter.cr @@ -77,34 +77,12 @@ module Ameba::Formatter "Finished in #{to_human(finished - started)}".colorize(:default) end - private def to_human(span : Time::Span) - total_milliseconds = span.total_milliseconds - if total_milliseconds < 1 - return "#{(span.total_milliseconds * 1_000).round.to_i} microseconds" - end - - total_seconds = span.total_seconds - if total_seconds < 1 - return "#{span.total_milliseconds.round(2)} milliseconds" - end - - if total_seconds < 60 - return "#{total_seconds.round(2)} seconds" - end - - minutes = span.minutes - seconds = span.seconds - - "#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes" - end - private def final_message(sources, failed_sources) total = sources.size failures = failed_sources.sum(&.issues.count(&.enabled?)) color = failures == 0 ? :green : :red - s = failures != 1 ? "s" : "" - "#{total} inspected, #{failures} failure#{s}".colorize(color) + "#{total} inspected, #{failures} #{pluralize(failures, "failure")}".colorize(color) end end end diff --git a/src/ameba/formatter/github_actions_formatter.cr b/src/ameba/formatter/github_actions_formatter.cr index 92b7cc0c0..80ff5a0f0 100644 --- a/src/ameba/formatter/github_actions_formatter.cr +++ b/src/ameba/formatter/github_actions_formatter.cr @@ -1,8 +1,12 @@ +require "./util" + module Ameba::Formatter # A formatter that outputs issues in a GitHub Actions compatible format. # # See [GitHub Actions documentation](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions) for details. class GitHubActionsFormatter < BaseFormatter + include Util + @started_at : Time::Span? @mutex = Mutex.new @@ -145,31 +149,6 @@ module Ameba::Formatter location_url || line_selector end - private def pluralize(count : Int, singular : String, plural = "#{singular}s") - count == 1 ? singular : plural - end - - private def to_human(span : Time::Span) - total_milliseconds = span.total_milliseconds - if total_milliseconds < 1 - return "#{(span.total_milliseconds * 1_000).round.to_i} microseconds" - end - - total_seconds = span.total_seconds - if total_seconds < 1 - return "#{span.total_milliseconds.round(2)} milliseconds" - end - - if total_seconds < 60 - return "#{total_seconds.round(2)} seconds" - end - - minutes = span.minutes - seconds = span.seconds - - "#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes" - end - private def command_name(severity : Severity) : String case severity in .error? then "error" diff --git a/src/ameba/formatter/util.cr b/src/ameba/formatter/util.cr index b9dbe7336..99aa6986e 100644 --- a/src/ameba/formatter/util.cr +++ b/src/ameba/formatter/util.cr @@ -2,6 +2,31 @@ module Ameba::Formatter module Util extend self + def pluralize(count : Int, singular : String, plural = "#{singular}s") + count == 1 ? singular : plural + end + + def to_human(span : Time::Span) + total_milliseconds = span.total_milliseconds + if total_milliseconds < 1 + return "#{(span.total_milliseconds * 1_000).round.to_i} microseconds" + end + + total_seconds = span.total_seconds + if total_seconds < 1 + return "#{span.total_milliseconds.round(2)} milliseconds" + end + + if total_seconds < 60 + return "#{total_seconds.round(2)} seconds" + end + + minutes = span.minutes + seconds = span.seconds + + "#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes" + end + def deansify(message : String?) : String? message.try &.gsub(/\x1b[^m]*m/, "").presence end