class Uptime

Attributes

days[R]
hours[R]
minutes[R]

Public Class Methods

new(motd) click to toggle source
Calls superclass method Component::new
# File lib/panda_motd/components/uptime.rb, line 8
def initialize(motd)
  super(motd, "uptime")
end

Public Instance Methods

process() click to toggle source

Calculates the number of days, hours, and minutes based on the current uptime value.

@see Component#process

# File lib/panda_motd/components/uptime.rb, line 16
def process
  uptime = SysInfo.new.uptime

  @days = (uptime / 24).floor
  @hours = (uptime - @days * 24).floor
  @minutes = ((uptime - @days * 24 - hours) * 60).floor
end
to_s() click to toggle source

Gets a printable uptime string with a prefix. The prefix can be configured, and defaults to “up”.

# File lib/panda_motd/components/uptime.rb, line 26
def to_s
  "#{@config["prefix"] || "up"} #{format_uptime}"
end

Private Instance Methods

format_uptime() click to toggle source

Formats the uptime values in such a way that it is easier to read. If any of the measurements are zero, that part is omitted. Words are properly pluralized.

Examples:

`3d 20h 55m` becomes `3 days, 20 hours, 55 minutes`

`3d 0h 55m` becomes `3 days, 55 minutes`

# File lib/panda_motd/components/uptime.rb, line 41
def format_uptime
  [@days, @hours, @minutes].zip(%w[day hour minute])
                           .reject { |n, _word| n.zero? }
                           .map { |n, word| "#{n} #{word}#{"s" if n != 1}" }
                           .join(", ")
end