Skip to content

Commit

Permalink
Removed unwrap calls, changed test serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
shibedrill committed Nov 11, 2023
1 parent 8b7cade commit b75adff
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/test_output.txt
/test_output.txt
/test_output.ron
76 changes: 65 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "oxidefetch"
description = "A fast, cross platform Fetch program for your terminal"
version = "1.4.6"
version = "1.4.7"
edition = "2021"
authors = [ "NamedNeon", "shibedrill" ]
license = "MIT"
Expand All @@ -16,9 +16,14 @@ colored = "2.0.0"
compound_duration = "1.2.0"
lazy_static = "1.4.0"
quoted-string = "0.6.1"
ron = "0.8.1"
sysinfo = "0.29.1"
whoami = "1.3.0"

[dependencies.serde]
version = "1.0.192"
features = ["derive"]

[profile.release]
strip = true
opt-level = "z"
Expand All @@ -27,13 +32,4 @@ codegen-units = 1
panic = "abort"

[features]
field-titles = []

[package.metadata.rpm]
package = "oxidefetch"

[package.metadata.rpm.cargo]
buildflags = ["--release"]

[package.metadata.rpm.targets]
oxidefetch = { path = "/usr/bin/oxidefetch" }
field-titles = []
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ No weird quirks to report at this time.
**1.4.4:** Fixed an issue where GPUs would all print on one line.
**1.4.5:** Minor changes to system color detection. Removed all warnings.
**1.4.6:** Cargo formatting applied to all files. Mild string reformatting in print statements.
**1.4.7:** Removed several `unwrap()` calls. Changed debug output to serialize to RON.

### License
This software is covered by the MIT license. See license.txt for details.
25 changes: 15 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use std::env;
use sysinfo::*;
use whoami;

#[cfg(test)]
use serde::Serialize;

fn main() {
// Generate system info struct
let sys_info = Information::new();
Expand Down Expand Up @@ -63,9 +66,8 @@ fn main() {
color_print("Terminal:\t", '', &sys_info.terminal, "magenta");
color_print("CPU:\t", '', &Some(sys_info.cpu), "green");

if sys_info.gpu.is_some() {
let gpus = sys_info.gpu.unwrap();
for gpu in gpus {
if let Some(gpuvec) = sys_info.gpu {
for gpu in gpuvec {
color_print("GPU:\t", '󰍹', &Some(gpu), "bright green")
}
}
Expand All @@ -76,17 +78,18 @@ fn main() {
#[allow(unused_variables)] // The field title var is sometimes unused due to compile time features
fn color_print(field_title: &str, icon: char, field: &Option<String>, color: &str) {
// If the field is missing, it won't print.
if field.is_some() {
if let Some(fieldvalue) = field {
#[cfg(feature = "field-titles")]
print!("{} ", field_title.bright_white());
println!(
"{}",
format!("{} {}", icon, field.as_ref().unwrap()).color(color)
format!("{} {}", icon, fieldvalue).color(color)
);
}
}

#[derive(Debug)]
#[cfg_attr(test, derive(Serialize))]
struct Information {
// Only fields whose setters can fail, are given Option<String> types.
// Unsure if I should coerce these fields into an Option<String> *here*, or
Expand Down Expand Up @@ -137,8 +140,8 @@ impl Information {
// Tracks the SHELL env var and trims the last item from the resultant fs path.
shell: {
let var = env::var("SHELL");
if var.is_ok() {
Some(format!("{}", var.unwrap().split('/').last().unwrap()))
if let Ok(var_ok) = var {
Some(format!("{}", var_ok.split('/').last().unwrap()))
} else {
None
}
Expand Down Expand Up @@ -288,14 +291,16 @@ mod test {
#[test]
pub fn log_gathered_data() {
let sys_info = Information::new();
let data_string = format!("{:#?}", sys_info);
let result = fs::write("./test_output.txt", data_string);
//let data_string = format!("{:#?}", sys_info);
let data_string = ron::ser::to_string_pretty(&sys_info, ron::ser::PrettyConfig::default())
.expect("Failed to serialize data structure. Aborting...");
let result = fs::write("./test_output.ron", data_string);

if result.is_ok() {
println!(
"
HEY THERE! A logging file was generated by this test. \
It's located in this folder, and called 'test_output.txt'. \
It's located in this folder, and called 'test_output.ron'. \
SEND THIS FILE to the maintainer of the project!
"
);
Expand Down

0 comments on commit b75adff

Please sign in to comment.