this repo has no description
dotfiles
at main 99 lines 2.4 kB view raw
1#!/usr/bin/env ruby 2# encoding: utf-8 3 4require 'ostruct' 5 6FA_LIGHTNING = "<span color='yellow'><span font='FontAwesome'>\uf0e7</span></span>" 7FA_PLUG = "<span font='FontAwesome'>\uf1e6</span>" 8FA_UNKNOWN = "<span font='FontAwesome'>\uf128</span>" 9 10def color(percent) 11 if percent < 10 12 '#FF0000' 13 elsif percent < 20 14 '#FF3300' 15 elsif percent < 30 16 '#FF6600' 17 elsif percent < 40 18 '#FF9900' 19 elsif percent < 50 20 '#FFCC00' 21 elsif percent < 60 22 '#FFFF00' 23 elsif percent < 70 24 '#FFFF33' 25 elsif percent < 80 26 '#FFFF66' 27 else 28 '#FFFFFF' 29 end 30end 31 32def batt_symbol(percent) 33 if percent < 10 34 "\uf244" #  35 elsif percent < 40 36 "\uf243" #  37 elsif percent < 60 38 "\uf242" #  39 elsif percent < 80 40 "\uf241" #  41 else 42 "\uf240" #  43 end 44end 45 46def batt_to_s(batteries, battery, short = false) 47 output = battery[:status] == :charging ? (FA_LIGHTNING + ' ') : '' 48 batt_color = batteries.all? { |b| b[:percent] < 10 } ? '#FFFFFF' : color(battery[:percent]) 49 if short 50 output += "<span color='#{batt_color}' font='FontAwesome'>#{batt_symbol battery[:percent]}</span>" 51 else 52 output += "#{battery[:id]}: <span color='#{batt_color}'>#{battery[:percent]}%</span>" 53 output += " #{battery[:hours].round 1}h" if [:discharging, :charging].include? battery[:status] 54 end 55 56 output 57end 58 59data = `acpi` 60 61if not data 62 puts "<span color='red'><span font='FontAwesome'>\uf00d \uf240</span></span>" 63 puts "<span color='red'><span font='FontAwesome'>\uf00d \uf240</span></span>" 64 exit 0 65end 66 67batteries = data.split("\n").map do |line| 68 next unless line =~ /^Battery (\d+): ([^,]+), (\d+)%/ 69 70 data = { id: $1.to_i, status: $2.downcase.to_sym, percent: $3.to_i } 71 72 case data[:status] 73 when :charging 74 data[:eta] = line.match(/(\d+:\d+:\d+) until charged/).to_a.last 75 when :discharging 76 data[:eta] = line.match(/(\d+:\d+:\d+) remaining/).to_a.last 77 end 78 79 if data[:eta] 80 segs = data[:eta].split ':' 81 data[:hours] = segs.first.to_f 82 data[:hours] += segs[1].to_f / 60.0 83 end 84 85 data 86end 87 88charging = `acpi -a` =~ /^Adapter \d+: on-line$/ 89 90output = charging ? FA_PLUG + ' ' : '' 91short_output = output 92 93output += batteries.map { |batt| batt_to_s batteries, batt }.join ' ' 94short_output += batteries.map { |batt| batt_to_s batteries, batt, true}.join ' ' 95 96puts output 97puts short_output 98 99exit 33 if batteries.all? { |batt| batt[:percent] < 10 }