Skip to content

Commit e65b19b

Browse files
author
Deren Vural
committed
formatting for trunk
Signed-off-by: Deren Vural <[email protected]>
1 parent 70245a2 commit e65b19b

File tree

5 files changed

+61
-65
lines changed

5 files changed

+61
-65
lines changed

src/formatter/mod.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,47 +72,46 @@ impl Formatter {
7272
Object::new(&[]).expect("Failed to create `Formatter`.")
7373
}
7474

75-
76-
pub fn format(self, values: Vec<String>, func: fn(Vec<String>) -> Option<String>) -> Option<String>{
75+
pub fn format(
76+
self,
77+
values: Vec<String>,
78+
func: fn(Vec<String>) -> Option<String>,
79+
) -> Option<String> {
7780
let mut results: Vec<String> = Vec::new();
7881

7982
// For each item in input list
8083
for i in values {
8184
// Remove all non-number characters
82-
let cleaned_value: String = i.chars().filter(|c| {
83-
// check if (base 10) digit
84-
if c.is_digit(10) {
85-
true
86-
} else {
87-
// check if full-stop
88-
if c.eq(&'.') {
85+
let cleaned_value: String = i
86+
.chars()
87+
.filter(|c| {
88+
// check if (base 10) digit
89+
if c.is_digit(10) {
8990
true
9091
} else {
91-
false
92+
// check if full-stop
93+
c.eq(&'.')
9294
}
93-
}
94-
}).collect();
95+
})
96+
.collect();
9597

9698
// Convert to float
9799
match cleaned_value.parse::<f64>() {
98100
Ok(parsed_value) => {
99101
// Convert to string
100102
results.push(parsed_value.to_string());
101-
},
103+
}
102104
Err(err) => {
103105
// Catch any errors..
104106
println!("Not a valid number: {}", err);
105-
},
107+
}
106108
}
107109
}
108110

109111
// Check for empty results
110-
if results.len() > 0 {
112+
if !results.is_empty() {
111113
// Apply any valid formatting
112-
match func(results) {
113-
Some(formatted_results) => Some(formatted_results),
114-
None => return None,
115-
}
114+
func(results)
116115
} else {
117116
None
118117
}
@@ -140,4 +139,3 @@ impl Default for Formatter {
140139
Self::new()
141140
}
142141
}
143-

src/main.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
mod custom_button;
2323
//use custom_button::CustomButton;
2424
mod processor;
25-
mod subprocess;
2625
mod property;
26+
mod subprocess;
2727
use property::Property;
2828
mod formatter;
2929
use formatter::Formatter;
@@ -169,29 +169,28 @@ fn build_ui(app: &Application) {
169169
let p: Property = Property::new(&proc, "", "", &form, &1);
170170

171171
let vecc: Vec<Vec<String>> = vec![
172-
vec!["1.68".to_string(),"2.01".to_string()],
173-
vec!["3.83".to_string(),"4.22".to_string()]
172+
vec!["1.68".to_string(), "2.01".to_string()],
173+
vec!["3.83".to_string(), "4.22".to_string()],
174174
];
175175
match p.parse(vecc, |input: Vec<String>| {
176176
Some(input.get(0).unwrap().to_string())
177177
}) {
178178
Some(results) => {
179179
println!("size: {}", results.len());
180180
for item in results {
181-
println!("item: {}",item);
181+
println!("item: {}", item);
182182
}
183-
},
183+
}
184184
None => println!("Something's gone really wrong!"),
185185
}
186186

187-
188187
// PERCENT
189188
let proc: Processor = Processor::new("nvidia-settings", "-q GpuUUID -t");
190189
let form: Formatter = Formatter::new();
191190
let p: Property = Property::new(&proc, "", "", &form, &1);
192191
let vecc: Vec<Vec<String>> = vec![
193-
vec!["1.68".to_string(),"2.01".to_string()],
194-
vec!["3.83".to_string(),"4.22".to_string()]
192+
vec!["1.68".to_string(), "2.01".to_string()],
193+
vec!["3.83".to_string(), "4.22".to_string()],
195194
];
196195
match p.parse(vecc, |input: Vec<String>| {
197196
// Grab input
@@ -206,20 +205,19 @@ fn build_ui(app: &Application) {
206205
Some(results) => {
207206
println!("size: {}", results.len());
208207
for item in results {
209-
println!("item: {}",item);
208+
println!("item: {}", item);
210209
}
211-
},
210+
}
212211
None => println!("Something's gone really wrong when formatting GENERIC info"),
213212
}
214213

215-
216214
// POWER
217215
let proc: Processor = Processor::new("nvidia-settings", "-q GpuUUID -t");
218216
let form: Formatter = Formatter::new();
219217
let p: Property = Property::new(&proc, "", "", &form, &1);
220218
let vecc: Vec<Vec<String>> = vec![
221-
vec!["1.68".to_string(),"2.01".to_string()],
222-
vec!["3.83".to_string(),"4.22".to_string()]
219+
vec!["1.68".to_string(), "2.01".to_string()],
220+
vec!["3.83".to_string(), "4.22".to_string()],
223221
];
224222
match p.parse(vecc, |input: Vec<String>| {
225223
// Grab input
@@ -239,32 +237,31 @@ fn build_ui(app: &Application) {
239237

240238
// Return result
241239
Some(output)
242-
},
240+
}
243241
Err(_) => {
244242
//this should catch "" etc
245243
println!("Not a valid number");
246244

247245
None
248-
},
246+
}
249247
}
250248
}) {
251249
Some(results) => {
252250
println!("size: {}", results.len());
253251
for item in results {
254-
println!("item: {}",item);
252+
println!("item: {}", item);
255253
}
256-
},
254+
}
257255
None => println!("Something's gone really wrong when formatting POWER info"),
258256
}
259257

