Skip to content

Commit

Permalink
Extract pluralize + to_human helpers into Formatter::Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Dec 10, 2024
1 parent 22869d8 commit 5ad69d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
24 changes: 1 addition & 23 deletions src/ameba/formatter/dot_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 4 additions & 25 deletions src/ameba/formatter/github_actions_formatter.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions src/ameba/formatter/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ad69d3

Please sign in to comment.