Module: Ergane::Formatter
- Defined in:
- lib/ergane/formatter.rb
Overview
Small, generic output helpers for interactive CLIs.
Class Method Summary collapse
-
.confirm?(prompt) ⇒ Boolean
Prompt on stderr and return true only for an affirmative answer.
-
.time_ago(time) ⇒ Object
Humanize the distance from
timeto now: “just now”, “5m ago”, “3h ago”, “2d ago”.
Class Method Details
.confirm?(prompt) ⇒ Boolean
Prompt on stderr and return true only for an affirmative answer. Returns false when stdin isn’t a TTY (non-interactive / piped input).
9 10 11 12 13 14 15 |
# File 'lib/ergane/formatter.rb', line 9 def confirm?(prompt) $stderr.print "#{prompt} [y/N] " return false unless $stdin.tty? answer = $stdin.gets&.strip answer&.match?(/\Ay(es)?\z/i) end |
.time_ago(time) ⇒ Object
Humanize the distance from time to now: “just now”, “5m ago”, “3h ago”, “2d ago”.
19 20 21 22 23 24 25 26 |
# File 'lib/ergane/formatter.rb', line 19 def time_ago(time) seconds = (Time.now - time).to_i return 'just now' if seconds < 60 return "#{seconds / 60}m ago" if seconds < 3600 return "#{seconds / 3600}h ago" if seconds < 86400 "#{seconds / 86400}d ago" end |