From 620ba2bbe8119b180b5672855b0a9bf31a806d8f Mon Sep 17 00:00:00 2001
From: crapStone <crapstone@noreply.codeberg.org>
Date: Sat, 16 Jul 2022 14:37:12 +0200
Subject: [PATCH] Add cli option to select device to control (#6)

Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/crapStone/lamp/pulls/6
---
 README.md   |  3 ++-
 src/cli.rs  | 22 +++++++++++++++-------
 src/main.rs | 15 ++++++++++++++-
 3 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 6764663..bd40c09 100644
--- a/README.md
+++ b/README.md
@@ -9,12 +9,13 @@ In contrast to acpilight lamp is not backwards compatible with xbacklight.
 It is intended to be used as a standalone replacement for new scripts.
 
 ```none
+    -c, --controller <DEVICE>       Select device to control
     -d, --decrease <PERCENT>        Decreases brightness
     -f, --full                      Sets brightness to highest value
     -g, --get                       Prints current brightness value
     -h, --help                      Print help information
     -i, --increase <PERCENT>        Increases brightness
-    -l, --list                      Lists all available brightness and led controllers
+    -l, --list                      Lists all devices with controllable brightness and led values
     -s, --set <VALUE>               Sets brightness to given value
     -t, --type <controller_type>    choose controller type [default: lin] [possible values: raw,
                                     lin, log]
diff --git a/src/cli.rs b/src/cli.rs
index 6531916..1d250b8 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -15,25 +15,25 @@ pub fn build_cli() -> App<'static> {
             Arg::with_name("set")
                 .short('s')
                 .long("set")
-                .value_name("VALUE")
                 .help("Sets brightness to given value")
-                .takes_value(true),
+                .takes_value(true)
+                .value_name("VALUE"),
         )
         .arg(
             Arg::with_name("inc")
                 .short('i')
                 .long("increase")
-                .value_name("PERCENT")
                 .help("Increases brightness")
-                .takes_value(true),
+                .takes_value(true)
+                .value_name("PERCENT"),
         )
         .arg(
             Arg::with_name("dec")
                 .short('d')
                 .long("decrease")
-                .value_name("PERCENT")
                 .help("Decreases brightness")
-                .takes_value(true),
+                .takes_value(true)
+                .value_name("PERCENT"),
         )
         .arg(
             Arg::with_name("get")
@@ -57,9 +57,17 @@ pub fn build_cli() -> App<'static> {
             Arg::with_name("list")
                 .short('l')
                 .long("list")
-                .help("Lists all available brightness and led controllers")
+                .help("Lists all devices with controllable brightness and led values")
                 .exclusive(true),
         )
+        .arg(
+            Arg::with_name("controller")
+                .short('c')
+                .long("controller")
+                .help("Select device to control")
+                .value_name("DEVICE")
+                .takes_value(true),
+        )
         .arg(
             Arg::with_name("ctrl_type")
                 .short('t')
diff --git a/src/main.rs b/src/main.rs
index aa37e9f..45a2d0a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,20 @@ fn main() {
 
     let (default_ctrl, ctrls) = controllers::get_controllers();
 
-    let p = ctrls.get(&default_ctrl).unwrap().to_owned();
+    let p = match matches.value_of("controller") {
+        Some(ctrl) => {
+            let p = ctrls.get(ctrl);
+            if p == None {
+                eprintln!("no device with name '{ctrl}' found");
+                eprintln!("use --list to ge a list of all available devices");
+                exit(exitcode::DATAERR);
+            }
+
+            p.unwrap().to_owned()
+        }
+        None => ctrls.get(&default_ctrl).unwrap().to_owned(),
+    };
+
     let controller: Box<dyn Controller> = match matches.value_of("ctrl_type") {
         Some("raw") => Box::new(RawController::new(p)),
         Some("lin") => Box::new(LinController::new(p)),