Skip to content

Commit b9b502c

Browse files
authored
Merge pull request #16 from derenv/output_formatting_1
Updated Properties: - New property "Memory Controller Utilization" - Cleaned up property labels - nvidia-settings specific fixes Better Provider-change handling - Checks for a change in provider when refresh button pressed
2 parents f5beda2 + 3d281ae commit b9b502c

File tree

9 files changed

+1434
-770
lines changed

9 files changed

+1434
-770
lines changed

src/formatter/mod.rs

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -117,63 +117,93 @@ impl Formatter {
117117
* Notes:
118118
*
119119
*/
120-
pub fn format(self, value: String) -> Option<String> {
120+
pub fn format(self, value: String, clean: bool) -> Option<String> {
121121
//println!("FORMATTING");//TEST
122122

123-
// Remove all non-number characters
124-
let cleaned_value: String = value
125-
.chars()
126-
.filter(|c| {
127-
// check if (base 10) digit
128-
if c.is_digit(10) {
129-
true
130-
} else {
131-
// check if full-stop
132-
c.eq(&'.')
133-
}
134-
})
135-
.collect();
136-
137-
// Convert to float
138-
match cleaned_value.parse::<f64>() {
139-
Ok(parsed_value) => {
140-
// Apply any valid formatting
141-
match self.imp().func.take() {
142-
Some(func) => {
143-
// Grab all format info from settings (this is done here to keep in one place)
144-
//===
145-
// Temperature format
146-
let temp_format: i32 = self.imp().get_setting::<i32>("tempformat");
147-
let mut params: Vec<(String, String)> = vec![];
148-
if let 0 = temp_format {
149-
params.push((String::from("tempformat"), String::from("C")));
150-
} else if let 1 = temp_format {
151-
params.push((String::from("tempformat"), String::from("F")));
152-
}
153-
//TODO: ???
154-
//
155-
//===
156-
157-
// Use function
158-
let result: Option<String>;
159-
if !params.is_empty() {
160-
result = func(vec![parsed_value.to_string()], Some(params));
161-
} else {
162-
result = func(vec![parsed_value.to_string()], None);
163-
}
164-
// Return it!
165-
self.imp().func.set(Some(func));
123+
// Apply cleaning if required
124+
if clean {
125+
// Remove all non-number characters
126+
let cleaned_value: String = value
127+
.chars()
128+
.filter(|c| {
129+
// check if (base 10) digit
130+
if c.is_digit(10) {
131+
true
132+
} else {
133+
// check if full-stop
134+
c.eq(&'.')
135+
}
136+
})
137+
.collect();
138+
139+
// Convert to float
140+
match cleaned_value.parse::<f64>() {
141+
Ok(parsed_value) => {
142+
// Apply any valid formatting
143+
match self.imp().func.take() {
144+
Some(func) => {
145+
// Grab all format info from settings (this is done here to keep in one place)
146+
//===
147+
// Temperature format
148+
let temp_format: i32 = self.imp().get_setting::<i32>("tempformat");
149+
let mut params: Vec<(String, String)> = vec![];
150+
if let 0 = temp_format {
151+
params.push((String::from("tempformat"), String::from("C")));
152+
} else if let 1 = temp_format {
153+
params.push((String::from("tempformat"), String::from("F")));
154+
}
155+
//TODO: ???
156+
//
157+
//===
158+
159+
// Use function
160+
let result: Option<String>;
161+
if !params.is_empty() {
162+
result = func(vec![parsed_value.to_string()], Some(params));
163+
} else {
164+
result = func(vec![parsed_value.to_string()], None);
165+
}
166+
// Return it!
167+
self.imp().func.set(Some(func));
166168

167-
result
169+
// Return final result
170+
result
171+
}
172+
None => panic!("Missing formatting function!"),
168173
}
169-
None => panic!("Missing formatting function!"),
174+
}
175+
Err(err) => {
176+
// Catch any errors..
177+
println!("Not a valid number: {}", err);
178+
179+
None
170180
}
171181
}
172-
Err(err) => {
173-
// Catch any errors..
174-
println!("Not a valid number: {}", err);
182+
} else {
183+
// Apply any valid formatting
184+
match self.imp().func.take() {
185+
Some(func) => {
186+
// Grab all format info from settings (this is done here to keep in one place)
187+
//===
188+
let mut _params: Vec<(String, String)> = vec![]; //mut
189+
//TODO: ???
190+
//
191+
//===
175192

176-
None
193+
// Use function
194+
let result: Option<String>;
195+
if !_params.is_empty() {
196+
result = func(vec![value], Some(_params));
197+
} else {
198+
result = func(vec![value], None);
199+
}
200+
// Return it!
201+
self.imp().func.set(Some(func));
202+
203+
// Return final result
204+
result
205+
}
206+
None => panic!("Missing formatting function!"),
177207
}
178208
}
179209
}

src/gpu_page/mod.rs

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,29 @@ impl GpuPage {
165165
// TODO: Load config of gpu (use uuid as ID)
166166
// TODO: needs to be a vector as may bed of variable size..
167167
//let statistics: Vec<&str> = load_json_settings(&self.property("uuid"));//array?vector?json-type object?
168-
let statistics_data: Vec<&str> = vec![
169-
"util",
170-
"temp",
171-
"memory_usage",
172-
"memory_total",
173-
"fan_speed",
174-
"power_usage",
175-
];
168+
let statistics_data: Vec<&str>;//TEST
169+
match self.property::<Provider>("provider").property::<i32>("provider-type") {
170+
1 => {
171+
statistics_data = vec![
172+
"util",
173+
"temp",
174+
"memory_usage",
175+
"memory_total",
176+
"mem_ctrl_util",
177+
];
178+
}
179+
_ => {
180+
statistics_data = vec![
181+
"util",
182+
"temp",
183+
"memory_usage",
184+
"memory_total",
185+
"mem_ctrl_util",
186+
"fan_speed",
187+
"power_usage",
188+
];
189+
}
190+
}
176191
let statistics_store: Arc<Mutex<Vec<&str>>> = Arc::new(Mutex::new(statistics_data));
177192

178193
// Edit button
@@ -220,38 +235,94 @@ impl GpuPage {
220235
//child_manager.set_property("outline-width", 1);
221236
//child_manager.set_property("border-radius", 3);
222237

223-
// Fetch layout manager for this child grid
238+
// Fetch layout manager for this (grid) child
224239
let internal_grid_manager = new_grid.layout_manager().expect("Fuck..");
225240

226-
// Build label & add to grid
241+
// Decide on title label size
242+
let space: i32;
243+
let pretty_label: &str;
244+
match statistic.to_owned() {
245+
"util" => {
246+
pretty_label = "GPU Utilization";
247+
space = 5
248+
},
249+
"mem_ctrl_util" => {
250+
pretty_label = "Memory Controller Utilization";
251+
space = 5
252+
},
253+
"encoder_util" => {
254+
pretty_label = "Encoder Utilization";
255+
space = 5
256+
},
257+
"decoder_util" => {
258+
pretty_label = "Decoder Utilization";
259+
space = 5
260+
},
261+
"fan_speed" => {
262+
pretty_label = "Fan Speed";
263+
space = 5
264+
},
265+
"temp" => {
266+
pretty_label = "Temperature";
267+
space = 5
268+
},
269+
"memory_usage" => {
270+
pretty_label = "Memory Usage";
271+
space = 8
272+
},
273+
"memory_total" => {
274+
pretty_label = "Memory Total";
275+
space = 8
276+
},
277+
"power_usage" => {
278+
pretty_label = "Power Usage";
279+
space = 8
280+
},
281+
_ => {
282+
pretty_label = statistic;
283+
space = 5
284+
},
285+
}
286+
287+
// Build title label & add to grid
227288
let new_title: String = String::from(statistic.to_owned()) + "_label";
228289
let new_title_label: Label = Label::builder()
229-
.label(statistic)
290+
.label(pretty_label)
230291
.name(&new_title)
231292
.hexpand(true)
232293
.hexpand_set(true)
233294
.halign(Align::Center)
234295
//.valign(Align::Center)
235296
.margin_top(12)
236297
.margin_bottom(12)
298+
.width_chars(space)
237299
.build();
238300
new_grid.attach(&new_title_label, 0, 0, 1, 1);
239301

240-
// Set layout properties of child
302+
// Set layout properties of (title label) child
241303
let title_manager: LayoutChild = internal_grid_manager.layout_child(&new_title_label);
242304
title_manager.set_property("row-span", 1);
243305

244-
// Build label & add to grid
306+
// Decide on content label size
307+
let space: i32;
308+
match statistic.to_owned() {
309+
"util" | "fan_speed" | "temp" => space = 5,
310+
"memory_usage" | "memory_total" => space = 8,
311+
_ => space = 5,
312+
}
313+
314+
// Build content label & add to grid
245315
let new_content: String = String::from(statistic.to_owned());
246316
let new_content_label: Label = Label::builder()
247317
.label("")
248318
.name(&new_content)
249319
//.halign(Align::End)
250320
//.valign(Align::Center)
321+
.width_chars(space)
251322
.build();
252323
new_grid.attach(&new_content_label, 1, 0, 1, 1);
253324

254-
// Set layout properties of child
325+
// Set layout properties of (content label) child
255326
let content_manager: LayoutChild = internal_grid_manager.layout_child(&new_content_label);
256327
content_manager.set_property("row-span", 1);
257328

@@ -266,7 +337,7 @@ impl GpuPage {
266337
labels.push(new_content_label);
267338
}
268339

269-
// Fetch uuid, needed for processor
340+
// Create thread safe container for uuid, needed for processor
270341
let uuid_store: Arc<Mutex<String>> = Arc::new(Mutex::new(self.property("uuid")));
271342

272343
// Create thread safe container for provider
@@ -275,14 +346,13 @@ impl GpuPage {
275346
// Async fill the labels
276347
glib::timeout_add_seconds_local(refresh_rate, move || {
277348
// Grab locked data
278-
// Get list of statistics
349+
// list of statistics
279350
let statistics_lock: Arc<Mutex<Vec<&str>>> = Arc::clone(&statistics_store);
280351
let statistics: MutexGuard<Vec<&str>> = statistics_lock.lock().unwrap();
281-
// Get uuid
352+
// uuid
282353
let uuid_lock: Arc<Mutex<String>> = Arc::clone(&uuid_store);
283354
let uuid: String = uuid_lock.lock().unwrap().as_str().to_owned();
284-
285-
// Create provider for scanning gpu data
355+
// current provider for scanning gpu data
286356
let provider_lock: Arc<Mutex<Option<Provider>>> = Arc::clone(&provider_store);
287357
let mut provider_container: MutexGuard<Option<Provider>> = provider_lock.lock().unwrap();
288358

0 commit comments

Comments
 (0)