improvements

This commit is contained in:
crapStone 2022-07-16 01:01:48 +02:00
parent 114e567826
commit 3a8ebca75e
No known key found for this signature in database
GPG key ID: 4CAA9E39EEDEB1F0
4 changed files with 59 additions and 56 deletions

View file

@ -3,26 +3,38 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{path::PathBuf, io::Error};
use std::{
fs,
io::{self, Error},
path::PathBuf,
};
use clap_complete::{generate_to, shells, Generator};
include!("src/cli.rs");
const OUTDIR: &str = "completions";
fn main() -> Result<(), Error> {
let outdir = "completions";
let mut cmd = build_cli();
let path = generate_completions(shells::Bash, &mut cmd, outdir)?;
println!("cargo:debug=completion file is generated: {:?}", path);
let path = generate_completions(shells::Zsh, &mut cmd, outdir)?;
println!("cargo:debug=completion file is generated: {:?}", path);
let path = generate_completions(shells::Fish, &mut cmd, outdir)?;
println!("cargo:debug=completion file is generated: {:?}", path);
if let Err(why) = fs::create_dir(OUTDIR) {
if why.kind() != io::ErrorKind::AlreadyExists {
eprintln!("cargo:error=could not create directory: {OUTDIR}");
return Err(why);
}
}
let path = generate_completions(shells::Bash, &mut cmd)?;
println!("cargo:debug=completion file is generated: {path:?}");
let path = generate_completions(shells::Zsh, &mut cmd)?;
println!("cargo:debug=completion file is generated: {path:?}");
let path = generate_completions(shells::Fish, &mut cmd)?;
println!("cargo:debug=completion file is generated: {path:?}");
Ok(())
}
fn generate_completions<G: Generator>(shell: G, cmd: &mut clap::Command, outdir: &str) -> Result<PathBuf, Error> {
generate_to(shell, cmd, "lamp", outdir)
fn generate_completions<G: Generator>(shell: G, cmd: &mut clap::Command) -> Result<PathBuf, Error> {
generate_to(shell, cmd, env!("CARGO_PKG_NAME"), OUTDIR)
}

View file

@ -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
"#,
),

View file

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

View file

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