Class: VER::Status::Battery
- Inherits:
-
Tk::Canvas
- Object
- Tk::Canvas
- VER::Status::Battery
- Defined in:
- lib/ver/status/battery.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#sticky ⇒ Object
readonly
Returns the value of attribute sticky.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Class Method Summary collapse
Instance Method Summary collapse
- #draw_battery ⇒ Object
- #draw_text ⇒ Object
-
#initialize(status, options = {}) ⇒ Battery
constructor
A new instance of Battery.
- #show(percent, string) ⇒ Object
- #style=(config) ⇒ Object
Constructor Details
#initialize(status, options = {}) ⇒ Battery
Returns a new instance of Battery.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ver/status/battery.rb', line 6 def initialize(status, = {}) @status = status @weight = .delete(:weight) || 0 @format = .delete(:format) || '[%b] %p% %t' @row = .delete(:row) @column = .delete(:column) @sticky = .delete(:sticky) super @width, @height = cget(:width), cget(:height) draw_battery draw_text percent, string = Battery.update(format) show(percent / 100.0, string) @show_block = VER.when_inactive_for(5000){ percent, string = Battery.update(format) show(percent / 100.0, string) } tk_parent.bind '<Destroy>' do |event| VER.cancel_block(@show_block) end end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def column @column end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def format @format end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def row @row end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def status @status end |
#sticky ⇒ Object (readonly)
Returns the value of attribute sticky.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def sticky @sticky end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
4 5 6 |
# File 'lib/ver/status/battery.rb', line 4 def weight @weight end |
Class Method Details
.battery_build(format) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ver/status/battery.rb', line 86 def self.battery_build(format) total = {} Dir.glob('/proc/acpi/battery/*/{state,info}') do |file| parsed = battery_parse(file) next unless parsed[:present] == 'yes' # FIXME: doesn't take care of multiple batteries total.merge!(parsed) end # rate might be 0 rate = total[:present_rate].to_i capacity = total[:remaining_capacity].to_i if rate == 0 hours, percent = 2, 100 time = hours_left = minutes_left = 'N/A' else hours, minutes = ((capacity * 60.0) / rate).divmod(60) minutes = minutes.round percent = ((100 / total[:last_full_capacity].to_f) * capacity).round hours_left = (hours + (minutes / 60.0)).round minutes_left = (hours / 60.0) + minutes time = "#{hours}:#{minutes}" end case total[:charging_state] when 'discharging' b = hours < 1 ? '!' : '-' when 'charging' b = '+' end final = { '%c' => capacity, '%r' => rate, '%b' => b, '%p' => percent, '%m' => minutes_left, '%h' => hours_left, '%t' => time, } @last = Time.now return percent, format.gsub(/%\w/, final) end |
.battery_parse(file) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ver/status/battery.rb', line 133 def self.battery_parse(file) data = {} File.open(file) do |io| io.each_line do |line| next unless line =~ /^([^:]+):\s*(.+)$/ data[$1.downcase.tr(' ', '_').to_sym] = $2 end end data end |
.update(format) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ver/status/battery.rb', line 67 def self.update(format) now = Time.now if @battery_last if @battery_last < (now - 60) @battery_last = now @battery_value = battery_build(format) else @battery_value end else @battery_last = now @battery_value = battery_build(format) end rescue => ex VER.error(ex) ex. end |
Instance Method Details
#draw_battery ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ver/status/battery.rb', line 37 def draw_battery # yay for scalable vector graphics @battery = create_polygon( 0, 0, 0, @height, @width * 0.95, @height, @width * 0.95, @height * 0.8, @width, @height * 0.8, @width, @height * 0.2, @width * 0.95, @height * 0.2, @width * 0.95, 0 ) end |
#draw_text ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/ver/status/battery.rb', line 51 def draw_text @label = create_text( @width * 0.5, @height * 0.5, font: status.text.[:font], anchor: 'center' ) end |
#show(percent, string) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/ver/status/battery.rb', line 60 def show(percent, string) r, g, b = 0xffff - (0xffff * percent), (0xffff * percent), 0 color = '#%04x%04x%04x' % [r, g, b] @battery.configure fill: color @label.configure text: string end |
#style=(config) ⇒ Object
33 34 35 |
# File 'lib/ver/status/battery.rb', line 33 def style=(config) configure(background: config[:background]) end |