mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 14:26:58 +00:00
improvements
This commit is contained in:
parent
114e567826
commit
3a8ebca75e
4 changed files with 59 additions and 56 deletions
|
@ -76,8 +76,8 @@ pub fn build_cli() -> App<'static> {
|
|||
.long_help(
|
||||
r#"You can choose between these controller types:
|
||||
raw: uses the raw values found in the device files
|
||||
lin: uses percentage values (0.0 - 1.0) with a linear curve for the actual brightness
|
||||
log: uses percentage values (0.0 - 1.0) with a logarithmic curve for the actual brightness
|
||||
lin: uses percentage values (0.0 - 1.0) with a linear curve
|
||||
log: uses percentage values (0.0 - 1.0) with a logarithmic curve
|
||||
the perceived brightness for the human eyes should be linear with this controller
|
||||
"#,
|
||||
),
|
||||
|
|
|
@ -12,45 +12,39 @@ use std::process::exit;
|
|||
const SYS_PATHS: [&str; 2] = ["/sys/class/backlight", "/sys/class/leds"];
|
||||
|
||||
pub trait Controller {
|
||||
fn get_brightness(&self) -> i32;
|
||||
fn get_max_brightness(&self) -> i32;
|
||||
fn set_brightness(&self, value: i32);
|
||||
fn get_brightness(&self) -> u32;
|
||||
fn get_max_brightness(&self) -> u32;
|
||||
fn set_brightness(&self, value: u32);
|
||||
|
||||
fn check_brightness_value(&self, value: i32) {
|
||||
if value > self.get_max_brightness() {
|
||||
eprintln!(
|
||||
"brightness value too high: {} > {}",
|
||||
value,
|
||||
self.get_max_brightness()
|
||||
);
|
||||
exit(exitcode::DATAERR);
|
||||
} else if value < 0 {
|
||||
eprintln!("brightness value too low: {}", value);
|
||||
fn check_brightness_value(&self, value: u32) {
|
||||
let max = self.get_max_brightness();
|
||||
if value > max {
|
||||
eprintln!("brightness value too high: {value} > {max}",);
|
||||
exit(exitcode::DATAERR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RawController {
|
||||
path: Box<PathBuf>,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl RawController {
|
||||
pub fn new(path: Box<PathBuf>) -> Self {
|
||||
pub fn new(path: PathBuf) -> Self {
|
||||
Self { path }
|
||||
}
|
||||
}
|
||||
|
||||
impl Controller for RawController {
|
||||
fn get_brightness(&self) -> i32 {
|
||||
fn get_brightness(&self) -> u32 {
|
||||
read_file_to_int(self.path.join("brightness"))
|
||||
}
|
||||
|
||||
fn get_max_brightness(&self) -> i32 {
|
||||
fn get_max_brightness(&self) -> u32 {
|
||||
read_file_to_int(self.path.join("max_brightness"))
|
||||
}
|
||||
|
||||
fn set_brightness(&self, value: i32) {
|
||||
fn set_brightness(&self, value: u32) {
|
||||
self.check_brightness_value(value);
|
||||
|
||||
let path = self.path.join("brightness");
|
||||
|
@ -67,8 +61,7 @@ impl Controller for RawController {
|
|||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
"could not write '{}' to file '{}': {:?}",
|
||||
value,
|
||||
"could not write '{value}' to file '{}': {:?}",
|
||||
&path.display(),
|
||||
err.kind()
|
||||
);
|
||||
|
@ -83,7 +76,7 @@ pub struct LinController {
|
|||
}
|
||||
|
||||
impl LinController {
|
||||
pub fn new(path: Box<PathBuf>) -> Self {
|
||||
pub fn new(path: PathBuf) -> Self {
|
||||
Self {
|
||||
parent_controller: RawController::new(path),
|
||||
}
|
||||
|
@ -91,23 +84,22 @@ impl LinController {
|
|||
}
|
||||
|
||||
impl Controller for LinController {
|
||||
fn get_brightness(&self) -> i32 {
|
||||
fn get_brightness(&self) -> u32 {
|
||||
((self.parent_controller.get_brightness() as f64
|
||||
/ self.parent_controller.get_max_brightness() as f64)
|
||||
* self.get_max_brightness() as f64) as i32
|
||||
* self.get_max_brightness() as f64) as u32
|
||||
}
|
||||
|
||||
fn get_max_brightness(&self) -> i32 {
|
||||
fn get_max_brightness(&self) -> u32 {
|
||||
100
|
||||
}
|
||||
|
||||
fn set_brightness(&self, value: i32) {
|
||||
fn set_brightness(&self, value: u32) {
|
||||
self.check_brightness_value(value);
|
||||
|
||||
if value > self.get_max_brightness() {
|
||||
eprintln!(
|
||||
"brightness value too high! {} > {}",
|
||||
value,
|
||||
"brightness value too high! {value} > {}",
|
||||
self.get_max_brightness()
|
||||
);
|
||||
exit(exitcode::DATAERR);
|
||||
|
@ -124,7 +116,7 @@ pub struct LogController {
|
|||
}
|
||||
|
||||
impl LogController {
|
||||
pub fn new(path: Box<PathBuf>) -> Self {
|
||||
pub fn new(path: PathBuf) -> Self {
|
||||
Self {
|
||||
parent_controller: RawController::new(path),
|
||||
}
|
||||
|
@ -132,23 +124,22 @@ impl LogController {
|
|||
}
|
||||
|
||||
impl Controller for LogController {
|
||||
fn get_brightness(&self) -> i32 {
|
||||
fn get_brightness(&self) -> u32 {
|
||||
((self.parent_controller.get_brightness() as f64).log10()
|
||||
/ (self.parent_controller.get_max_brightness() as f64).log10()
|
||||
* self.get_max_brightness() as f64) as i32
|
||||
* self.get_max_brightness() as f64) as u32
|
||||
}
|
||||
|
||||
fn get_max_brightness(&self) -> i32 {
|
||||
fn get_max_brightness(&self) -> u32 {
|
||||
100
|
||||
}
|
||||
|
||||
fn set_brightness(&self, value: i32) {
|
||||
fn set_brightness(&self, value: u32) {
|
||||
self.check_brightness_value(value);
|
||||
|
||||
if value > self.get_max_brightness() {
|
||||
eprintln!(
|
||||
"brightness value too high! {} > {}",
|
||||
value,
|
||||
"brightness value too high! {value} > {}",
|
||||
self.get_max_brightness()
|
||||
);
|
||||
exit(exitcode::DATAERR);
|
||||
|
@ -157,11 +148,11 @@ impl Controller for LogController {
|
|||
self.parent_controller.set_brightness(10f64.powf(
|
||||
(value as f64 / self.get_max_brightness() as f64)
|
||||
* (self.parent_controller.get_max_brightness() as f64).log10(),
|
||||
) as i32)
|
||||
) as u32)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_file_to_int(path: PathBuf) -> i32 {
|
||||
fn read_file_to_int(path: PathBuf) -> u32 {
|
||||
let mut file = match File::open(&path) {
|
||||
Err(why) => {
|
||||
eprintln!("couldn't open {}: {:?}", path.display(), why.kind());
|
||||
|
@ -183,8 +174,8 @@ fn read_file_to_int(path: PathBuf) -> i32 {
|
|||
/// Searches through all paths in `SYS_PATHS` and creates a `HashMap` with the name and absolute path.
|
||||
///
|
||||
/// It returns a `Tuple` of the default backlight name and the `HashMap`.
|
||||
pub fn get_controllers() -> (String, HashMap<String, Box<PathBuf>>) {
|
||||
let mut controllers: HashMap<String, Box<PathBuf>> = HashMap::new();
|
||||
pub fn get_controllers() -> (String, HashMap<String, PathBuf>) {
|
||||
let mut controllers: HashMap<String, PathBuf> = HashMap::new();
|
||||
|
||||
let mut default = None;
|
||||
|
||||
|
@ -198,7 +189,7 @@ pub fn get_controllers() -> (String, HashMap<String, Box<PathBuf>>) {
|
|||
default = Some(key.clone());
|
||||
}
|
||||
|
||||
controllers.insert(key, Box::new(name));
|
||||
controllers.insert(key, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -23,22 +23,22 @@ fn main() {
|
|||
Some("raw") => Box::new(RawController::new(p)),
|
||||
Some("lin") => Box::new(LinController::new(p)),
|
||||
Some("log") => Box::new(LogController::new(p)),
|
||||
Some(_) | None => panic!("{}", ERROR_MSG),
|
||||
Some(_) | None => panic!("{ERROR_MSG}"),
|
||||
};
|
||||
|
||||
if matches.is_present("list") {
|
||||
for ctrl in ctrls.keys() {
|
||||
println!("{}", ctrl);
|
||||
println!("{ctrl}");
|
||||
}
|
||||
exit(exitcode::OK);
|
||||
} else if let Some(value) = matches.value_of("set") {
|
||||
let new_value = value.parse::<i32>().unwrap();
|
||||
let new_value = value.parse::<u32>().unwrap();
|
||||
controller.set_brightness(new_value);
|
||||
} else if let Some(value) = matches.value_of("inc") {
|
||||
let new_value = controller.get_brightness() + value.parse::<i32>().unwrap();
|
||||
let new_value = controller.get_brightness() + value.parse::<u32>().unwrap();
|
||||
controller.set_brightness(new_value.min(controller.get_max_brightness()));
|
||||
} else if let Some(value) = matches.value_of("dec") {
|
||||
let new_value = controller.get_brightness() - value.parse::<i32>().unwrap();
|
||||
let new_value = controller.get_brightness() - value.parse::<u32>().unwrap();
|
||||
controller.set_brightness(new_value.max(0));
|
||||
} else if matches.is_present("get") {
|
||||
println!("{}", controller.get_brightness());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue