From f4a126c8ef0c27ad022fcf98d1aa5da828c6177f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 23 Oct 2022 14:37:12 +0200 Subject: [PATCH] Add a `port_name()` function to `kaleidoscope_focus::Focus` The function returns the name of the serial port Focus is connected to, if available. Also updated the command-line tools to make use of this information, and display the port name on the progress indicator. Signed-off-by: Gergely Nagy --- CHANGELOG.md | 6 +++++- kaleidoscope-focus-cli/src/shared.rs | 16 ++++++++++++++-- kaleidoscope-focus/src/lib.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff67150..dba84c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -_No changes yet._ +### Added +- Added a `port_name()` function to `kaleidoscope_focus::Focus`, which returns + the name of the serial port it is connected to, if available. +- Updated the command-line tools to make use of it, and display the port name on + the progress indicator. ## [0.1.0] - 2022-10-23 diff --git a/kaleidoscope-focus-cli/src/shared.rs b/kaleidoscope-focus-cli/src/shared.rs index 17dc721..6c205af 100644 --- a/kaleidoscope-focus-cli/src/shared.rs +++ b/kaleidoscope-focus-cli/src/shared.rs @@ -75,6 +75,12 @@ impl Cli { } pub fn send(&mut self, command: &str, args: &[String]) -> Result<()> { + self.progress.set_prefix(format!( + "sending `{}` (to {}): ", + &command, + &self.conn.port_name().unwrap() + )); + let reply = self.conn.flush()?.request(command, Some(args))?; self.progress.finish_and_clear(); @@ -96,7 +102,10 @@ impl Cli { } pub fn backup(&mut self) -> Result<()> { - self.progress.set_prefix("backing up: "); + self.progress.set_prefix(format!( + "backing up (from {}): ", + &self.conn.port_name().unwrap() + )); let reply = self.conn.flush()?.command("backup")?; @@ -171,7 +180,10 @@ impl Cli { let backup: BackupData = serde_json::from_reader(io::stdin()).expect("Unable to parse the backup"); - self.progress.set_prefix("restoring: "); + self.progress.set_prefix(format!( + "restoring (to {}): ", + &self.conn.port_name().unwrap() + )); for k in &backup.restore { self.progress.set_message(k.clone()); diff --git a/kaleidoscope-focus/src/lib.rs b/kaleidoscope-focus/src/lib.rs index 5f437d2..7290f8a 100644 --- a/kaleidoscope-focus/src/lib.rs +++ b/kaleidoscope-focus/src/lib.rs @@ -233,6 +233,20 @@ impl Focus { Ok(self) } + /// Return the port name - if known - of the connected device. + /// + /// ```no_run + /// # use kaleidoscope_focus::Focus; + /// # fn main() -> Result<(), std::io::Error> { + /// let mut conn = Focus::create("/dev/ttyACM0").open()?; + /// assert_eq!(conn.port_name(), Some("/dev/ttyACM0".to_string())); + /// # Ok(()) + /// # } + /// ``` + pub fn port_name(&self) -> Option { + self.port.name() + } + /// Find supported devices, and return the paths to their ports. /// /// Iterates over available USB serial ports, and keeps only those that belong