Reimplement #pretty_duration
Also reuse this improved implementation in the GitHub rate limit errors. Closes Homebrew/homebrew#44721. Signed-off-by: Baptiste Fontaine <batifon@yahoo.fr>
This commit is contained in:
parent
2392574a77
commit
f0a370ea12
@ -49,11 +49,11 @@ class UtilTests < Homebrew::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_pretty_duration
|
def test_pretty_duration
|
||||||
assert_equal "2 seconds", pretty_duration(1)
|
assert_equal "1 second", pretty_duration(1)
|
||||||
assert_equal "2 seconds", pretty_duration(2.5)
|
assert_equal "2 seconds", pretty_duration(2.5)
|
||||||
assert_equal "42 seconds", pretty_duration(42)
|
assert_equal "42 seconds", pretty_duration(42)
|
||||||
assert_equal "4.2 minutes", pretty_duration(252)
|
assert_equal "4 minutes", pretty_duration(240)
|
||||||
assert_equal "4.2 minutes", pretty_duration(252.45)
|
assert_equal "4 minutes 12 seconds", pretty_duration(252.45)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_plural
|
def test_plural
|
||||||
|
|||||||
@ -138,9 +138,18 @@ def pretty_uninstalled(f)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pretty_duration(s)
|
def pretty_duration(s)
|
||||||
return "2 seconds" if s < 3 # avoids the plural problem ;)
|
s = s.to_i
|
||||||
return "#{s.to_i} seconds" if s < 120
|
res = ""
|
||||||
"%.1f minutes" % (s/60.0)
|
|
||||||
|
if s > 59
|
||||||
|
m = s / 60
|
||||||
|
s %= 60
|
||||||
|
res = "#{m} minute#{plural m}"
|
||||||
|
return res if s == 0
|
||||||
|
res << " "
|
||||||
|
end
|
||||||
|
|
||||||
|
res + "#{s} second#{plural s}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def plural(n, s = "s")
|
def plural(n, s = "s")
|
||||||
@ -465,11 +474,7 @@ module GitHub
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pretty_ratelimit_reset(reset)
|
def pretty_ratelimit_reset(reset)
|
||||||
if (seconds = Time.at(reset) - Time.now) > 180
|
pretty_duration(Time.at(reset) - Time.now)
|
||||||
"%d minutes %d seconds" % [seconds / 60, seconds % 60]
|
|
||||||
else
|
|
||||||
"#{seconds} seconds"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user