Module: OsCtl::Utils::Humanize

Included in:
Cli::Command, Cli::Ps::Columns, Cli::Top::Tui::Main, Cli::Tree
Defined in:
lib/osctl/utils/humanize.rb

Instance Method Summary collapse

Instance Method Details

#break_interval(interval) ⇒ Object (protected)



71
72
73
74
75
76
77
# File 'lib/osctl/utils/humanize.rb', line 71

def break_interval(interval)
  d = interval / 86400
  h = interval / 3600 % 24
  m = interval / 60 % 60
  s = interval % 60
  [d, h, m, s]
end

#format_long_duration(interval) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/osctl/utils/humanize.rb', line 22

def format_long_duration(interval)
  d, h, m, s = break_interval(interval)

  if d > 0
    "%d days, %02d:%02d:%02d" % [d, h, m, s]
  else
    "%02d:%02d:%02d" % [h, m, s]
  end
end

#format_percent(v) ⇒ Object



49
50
51
# File 'lib/osctl/utils/humanize.rb', line 49

def format_percent(v)
  v.round(1)
end

#format_short_duration(interval) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/osctl/utils/humanize.rb', line 32

def format_short_duration(interval)
  d, h, m, s = break_interval(interval)

  if d == 0 && h == 0 && m == 0
    "#{s}s"

  elsif d == 0 && h == 0
    '%02d:%02d' % [m, s]

  elsif d == 0
    '%02d:%02d:%02d' % [h, m, s]

  else
    '%dd, %02d:%02d:%02d' % [d, h, m, s]
  end
end

#humanize_data(v) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/osctl/utils/humanize.rb', line 3

def humanize_data(v)
  bits = 39
  units = %i(T G M K)

  units.each do |u|
    threshold = 2 << bits

    return "#{(v / threshold.to_f).round(1)}#{u}" if v >= threshold

    bits -= 10
  end

  v.round(2).to_s
end

#humanize_time_ns(v) ⇒ Object



18
19
20
# File 'lib/osctl/utils/humanize.rb', line 18

def humanize_time_ns(v)
  format_short_duration(v / 1_000_000_000)
end

#parse_data(v) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/osctl/utils/humanize.rb', line 53

def parse_data(v)
  units = %w(k m g t)

  if /^\d+$/ =~ v
    v.to_i

  elsif /^(\d+)(#{units.join('|')})$/i =~ v
    n = $1.to_i
    i = units.index($2.downcase)

    n * (2 << (9 + (10*i)))

  else
    v
  end
end