Module: SolidQueueTui::FormattingHelpers

Instance Method Summary collapse

Instance Method Details

#format_duration(seconds) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 21

def format_duration(seconds)
  return "n/a" unless seconds
  seconds = seconds.to_i
  if seconds < 1
    "<1s"
  elsif seconds < 60
    "#{seconds}s"
  elsif seconds < 3600
    "#{seconds / 60}m #{seconds % 60}s"
  elsif seconds < 86400
    "#{seconds / 3600}h #{(seconds % 3600) / 60}m"
  else
    "#{seconds / 86400}d #{(seconds % 86400) / 3600}h"
  end
end

#format_number(n) ⇒ Object



37
38
39
40
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 37

def format_number(n)
  return "0" if n.nil? || n == 0
  n.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

#format_time(time) ⇒ Object



16
17
18
19
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 16

def format_time(time)
  return "n/a" unless time
  time.strftime("%Y-%m-%d %H:%M:%S")
end

#humanize_duration(seconds) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 47

def humanize_duration(seconds)
  case seconds.abs
  when 0..59       then "#{seconds.abs}s"
  when 60..3599    then "#{seconds.abs / 60}m"
  when 3600..86399 then "#{seconds.abs / 3600}h"
  else "#{seconds.abs / 86400}d"
  end
end

#time_ago(time) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 5

def time_ago(time)
  return "n/a" unless time
  seconds = (Time.now.utc - time).to_i
  case seconds
  when 0..59       then "#{seconds}s ago"
  when 60..3599    then "#{seconds / 60}m ago"
  when 3600..86399 then "#{seconds / 3600}h ago"
  else "#{seconds / 86400}d ago"
  end
end

#time_until(time) ⇒ Object



56
57
58
59
60
61
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 56

def time_until(time)
  return "n/a" unless time
  seconds = (time - Time.now.utc).to_i
  return "now" if seconds <= 0
  "in #{humanize_duration(seconds)}"
end

#truncate(str, max) ⇒ Object



42
43
44
45
# File 'lib/solid_queue_tui/formatting_helpers.rb', line 42

def truncate(str, max)
  return "" unless str
  str.length > max ? "#{str[0...max - 3]}..." : str
end