Last active
December 14, 2015 00:29
-
-
Save sneakin/4999351 to your computer and use it in GitHub Desktop.
Display loadavg and memory usage as a textual bar graph suitable for Tmux's statusbar: █▄▃ ▁█
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
module LinuxProcInfo | |
def self.cpucount | |
File.readlines("/proc/cpuinfo").find_all { |s| s.start_with?('processor') }.count | |
end | |
def self.loadavg | |
File.read('/proc/loadavg').split[0, 3].collect(&:to_f) | |
end | |
def self.freemem | |
fields = File.readlines('/proc/meminfo').inject(Hash.new) { |hash, line| | |
field, value, unit = line.split(/:?\s+/) | |
hash[field] = value | |
hash | |
} | |
[ [ fields['MemTotal'].to_i, fields['MemFree'].to_i ], [ fields['SwapTotal'].to_i, fields['SwapFree'].to_i ] ] | |
end | |
end | |
class Numeric | |
def clamp(min, max) | |
if self > max | |
max | |
elsif self < min | |
min | |
else | |
self | |
end | |
end | |
end | |
class TerminalGraph | |
def initialize | |
@data = [] | |
end | |
def <<(datum) | |
datum = datum.to_f unless datum == nil | |
@data << datum | |
self | |
end | |
def delete_at(index) | |
@data.delete_at(index) | |
self | |
end | |
def render(min = nil, max = nil) | |
max = @data.max { |d| d || 0.0 } unless max | |
min = @data.min { |d| d || 0.0 } unless min | |
height = max - min | |
@data.inject("") { |s, datum| | |
s + (datum.nil?? ' ' : single_bar((datum - min) / height)) | |
} | |
end | |
UNICODE_ZERO_BAR = 0x2581 | |
def single_bar(percentage) | |
n = (7 * percentage.clamp(0.0, 1.0)).round | |
(UNICODE_ZERO_BAR + n).chr(Encoding::UTF_8) | |
end | |
end | |
if __FILE__ == $0 | |
graph = TerminalGraph.new | |
cpucount = LinuxProcInfo.cpucount.to_f | |
mem, swap = LinuxProcInfo.freemem | |
LinuxProcInfo.loadavg.each { |n| graph << (n / cpucount) } | |
graph << nil | |
graph << (mem[0] - mem[1]) / mem[0].to_f | |
graph << (swap[0] - swap[1]) / swap[0].to_f | |
$stdout.puts(graph.render(0.0, 1.0)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment