1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
132
133
134
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
use crate::Update;
use battery::units::time::minute;
use battery::{
units::{ratio::percent, thermodynamic_temperature::degree_celsius},
Battery, Manager,
};
use std::fmt;
#[derive(Default)]
pub struct BatPlugin {
manager: Vec<Manager>,
batteries: Vec<Battery>,
format: BatOut,
display: String,
}
enum BatOut {
Celsius,
Percent,
Time,
}
impl Default for BatOut {
fn default() -> Self {
BatOut::Percent
}
}
impl BatPlugin {
fn new() -> Self {
let manager = Manager::new().unwrap();
let batteries = manager.batteries().unwrap();
let batteries = {
let mut vec = Vec::new();
for i in batteries {
vec.push(i.unwrap());
}
vec
};
BatPlugin {
manager: vec![manager],
batteries,
..BatPlugin::default()
}
}
pub fn new_percent() -> Box<BatPlugin> {
Box::new(BatPlugin::new())
}
pub fn new_celsius() -> Box<BatPlugin> {
let mut bat_plug = BatPlugin::new();
bat_plug.format = BatOut::Celsius;
Box::new(bat_plug)
}
pub fn new_time() -> Box<BatPlugin> {
let mut bat_plug = BatPlugin::new();
bat_plug.format = BatOut::Time;
Box::new(bat_plug)
}
}
impl fmt::Display for BatPlugin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.display)
}
}
impl Update for BatPlugin {
fn refresh(&mut self) {
for i in &mut self.batteries {
self.manager[0].refresh(i).unwrap();
}
let format = match self.format {
BatOut::Percent => self.update_percent(),
BatOut::Celsius => self.update_celcius(),
BatOut::Time => self.update_time(),
};
self.display = format;
}
}
trait BatExt {
fn update_percent(&self) -> String;
fn update_time(&self) -> String;
fn update_celcius(&self) -> String;
fn time_to_empty(batteries: &Vec<Battery>) -> String {
let mut string = String::new();
for i in batteries {
if let Some(value) = i.time_to_empty() {
let duration = value.get::<minute>();
if duration > 60.0 {
let duration = (duration.round() / 60_f32).round();
string.push_str(format!("{}h", duration).as_str());
} else {
let duration = duration.round();
string.push_str(format!("{}m", duration).as_str());
}
}
}
string
}
fn time_to_full(batteries: &Vec<Battery>) -> String {
let mut string = String::new();
for i in batteries {
if let Some(value) = i.time_to_full() {
let duration = value.get::<minute>();
if duration > 60.0 {
let duration = duration.round() / 60_f32;
string.push_str(format!("{}h (Charging)", duration).as_str());
} else {
let duration = duration.round();
string.push_str(format!("{}m (Charging)", duration).as_str());
}
}
}
string
}
}
impl BatExt for BatPlugin {
fn update_percent(&self) -> String {
let mut string = String::new();
for i in &self.batteries {
string.push_str(format!("{}%", i.state_of_charge().get::<percent>().round()).as_str());
}
string
}
fn update_time(&self) -> String {
match self.batteries[0].state() {
battery::State::Discharging => BatPlugin::time_to_empty(&self.batteries),
battery::State::Charging => BatPlugin::time_to_full(&self.batteries),
battery::State::Empty => String::from("empty"),
battery::State::Full => String::from("Full"),
battery::State::__Nonexhaustive => String::from("nonexhastive"),
battery::State::Unknown => String::from("Unknown"),
}
}
fn update_celcius(&self) -> String {
let mut string = String::new();
for i in &self.batteries {
if let Some(value) = i.temperature() {
string.push_str(format!("{} °C", value.get::<degree_celsius>()).as_str());
}
}
string
}
}