260-
261258
// MEMORY
262259
let proc: Processor = Processor::new("nvidia-settings", "-q GpuUUID -t");
263260
let form: Formatter = Formatter::new();
264261
let p: Property = Property::new(&proc, "", "", &form, &1);
265262
let vecc: Vec<Vec<String>> = vec![
266-
vec!["1.68".to_string(),"2.01".to_string()],
267-
vec!["3.83".to_string(),"4.22".to_string()]
263+
vec!["1.68".to_string(), "2.01".to_string()],
264+
vec!["3.83".to_string(), "4.22".to_string()],
268265
];
269266
match p.parse(vecc, |input: Vec<String>| {
270267
// Grab input
@@ -290,41 +287,40 @@ fn build_ui(app: &Application) {
290287

291288
// Return result
292289
Some(output)
293-
},
290+
}
294291
Err(_) => {
295292
//this should catch "" etc
296293
println!("Not a valid number");
297294

298295
None
299-
},
296+
}
300297
}
301-
},
298+
}
302299
Err(_) => {
303300
//this should catch "" etc
304301
//TODO: fix this!
305302
println!("Not a valid number");
306303

307304
None
308-
},
305+
}
309306
}
310307
}) {
311308
Some(results) => {
312309
println!("size: {}", results.len());
313310
for item in results {
314-
println!("item: {}",item);
311+
println!("item: {}", item);
315312
}
316-
},
313+
}
317314
None => println!("Something's gone really wrong when formatting MEMORY info"),
318315
}
319316

