Class: ArTTY::SystemInfo
- Inherits:
-
Object
- Object
- ArTTY::SystemInfo
- Defined in:
- lib/arTTY/system_info.rb
Instance Method Summary collapse
- #cpu ⇒ Object
- #fields(fields) ⇒ Object
- #fs_usage(fs = "/") ⇒ Object
- #height ⇒ Object
- #hostname ⇒ Object
- #info ⇒ Object
-
#initialize(fields = nil) ⇒ SystemInfo
constructor
A new instance of SystemInfo.
- #ipv4 ⇒ Object
- #ipv6 ⇒ Object
- #kernel ⇒ Object
- #os ⇒ Object
- #ram ⇒ Object
- #refresh ⇒ Object
- #shell ⇒ Object
- #tty ⇒ Object
- #uptime ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(fields = nil) ⇒ SystemInfo
Returns a new instance of SystemInfo.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/arTTY/system_info.rb', line 62 def initialize(fields = nil) @info = Hash.new @fields = fields if (fields.nil? || fields.empty?) @fields = [ "hostname", "os", "kernel", "uptime", "ip", "shell", "tty", "cpu", "ram", "fs", "colors" ] end refresh end |
Instance Method Details
#cpu ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/arTTY/system_info.rb', line 8 def cpu if (Pathname.new("/proc/cpuinfo").exist?) File.read("/proc/cpuinfo").each_line do |line| line.match(/name\s+:\s+(.+)/) do |m| out = m[1].gsub(/\((R|TM)\)| (@|CPU)/, "") return out.gsub(/\s+/, " ").strip end end end return "" end |
#fields(fields) ⇒ Object
20 21 22 23 |
# File 'lib/arTTY/system_info.rb', line 20 def fields(fields) @fields = fields refresh end |
#fs_usage(fs = "/") ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/arTTY/system_info.rb', line 25 def fs_usage(fs = "/") return "" if (ScoobyDoo.where_are_you("df").nil?) df = %x(df -h #{fs} | tail -n 1) df.match(/^\S+\s+(\S+)\s+(\S+)\s+\S+\s+(\S+)/) do |m| return "#{m[2]} / #{m[1]} (#{m[3]})".strip end return "" end |
#height ⇒ Object
36 37 38 |
# File 'lib/arTTY/system_info.rb', line 36 def height return @info.keys.length end |
#hostname ⇒ Object
40 41 42 43 |
# File 'lib/arTTY/system_info.rb', line 40 def hostname return "" if (ScoobyDoo.where_are_you("hostname").nil?) return %x(hostname).split(".")[0].strip end |
#info ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/arTTY/system_info.rb', line 45 def info info = Array.new fillto = @info.keys.map(&:length).max @info.each do |k, v| if (k.empty?) info.push("") info.push(v) else lfill = " " * (fillto - k.length) info.push("#{lfill}#{k.blue}: #{v.light_blue}") end end return info end |
#ipv4 ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/arTTY/system_info.rb', line 83 def ipv4 return "" if (ScoobyDoo.where_are_you("ip").nil?) %x(ip r).match(/^default.+dev\s+(\S+)/) do |dev| %x(ip -o a s #{dev[1]}).match(/\s+inet\s+(\S+)/) do |ip| return ip[1].strip end end return "" end |
#ipv6 ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/arTTY/system_info.rb', line 93 def ipv6 return "" if (ScoobyDoo.where_are_you("ip").nil?) %x(ip r).match(/^default.+dev\s+(\S+)/) do |dev| %x(ip -o a s #{dev[1]}).match( /\s+inet6\s+((?!fe[89ab])\S+)/i ) do |ip| return ip[1].strip end end return "" end |
#kernel ⇒ Object
105 106 107 108 |
# File 'lib/arTTY/system_info.rb', line 105 def kernel return "" if (ScoobyDoo.where_are_you("uname").nil?) return %x(uname -r).strip end |
#os ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/arTTY/system_info.rb', line 110 def os return "" if (ScoobyDoo.where_are_you("uname").nil?) os = Pathname.new("/etc/os-release"). if (os.exist?) File.read(os).each_line do |line| line.match(/^PRETTY_NAME="(.+)"/) do |m| return "#{m[1].strip} #{%x(uname -m).strip}" end end end return "#{%x(uname -s).strip} #{%x(uname -m).strip}" end |
#ram ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/arTTY/system_info.rb', line 125 def ram return "" if (ScoobyDoo.where_are_you("free").nil?) %x(free -m).match(/Mem:\s+(\d+)\s+(\d+)/) do |m| return "#{m[2]} MB / #{m[1]} MB".strip end return "" end |
#refresh ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/arTTY/system_info.rb', line 135 def refresh @info.clear @fields.each do |field| case field when /^colors$/i @info[""] = [ "▄▄▄".light_black.on_black, "▄▄▄".light_red.on_red, "▄▄▄".light_green.on_green, "▄▄▄".light_yellow.on_yellow, "▄▄▄".light_blue.on_blue, "▄▄▄".light_magenta.on_magenta, "▄▄▄".light_cyan.on_cyan, "▄▄▄".light_white.on_white ].join when /^cpu$/i @info["CPU"] = cpu when /^fs$/i rootfs = fs_usage homefs = fs_usage(ENV["HOME"]) @info["RootFS"] = rootfs @info["HomeFS"] = homefs if (homefs != rootfs) when /^host(name)?$/i @info["Host"] = hostname when /^ip$/i @info["IPv4"] = ipv4 @info["IPv6"] = ipv6 when /^ipv4$/i @info["IPv4"] = ipv4 when /^ipv6$/i @info["IPv6"] = ipv6 when /^kernel$/i @info["Kernel"] = kernel when /^os$/i @info["OS"] = os when /^ram$/i @info["RAM"] = ram when /^shell$/i @info["Shell"] = shell when /^tty$/i @info["TTY"] = tty when /^uptime$/i @info["Uptime"] = uptime end end @info.delete_if do |k, v| v.empty? end end |
#shell ⇒ Object
185 186 187 188 |
# File 'lib/arTTY/system_info.rb', line 185 def shell return "" if (ENV["SHELL"].nil?) return ENV["SHELL"].strip end |
#tty ⇒ Object
190 191 192 193 |
# File 'lib/arTTY/system_info.rb', line 190 def tty return "" if (ScoobyDoo.where_are_you("tty").nil?) return %x(tty 2>/dev/null).strip end |
#uptime ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/arTTY/system_info.rb', line 195 def uptime return "" if (ScoobyDoo.where_are_you("uptime").nil?) up = %x(uptime).gsub(/^.+up\s+|,\s+\d+\s+user.+$/, "").strip up.gsub!(/(days?)\s+/, "\\1, ") up.gsub!(/0?(\d+):0?(\d+)/, "\\1 hour, \\2 min") up.gsub!(/(0 hour, |, 0 min)/, "") up.gsub!(/((\d\d+|[2-9]) (hour|min))/, "\\1s") return up.gsub(/\s+/, " ").strip end |
#width ⇒ Object
206 207 208 209 210 211 |
# File 'lib/arTTY/system_info.rb', line 206 def width return 0 if (@info.empty?) k = @info.keys.map(&:plain).map(&:length).max v = @info.values.map(&:plain).map(&:length).max return k + v + 2 end |