1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

watt: turn into a library

This commit is contained in:
RGBCube 2025-06-14 22:54:46 +03:00
parent 3c82679ada
commit a341d08c45
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
11 changed files with 115 additions and 84 deletions

View file

@ -1,12 +1,17 @@
[package] [workspace]
name = "watt" members = [ "watt" ]
description = "Modern CPU frequency and power management utility for Linux" resolver = "3"
version = "0.4.0"
edition = "2024"
authors = [ "NotAShelf <raf@notashelf.dev>", "RGBCube <git@rgbcu.be>" ]
rust-version = "1.85"
[dependencies] [workspace.package]
authors = [ "NotAShelf <raf@notashelf.dev>", "RGBCube <git@rgbcu.be>" ]
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" anyhow = "1.0"
clap = { version = "4.0", features = [ "derive", "env" ] } clap = { version = "4.0", features = [ "derive", "env" ] }
clap-verbosity-flag = "3.0.2" clap-verbosity-flag = "3.0.2"

28
watt/Cargo.toml Normal file
View file

@ -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

View file

@ -523,7 +523,7 @@ pub struct DaemonConfig {
} }
impl 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<Self> { pub fn load_from(path: Option<&Path>) -> anyhow::Result<Self> {
let contents = if let Some(path) = path { let contents = if let Some(path) = path {

View file

@ -1,37 +1,27 @@
mod cpu; use std::path::PathBuf;
mod power_supply;
mod system;
mod fs; use anyhow::Context as _;
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 clap::Parser 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)] #[derive(clap::Parser, Debug)]
#[clap(author, version, about)] #[clap(author, version, about)]
struct Cli { pub struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
command: Command, command: Command,
} }
#[derive(clap::Parser, Debug)] #[derive(clap::Parser, Debug)]
#[clap(multicall = true)] #[clap(multicall = true)]
enum Command { pub enum Command {
/// Watt daemon. /// Watt daemon.
Watt { Watt {
#[command(flatten)] #[command(flatten)]
@ -62,18 +52,18 @@ enum Command {
} }
#[derive(clap::Parser, Debug)] #[derive(clap::Parser, Debug)]
enum CpuCommand { pub enum CpuCommand {
/// Modify CPU attributes. /// Modify CPU attributes.
Set(config::CpuDelta), Set(config::CpuDelta),
} }
#[derive(clap::Parser, Debug)] #[derive(clap::Parser, Debug)]
enum PowerCommand { pub enum PowerCommand {
/// Modify power supply attributes. /// Modify power supply attributes.
Set(config::PowerDelta), Set(config::PowerDelta),
} }
fn real_main() -> anyhow::Result<()> { pub fn main() -> anyhow::Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
yansi::whenever(yansi::Condition::TTY_AND_COLOR); yansi::whenever(yansi::Condition::TTY_AND_COLOR);
@ -107,52 +97,3 @@ fn real_main() -> anyhow::Result<()> {
} => delta.apply(), } => 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);
}

57
watt/main.rs Normal file
View file

@ -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);
}