mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 08:57:55 +00:00
improvements
This commit is contained in:
parent
114e567826
commit
3a8ebca75e
4 changed files with 59 additions and 56 deletions
32
build.rs
32
build.rs
|
@ -3,26 +3,38 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// 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};
|
use clap_complete::{generate_to, shells, Generator};
|
||||||
|
|
||||||
include!("src/cli.rs");
|
include!("src/cli.rs");
|
||||||
|
|
||||||
|
const OUTDIR: &str = "completions";
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
let outdir = "completions";
|
|
||||||
let mut cmd = build_cli();
|
let mut cmd = build_cli();
|
||||||
|
|
||||||
let path = generate_completions(shells::Bash, &mut cmd, outdir)?;
|
if let Err(why) = fs::create_dir(OUTDIR) {
|
||||||
println!("cargo:debug=completion file is generated: {:?}", path);
|
if why.kind() != io::ErrorKind::AlreadyExists {
|
||||||
let path = generate_completions(shells::Zsh, &mut cmd, outdir)?;
|
eprintln!("cargo:error=could not create directory: {OUTDIR}");
|
||||||
println!("cargo:debug=completion file is generated: {:?}", path);
|
return Err(why);
|
||||||
let path = generate_completions(shells::Fish, &mut cmd, outdir)?;
|
}
|
||||||
println!("cargo:debug=completion file is generated: {:?}", path);
|
}
|
||||||
|
|
||||||
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_completions<G: Generator>(shell: G, cmd: &mut clap::Command, outdir: &str) -> Result<PathBuf, Error> {
|
fn generate_completions<G: Generator>(shell: G, cmd: &mut clap::Command) -> Result<PathBuf, Error> {
|
||||||
generate_to(shell, cmd, "lamp", outdir)
|
generate_to(shell, cmd, env!("CARGO_PKG_NAME"), OUTDIR)
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,8 +76,8 @@ pub fn build_cli() -> App<'static> {
|
||||||
.long_help(
|
.long_help(
|
||||||
r#"You can choose between these controller types:
|
r#"You can choose between these controller types:
|
||||||
raw: uses the raw values found in the device files
|
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
|
lin: uses percentage values (0.0 - 1.0) with a linear curve
|
||||||
log: uses percentage values (0.0 - 1.0) with a logarithmic curve for the actual brightness
|
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
|
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"];
|
const SYS_PATHS: [&str; 2] = ["/sys/class/backlight", "/sys/class/leds"];
|
||||||
|
|
||||||
pub trait Controller {
|
pub trait Controller {
|
||||||
fn get_brightness(&self) -> i32;
|
fn get_brightness(&self) -> u32;
|
||||||
fn get_max_brightness(&self) -> i32;
|
fn get_max_brightness(&self) -> u32;
|
||||||
fn set_brightness(&self, value: i32);
|
fn set_brightness(&self, value: u32);
|
||||||
|
|
||||||
fn check_brightness_value(&self, value: i32) {
|
fn check_brightness_value(&self, value: u32) {
|
||||||
if value > self.get_max_brightness() {
|
let max = self.get_max_brightness();
|
||||||
eprintln!(
|
if value > max {
|
||||||
"brightness value too high: {} > {}",
|
eprintln!("brightness value too high: {value} > {max}",);
|
||||||
value,
|
|
||||||
self.get_max_brightness()
|
|
||||||
);
|
|
||||||
exit(exitcode::DATAERR);
|
|
||||||
} else if value < 0 {
|
|
||||||
eprintln!("brightness value too low: {}", value);
|
|
||||||
exit(exitcode::DATAERR);
|
exit(exitcode::DATAERR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RawController {
|
pub struct RawController {
|
||||||
path: Box<PathBuf>,
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawController {
|
impl RawController {
|
||||||
pub fn new(path: Box<PathBuf>) -> Self {
|
pub fn new(path: PathBuf) -> Self {
|
||||||
Self { path }
|
Self { path }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller for RawController {
|
impl Controller for RawController {
|
||||||
fn get_brightness(&self) -> i32 {
|
fn get_brightness(&self) -> u32 {
|
||||||
read_file_to_int(self.path.join("brightness"))
|
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"))
|
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);
|
self.check_brightness_value(value);
|
||||||
|
|
||||||
let path = self.path.join("brightness");
|
let path = self.path.join("brightness");
|
||||||
|
@ -67,8 +61,7 @@ impl Controller for RawController {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"could not write '{}' to file '{}': {:?}",
|
"could not write '{value}' to file '{}': {:?}",
|
||||||
value,
|
|
||||||
&path.display(),
|
&path.display(),
|
||||||
err.kind()
|
err.kind()
|
||||||
);
|
);
|
||||||
|
@ -83,7 +76,7 @@ pub struct LinController {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LinController {
|
impl LinController {
|
||||||
pub fn new(path: Box<PathBuf>) -> Self {
|
pub fn new(path: PathBuf) -> Self {
|
||||||
Self {
|
Self {
|
||||||
parent_controller: RawController::new(path),
|
parent_controller: RawController::new(path),
|
||||||
}
|
}
|
||||||
|
@ -91,23 +84,22 @@ impl LinController {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller for 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_brightness() as f64
|
||||||
/ self.parent_controller.get_max_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
|
100
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_brightness(&self, value: i32) {
|
fn set_brightness(&self, value: u32) {
|
||||||
self.check_brightness_value(value);
|
self.check_brightness_value(value);
|
||||||
|
|
||||||
if value > self.get_max_brightness() {
|
if value > self.get_max_brightness() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"brightness value too high! {} > {}",
|
"brightness value too high! {value} > {}",
|
||||||
value,
|
|
||||||
self.get_max_brightness()
|
self.get_max_brightness()
|
||||||
);
|
);
|
||||||
exit(exitcode::DATAERR);
|
exit(exitcode::DATAERR);
|
||||||
|
@ -124,7 +116,7 @@ pub struct LogController {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LogController {
|
impl LogController {
|
||||||
pub fn new(path: Box<PathBuf>) -> Self {
|
pub fn new(path: PathBuf) -> Self {
|
||||||
Self {
|
Self {
|
||||||
parent_controller: RawController::new(path),
|
parent_controller: RawController::new(path),
|
||||||
}
|
}
|
||||||
|
@ -132,23 +124,22 @@ impl LogController {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller for 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_brightness() as f64).log10()
|
||||||
/ (self.parent_controller.get_max_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
|
100
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_brightness(&self, value: i32) {
|
fn set_brightness(&self, value: u32) {
|
||||||
self.check_brightness_value(value);
|
self.check_brightness_value(value);
|
||||||
|
|
||||||
if value > self.get_max_brightness() {
|
if value > self.get_max_brightness() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"brightness value too high! {} > {}",
|
"brightness value too high! {value} > {}",
|
||||||
value,
|
|
||||||
self.get_max_brightness()
|
self.get_max_brightness()
|
||||||
);
|
);
|
||||||
exit(exitcode::DATAERR);
|
exit(exitcode::DATAERR);
|
||||||
|
@ -157,11 +148,11 @@ impl Controller for LogController {
|
||||||
self.parent_controller.set_brightness(10f64.powf(
|
self.parent_controller.set_brightness(10f64.powf(
|
||||||
(value as f64 / self.get_max_brightness() as f64)
|
(value as f64 / self.get_max_brightness() as f64)
|
||||||
* (self.parent_controller.get_max_brightness() as f64).log10(),
|
* (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) {
|
let mut file = match File::open(&path) {
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
eprintln!("couldn't open {}: {:?}", path.display(), why.kind());
|
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.
|
/// 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`.
|
/// It returns a `Tuple` of the default backlight name and the `HashMap`.
|
||||||
pub fn get_controllers() -> (String, HashMap<String, Box<PathBuf>>) {
|
pub fn get_controllers() -> (String, HashMap<String, PathBuf>) {
|
||||||
let mut controllers: HashMap<String, Box<PathBuf>> = HashMap::new();
|
let mut controllers: HashMap<String, PathBuf> = HashMap::new();
|
||||||
|
|
||||||
let mut default = None;
|
let mut default = None;
|
||||||
|
|
||||||
|
@ -198,7 +189,7 @@ pub fn get_controllers() -> (String, HashMap<String, Box<PathBuf>>) {
|
||||||
default = Some(key.clone());
|
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("raw") => Box::new(RawController::new(p)),
|
||||||
Some("lin") => Box::new(LinController::new(p)),
|
Some("lin") => Box::new(LinController::new(p)),
|
||||||
Some("log") => Box::new(LogController::new(p)),
|
Some("log") => Box::new(LogController::new(p)),
|
||||||
Some(_) | None => panic!("{}", ERROR_MSG),
|
Some(_) | None => panic!("{ERROR_MSG}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.is_present("list") {
|
if matches.is_present("list") {
|
||||||
for ctrl in ctrls.keys() {
|
for ctrl in ctrls.keys() {
|
||||||
println!("{}", ctrl);
|
println!("{ctrl}");
|
||||||
}
|
}
|
||||||
exit(exitcode::OK);
|
exit(exitcode::OK);
|
||||||
} else if let Some(value) = matches.value_of("set") {
|
} 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);
|
controller.set_brightness(new_value);
|
||||||
} else if let Some(value) = matches.value_of("inc") {
|
} 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()));
|
controller.set_brightness(new_value.min(controller.get_max_brightness()));
|
||||||
} else if let Some(value) = matches.value_of("dec") {
|
} 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));
|
controller.set_brightness(new_value.max(0));
|
||||||
} else if matches.is_present("get") {
|
} else if matches.is_present("get") {
|
||||||
println!("{}", controller.get_brightness());
|
println!("{}", controller.get_brightness());
|
||||||
|
|
Loading…
Reference in a new issue