Module: Megam::Stuff
Instance Method Summary collapse
- #git(args) ⇒ Object
- #has_git? ⇒ Boolean
- #launchy(message, url) ⇒ Object
- #spinner(ticks) ⇒ Object
-
#styled_hash(hash) ⇒ Object
left justified keyed hash with newlines.
- #time_ago(since) ⇒ Object
Instance Method Details
#git(args) ⇒ Object
24 25 26 27 28 |
# File 'lib/megam/core/stuff.rb', line 24 def git(args) return "" unless has_git? flattened_args = [args].flatten.compact.join(" ") %x{ git #{flattened_args} 2>&1 }.strip end |
#has_git? ⇒ Boolean
19 20 21 22 |
# File 'lib/megam/core/stuff.rb', line 19 def has_git? %x{ git --version } $?.success? end |
#launchy(message, url) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/megam/core/stuff.rb', line 52 def launchy(, url) action() do require("launchy") launchy = Launchy.open(url) if launchy.respond_to?(:join) launchy.join end end end |
#spinner(ticks) ⇒ Object
48 49 50 |
# File 'lib/megam/core/stuff.rb', line 48 def spinner(ticks) %w(/ - \\ |)[ticks % 4] end |
#styled_hash(hash) ⇒ Object
left justified keyed hash with newlines.
64 65 66 |
# File 'lib/megam/core/stuff.rb', line 64 def styled_hash(hash) hash.map{|k,v| "#{k.ljust(15)}=#{v}"}.join("\n") end |
#time_ago(since) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/megam/core/stuff.rb', line 30 def time_ago(since) if since.is_a?(String) since = Time.parse(since) end elapsed = Time.now - since = since.strftime("%Y/%m/%d %H:%M:%S") if elapsed <= 60 << " (~ #{elapsed.floor}s ago)" elsif elapsed <= (60 * 60) << " (~ #{(elapsed / 60).floor}m ago)" elsif elapsed <= (60 * 60 * 25) << " (~ #{(elapsed / 60 / 60).floor}h ago)" end end |