Skip to content

Commit

Permalink
feat: Display addresses in the peripherals list (#17)
Browse files Browse the repository at this point in the history
* feat: Display addresses in the peripherals list

* Do not display default bd addresses

And also improves the colors for displaying

---------

Co-authored-by: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com>
  • Loading branch information
dzamlo and dmtrKovalenko authored Nov 26, 2023
1 parent 36db4e2 commit da4b00e
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions src/tui/peripheral_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::tui::ui::{HandleInputResult, StableIndexList};
use crate::tui::{AppRoute, HandleKeydownResult};
use crate::GeneralOptions;
use crate::{route::Route, Ctx};
use btleplug::api::BDAddr;
use btleplug::platform::PeripheralId;
use crossterm::event::{KeyCode, KeyEvent};
use regex::Regex;
Expand All @@ -21,6 +22,10 @@ use tui::{
Frame,
};

lazy_static::lazy_static! {
static ref DEFAULT_BD_ADDR: BDAddr = BDAddr::default();
}

pub enum Focus {
Search,
List,
Expand Down Expand Up @@ -241,16 +246,52 @@ impl AppRoute for PeripheralList {
.enumerate()
.map(|(i, peripheral)| {
let is_highlighted = Some(i) == self.list_state.selected();
ListItem::new(Span::from(format!(
"{}{}{}",
if is_highlighted { "> " } else { " " },
peripheral.name,
match peripheral.rssi {
Some(rssi) => format!(" (rssi {rssi})"),
None => String::from(""),
}
)))
.style(Style::default().fg(Color::Gray))
let mut spans = vec![];
if is_highlighted {
spans.push(Span::styled("> ", Style::default().fg(Color::Blue)));
} else {
spans.push(Span::from(" "));
}

spans.push(Span::styled(
peripheral.name.as_str(),
if is_highlighted {
Style::default().fg(Color::White)
} else {
Style::default()
},
));

let address_info = if peripheral.address != *DEFAULT_BD_ADDR {
peripheral.address.to_string()
} else {
"".to_string()
};

let rssi_info = if let Some(rssi) = peripheral.rssi {
format!("rssi {}", rssi)
} else {
"".to_string()
};

let additional_info_params = vec![address_info, rssi_info]
.into_iter()
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();

if !additional_info_params.is_empty() {
let style = if is_highlighted {
Style::default()
} else {
Style::default().add_modifier(Modifier::DIM)
};

spans.push(Span::styled(" (", style));
spans.push(Span::styled(additional_info_params.join(", "), style));
spans.push(Span::styled(")", style));
}

ListItem::new(Line::from(spans))
})
.collect();

Expand Down

0 comments on commit da4b00e

Please sign in to comment.