Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zurrty committed Jan 11, 2023
1 parent fd8e96c commit 6a8b83d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
15 changes: 7 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ use tini::Ini;

use crate::{Error, Vendor};

#[derive(Debug, Clone)]
pub struct Config {
pub first_use: bool,
pub gpu_priority: Vec<Vendor>,
pub kill_on_unplug: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
first_use: true,
gpu_priority: vec![Vendor::NVIDIA, Vendor::AMD, Vendor::Intel],
kill_on_unplug: true,
}
}
}
Expand All @@ -24,23 +23,24 @@ impl Config {
let path = config_path();
std::fs::create_dir_all(&path.parent().unwrap())?;
if !std::fs::try_exists(&path)? {
std::fs::File::create(path)?;
std::fs::File::create(&path)?;
}

let ini = Ini::from_file(&config_path())?;
Ok(Self {
first_use: ini.get("general", "first_use").unwrap_or(true),
gpu_priority: ini
.get_vec::<String>("general", "gpu_priority")
.unwrap_or(vec!["nvidia".into(), "amd".into(), "intel".into()])
.get::<String>("general", "gpu_priority")
.unwrap_or(String::from("nvidia, amd, intel"))
.split(",")
.into_iter()
.filter_map(|vendor| match vendor.trim() {
.filter_map(|vendor| match vendor.to_ascii_lowercase().trim() {
"nvidia" => Some(Vendor::NVIDIA),
"amd" => Some(Vendor::AMD),
"intel" => Some(Vendor::Intel),
_ => None,
})
.collect(),
kill_on_unplug: ini.get("general", "kill_on_unplug").unwrap_or(true),
})
}
pub fn save(&self) -> Result<(), super::Error> {
Expand All @@ -55,7 +55,6 @@ impl Config {
.map(|v| v.to_string())
.collect::<Vec<String>>(),
)
.item("kill_on_unplug", self.kill_on_unplug)
.to_file(config_path().as_path())
.map_err(|e| Error::Io(e))
}
Expand Down
33 changes: 15 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn find_gpus() -> Result<Vec<GPU>, Error> {

pub fn prime_run(args: Vec<String>) -> Result<(), Error> {
let mut config = config::Config::open()?;
println!("{:?}", config);
if config.first_use {
log::info("It seems that it's your first time using primer, welcome!\nYou can edit the config at \"~/.config/primer/config.ini\"");
config.first_use = false;
Expand All @@ -156,22 +157,26 @@ pub fn prime_run(args: Vec<String>) -> Result<(), Error> {
return Err(e);
}
};

gpus.sort_by(|a, b| {
config
.gpu_priority
.clone()
.iter()
.position(|p| p == &a.vendor)
.cmp(&config.gpu_priority.iter().position(|p| p == &b.vendor))
let priority_a = config.gpu_priority.iter().position(|p| p == &a.vendor);
let priority_b = config.gpu_priority.iter().position(|p| p == &b.vendor);

priority_a.cmp(&priority_b)
});
let gpu = match gpus.first() {
Some(gpu) => gpu,
None => return Err(Error::DeviceNotFound),
};
println!("{}", "-- GPUs --".bold());
gpus.iter().for_each(|d| {
let name = d.name_fancy();
println!("{}", name.bold());
});
if gpu.integrated {
log::info("No discrete GPU detected, using integrated graphics.");
}
gpu.prepare_run(args)?.spawn()?;
gpu.prepare_run(args)?.spawn()?.wait()?;
Ok(())
}

Expand All @@ -187,7 +192,9 @@ fn main() -> Result<(), Error> {
Error::Io(err) => log::error(err),
Error::Ini(err) => log::error(err),
Error::DeviceNotFound => log::error("No device found!"),
Error::InvalidDevice => log::error("Graphics device invalid.\nMake sure you have the correct, and latest drivers."),
Error::InvalidDevice => log::error(
"Graphics device invalid.\nMake sure you have the correct and latest drivers.",
),
Error::EmptyCommand => println!("Usage: primer <command>"),
}
}
Expand All @@ -213,13 +220,3 @@ mod log {
show(text)
}
}

#[cfg(test)]
mod tests {
use crate::prime_run;

#[test]
fn test_glxinfo() {
prime_run(vec!["glxinfo".to_string(), "-B".to_string()]).unwrap();
}
}

0 comments on commit 6a8b83d

Please sign in to comment.