Skip to content

Instantly share code, notes, and snippets.

@penguin2716
Created August 4, 2017 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penguin2716/7ead6dd87660af77d827f49cad40e33b to your computer and use it in GitHub Desktop.
Save penguin2716/7ead6dd87660af77d827f49cad40e33b to your computer and use it in GitHub Desktop.
ruby script to check battery status
#!/usr/bin/env ruby
# coding: utf-8
require 'highline'
def ac_status
print "AC: "
if open("/sys/class/power_supply/AC/online", 'r').read.to_i == 1
puts "Online"
else
puts "Offline"
end
end
def battery_info(id, linewidth = HighLine::SystemExtensions.terminal_size.first)
barwidth = linewidth - 28 - 1
info = {}
open("/sys/class/power_supply/BAT#{id}/uevent", "r").each_line do |line|
key, value = line.split("=")
info[key.sub(/^POWER_SUPPLY_/, "")] = value.chomp
end
puts "#{info["NAME"]}: #{info["ENERGY_FULL_DESIGN"].to_i / 1000000.0}Wh #{info["TECHNOLOGY"]} battery (#{info["MODEL_NAME"]} by #{info["MANUFACTURER"]})"
ratio_design = info["ENERGY_FULL"].to_f / info["ENERGY_FULL_DESIGN"].to_f
print " design ["
print "#" * (barwidth * ratio_design).to_i
print " " * (barwidth - (barwidth * ratio_design).to_i)
printf("] %5.1fWh (%5.1f%%)\n", info["ENERGY_FULL"].to_f / 1000000.0, ratio_design * 100.0)
ratio_charge = info["ENERGY_NOW"].to_f / info["ENERGY_FULL"].to_f
print " charge ["
print "#" * (barwidth * ratio_charge * ratio_design).to_i
print "." * (barwidth - (barwidth * ratio_charge * ratio_design).to_i - (barwidth - (barwidth * ratio_design).to_i))
print " " * (barwidth - (barwidth * ratio_design).to_i)
printf("] %5.1fWh (%5.1f%%)\n", info["ENERGY_NOW"].to_f / 1000000.0, ratio_charge * 100.0)
end
ac_status
Dir.glob('/sys/class/power_supply/BAT*').size.times do |id|
battery_info id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment