Class: VER::Status::Battery

Inherits:
Tk::Canvas
  • Object
show all
Defined in:
lib/ver/status/battery.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @status = status
  @weight = options.delete(:weight) || 0
  @format = options.delete(:format) || '[%b] %p% %t'
  @row = options.delete(:row)
  @column = options.delete(:column)
  @sticky = options.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

#columnObject (readonly)

Returns the value of attribute column.



4
5
6
# File 'lib/ver/status/battery.rb', line 4

def column
  @column
end

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/ver/status/battery.rb', line 4

def format
  @format
end

#rowObject (readonly)

Returns the value of attribute row.



4
5
6
# File 'lib/ver/status/battery.rb', line 4

def row
  @row
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/ver/status/battery.rb', line 4

def status
  @status
end

#stickyObject (readonly)

Returns the value of attribute sticky.



4
5
6
# File 'lib/ver/status/battery.rb', line 4

def sticky
  @sticky
end

#weightObject (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.message
end

Instance Method Details

#draw_batteryObject



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_textObject



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.options[: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