320-
321317
// TEMPERATURE
322318
let proc: Processor = Processor::new("nvidia-settings", "-q GpuUUID -t");
323319
let form: Formatter = Formatter::new();
324320
let p: Property = Property::new(&proc, "", "", &form, &1);
325321
let vecc: Vec<Vec<String>> = vec![
326-
vec!["1.68".to_string(),"2.01".to_string()],
327-
vec!["3.83".to_string(),"4.22".to_string()]
322+
vec!["1.68".to_string(), "2.01".to_string()],
323+
vec!["3.83".to_string(), "4.22".to_string()],
328324
];
329325
match p.parse(vecc, |input: Vec<String>| {
330326
// Grab input
@@ -334,7 +330,7 @@ fn build_ui(app: &Application) {
334330
#[derive(Debug, PartialEq, Eq)]
335331
enum TemperatureUnit {
336332
CELCIUS = 0,
337-
FAHRENHEIT = 1
333+
FAHRENHEIT = 1,
338334
}
339335
let current_unit: TemperatureUnit = TemperatureUnit::CELCIUS;
340336
//let current_unit: TemperatureUnit = TemperatureUnit::FAHRENHEIT;
@@ -343,7 +339,7 @@ fn build_ui(app: &Application) {
343339
if current_unit == TemperatureUnit::CELCIUS {
344340
// Apply temperature unit
345341
output.push(char::from_u32(0x00B0).unwrap());
346-
output.push( 'C');
342+
output.push('C');
347343
} else if current_unit == TemperatureUnit::FAHRENHEIT {
348344
match output.parse::<f64>() {
349345
Ok(temp) => {
@@ -362,13 +358,13 @@ fn build_ui(app: &Application) {
362358

363359
// Return result
364360
Some(f_output)
365-
},
361+
}
366362
Err(_) => {
367363
//this should catch "" etc
368364
println!("Not a valid number");
369365

370366
None
371-
},
367+
}
372368
};
373369
}
374370

@@ -378,9 +374,9 @@ fn build_ui(app: &Application) {
378374
Some(results) => {
379375
println!("size: {}", results.len());
380376
for item in results {
381-
println!("item: {}",item);
377+
println!("item: {}", item);
382378
}
383-
},
379+
}
384380
None => println!("Something's gone really wrong when formatting TEMPERATURE info"),
385381
}
386382
});

src/processor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,6 @@ impl Processor {
282282
*/
283283
impl Default for Processor {
284284
fn default() -> Self {
285-
Self::new("","")
285+
Self::new("", "")
286286
}
287287
}

src/property/imp.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use gtk::subclass::prelude::*;
2626
use std::cell::Cell;
2727

2828
// Modules
29-
use crate::processor::Processor;
3029
use crate::formatter::Formatter;
30+
use crate::processor::Processor;
3131

3232
// Object holding the state
3333
#[derive(Default)]
@@ -127,9 +127,7 @@ impl ObjectImpl for Property {
127127
self.icon.replace(input_icon);
128128
}
129129
"gpu-count" => {
130-
let input_gpu_count = value
131-
.get()
132-
.expect("The value needs to be of type `i32`.");
130+
let input_gpu_count = value.get().expect("The value needs to be of type `i32`.");
133131
self.gpu_count.replace(input_gpu_count);
134132
}
135133
"call-extension" => {
@@ -186,7 +184,7 @@ impl ObjectImpl for Property {
186184
//TODO: this seems ridiculous..
187185
let value = self.gpu_count.take();
188186

189-
self.gpu_count.set(value.clone());
187+
self.gpu_count.set(value);
190188

191189
value.to_value()
192190
}

src/property/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use gtk::glib;
2727
use gtk::prelude::ObjectExt;
2828

2929
// Modules
30+
use crate::formatter::Formatter;
3031
use crate::processor::Processor;
31-
use crate::formatter::{Formatter};
3232

3333
// GObject wrapper for Property
3434
glib::wrapper! {
@@ -78,7 +78,7 @@ impl Property {
7878
call_extension: &str,
7979
icon: &str,
8080
formatter: &Formatter,
81-
gpu_count: &i32
81+
gpu_count: &i32,
8282
) -> Self {
8383
let obj: Property = Object::new(&[]).expect("Failed to create `Property`.");
8484

@@ -96,7 +96,11 @@ impl Property {
9696
//https://doc.rust-lang.org/std/primitive.array.html
9797
//https://www.tutorialspoint.com/rust/rust_array.htm
9898
//https://doc.rust-lang.org/std/vec/struct.Vec.html
99-
pub fn parse(self, values: Vec<Vec<String>>, func: fn(Vec<String>) -> Option<String>) -> Option<Vec<String>> {
99+
pub fn parse(
100+
self,
101+
values: Vec<Vec<String>>,
102+
func: fn(Vec<String>) -> Option<String>,
103+
) -> Option<Vec<String>> {
100104
let mut results: Vec<String> = Vec::new();
101105

102106
// For each GPU
@@ -113,6 +117,6 @@ impl Property {
113117
}
114118

115119
pub fn get_call_extension(&self) -> String {
116-
self.property::<String>("call-extension").to_owned()
120+
self.property::<String>("call-extension")
117121
}
118122
}

0 commit comments

Comments
 (0)