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:
parent
3c82679ada
commit
a341d08c45
11 changed files with 115 additions and 84 deletions
21
Cargo.toml
21
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 <raf@notashelf.dev>", "RGBCube <git@rgbcu.be>" ]
|
||||
rust-version = "1.85"
|
||||
[workspace]
|
||||
members = [ "watt" ]
|
||||
resolver = "3"
|
||||
|
||||
[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"
|
||||
clap = { version = "4.0", features = [ "derive", "env" ] }
|
||||
clap-verbosity-flag = "3.0.2"
|
||||
|
|
28
watt/Cargo.toml
Normal file
28
watt/Cargo.toml
Normal 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
|
|
@ -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<Self> {
|
||||
let contents = if let Some(path) = path {
|
|
@ -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);
|
||||
}
|
57
watt/main.rs
Normal file
57
watt/main.rs
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue