diff --git a/Cargo.toml b/Cargo.toml index 12f2395..92845d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,17 @@ -[package] -name = "watt" -description = "Modern CPU frequency and power management utility for Linux" -version = "0.4.0" -edition = "2024" -authors = [ "NotAShelf ", "RGBCube " ] -rust-version = "1.85" +[workspace] +members = [ "watt" ] +resolver = "3" -[dependencies] +[workspace.package] +authors = [ "NotAShelf ", "RGBCube " ] +description = "Modern CPU frequency and power management utility for Linux" +edition = "2024" # Keep in sync with .rustfmt.toml. +license = "MPL-2.0" +repository = "https://github.com/notashelf/watt" +rust-version = "1.85" +version = "0.4.0" + +[workspace.dependencies] anyhow = "1.0" clap = { version = "4.0", features = [ "derive", "env" ] } clap-verbosity-flag = "3.0.2" diff --git a/watt/Cargo.toml b/watt/Cargo.toml new file mode 100644 index 0000000..729ce63 --- /dev/null +++ b/watt/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "watt" +description.workspace = true +version.workspace = true +edition.workspace = true +authors.workspace = true +rust-version.workspace = true + +[lib] +path = "lib.rs" + +[[bin]] +name = "watt" +path = "main.rs" + +[dependencies] +anyhow.workspace = true +clap.workspace = true +clap-verbosity-flag.workspace = true +ctrlc.workspace = true +derive_more.workspace = true +env_logger.workspace = true +log.workspace = true +num_cpus.workspace = true +serde.workspace = true +thiserror.workspace = true +toml.workspace = true +yansi.workspace = true diff --git a/src/config.rs b/watt/config.rs similarity index 99% rename from src/config.rs rename to watt/config.rs index 4342a62..17fa748 100644 --- a/src/config.rs +++ b/watt/config.rs @@ -523,7 +523,7 @@ pub struct DaemonConfig { } impl DaemonConfig { - const DEFAULT: &str = include_str!("../config.toml"); + const DEFAULT: &str = include_str!("config.toml"); pub fn load_from(path: Option<&Path>) -> anyhow::Result { let contents = if let Some(path) = path { diff --git a/config.toml b/watt/config.toml similarity index 100% rename from config.toml rename to watt/config.toml diff --git a/src/cpu.rs b/watt/cpu.rs similarity index 100% rename from src/cpu.rs rename to watt/cpu.rs diff --git a/src/daemon.rs b/watt/daemon.rs similarity index 100% rename from src/daemon.rs rename to watt/daemon.rs diff --git a/src/fs.rs b/watt/fs.rs similarity index 100% rename from src/fs.rs rename to watt/fs.rs diff --git a/src/main.rs b/watt/lib.rs similarity index 56% rename from src/main.rs rename to watt/lib.rs index 0ec780c..6b84a5b 100644 --- a/src/main.rs +++ b/watt/lib.rs @@ -1,37 +1,27 @@ -mod cpu; -mod power_supply; -mod system; +use std::path::PathBuf; -mod fs; - -mod config; -// mod core; -mod daemon; -// mod engine; -// mod monitor; - -use std::{ - fmt::Write as _, - io, - io::Write as _, - path::PathBuf, - process, -}; - -use anyhow::Context; +use anyhow::Context as _; use clap::Parser as _; -use yansi::Paint as _; + +pub mod cpu; +pub mod power_supply; +pub mod system; + +pub mod fs; + +pub mod config; +pub mod daemon; #[derive(clap::Parser, Debug)] #[clap(author, version, about)] -struct Cli { +pub struct Cli { #[clap(subcommand)] command: Command, } #[derive(clap::Parser, Debug)] #[clap(multicall = true)] -enum Command { +pub enum Command { /// Watt daemon. Watt { #[command(flatten)] @@ -62,18 +52,18 @@ enum Command { } #[derive(clap::Parser, Debug)] -enum CpuCommand { +pub enum CpuCommand { /// Modify CPU attributes. Set(config::CpuDelta), } #[derive(clap::Parser, Debug)] -enum PowerCommand { +pub enum PowerCommand { /// Modify power supply attributes. Set(config::PowerDelta), } -fn real_main() -> anyhow::Result<()> { +pub fn main() -> anyhow::Result<()> { let cli = Cli::parse(); yansi::whenever(yansi::Condition::TTY_AND_COLOR); @@ -107,52 +97,3 @@ fn real_main() -> anyhow::Result<()> { } => delta.apply(), } } - -fn main() { - let Err(error) = real_main() else { - return; - }; - - let mut err = io::stderr(); - - let mut message = String::new(); - let mut chain = error.chain().rev().peekable(); - - while let Some(error) = chain.next() { - let _ = write!( - err, - "{header} ", - header = if chain.peek().is_none() { - "error:" - } else { - "cause:" - } - .red() - .bold(), - ); - - String::clear(&mut message); - let _ = write!(message, "{error}"); - - let mut chars = message.char_indices(); - - let _ = match (chars.next(), chars.next()) { - (Some((_, first)), Some((second_start, second))) - if second.is_lowercase() => - { - writeln!( - err, - "{first_lowercase}{rest}", - first_lowercase = first.to_lowercase(), - rest = &message[second_start..], - ) - }, - - _ => { - writeln!(err, "{message}") - }, - }; - } - - process::exit(1); -} diff --git a/watt/main.rs b/watt/main.rs new file mode 100644 index 0000000..8cb0902 --- /dev/null +++ b/watt/main.rs @@ -0,0 +1,57 @@ +use std::{ + fmt::Write as _, + io, + io::Write as _, + process, +}; + +use yansi::Paint as _; + +fn main() { + let Err(error) = watt::main() else { + return; + }; + + let mut err = io::stderr(); + + let mut message = String::new(); + let mut chain = error.chain().rev().peekable(); + + while let Some(error) = chain.next() { + let _ = write!( + err, + "{header} ", + header = if chain.peek().is_none() { + "error:" + } else { + "cause:" + } + .red() + .bold(), + ); + + String::clear(&mut message); + let _ = write!(message, "{error}"); + + let mut chars = message.char_indices(); + + let _ = match (chars.next(), chars.next()) { + (Some((_, first)), Some((second_start, second))) + if second.is_lowercase() => + { + writeln!( + err, + "{first_lowercase}{rest}", + first_lowercase = first.to_lowercase(), + rest = &message[second_start..], + ) + }, + + _ => { + writeln!(err, "{message}") + }, + }; + } + + process::exit(1); +} diff --git a/src/power_supply.rs b/watt/power_supply.rs similarity index 100% rename from src/power_supply.rs rename to watt/power_supply.rs diff --git a/src/system.rs b/watt/system.rs similarity index 100% rename from src/system.rs rename to watt/system.rs