Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 149 additions & 6 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use ratatui::{
Frame,
layout::{Alignment, Constraint, Direction, Flex, Layout},
style::{Color, Style, Stylize},
text::Line,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Cell, Clear, List, Padding, Row, Table, TableState},
};
use tokio::sync::mpsc::UnboundedSender;

use crate::{
app::{AppResult, ColorMode, FocusedBlock},
config::Config,
device::Device,
event::Event,
};
Expand All @@ -27,10 +28,15 @@ pub struct Adapter {
pub vendor: Option<String>,
pub supported_modes: Vec<String>,
pub device: Device,
pub config: Arc<Config>,
}

impl Adapter {
pub async fn new(session: Arc<Session>, sender: UnboundedSender<Event>) -> AppResult<Self> {
pub async fn new(
session: Arc<Session>,
sender: UnboundedSender<Event>,
config: Arc<Config>,
) -> AppResult<Self> {
let adapter = session.adapter().context("No adapter found")?;

let is_powered = adapter.is_powered().await?;
Expand All @@ -48,6 +54,7 @@ impl Adapter {
vendor,
supported_modes,
device,
config,
})
}

Expand Down Expand Up @@ -82,25 +89,27 @@ impl Adapter {
None => false,
};

let (device_block, access_point_block, connected_devices_block) = {
let (device_block, access_point_block, connected_devices_block, help_block) = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(if any_connected_devices {
&[
Constraint::Percentage(33),
Constraint::Percentage(33),
Constraint::Percentage(33),
Constraint::Length(1),
]
} else {
&[
Constraint::Percentage(50),
Constraint::Percentage(50),
Constraint::Fill(1),
Constraint::Length(1),
]
})
.margin(1)
.split(frame.area());
(chunks[0], chunks[1], chunks[2])
(chunks[0], chunks[1], chunks[2], chunks[3])
};

// Device
Expand Down Expand Up @@ -423,6 +432,46 @@ impl Adapter {

frame.render_widget(connected_devices_list, connected_devices_block);
}

let help_message = match focused_block {
FocusedBlock::Device => Line::from(vec![
Span::from(self.config.device.infos.to_string()).bold(),
Span::from(" Infos"),
Span::from(" | "),
Span::from(self.config.device.toggle_power.to_string()).bold(),
Span::from(" Toggle Power"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::AdapterInfos | FocusedBlock::AccessPointInput => Line::from(vec![
Span::from("󱊷 ").bold(),
Span::from(" Discard"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::AccessPoint => Line::from(vec![
Span::from(self.config.ap.start.to_string()).bold(),
Span::from(" New AP"),
Span::from(" | "),
Span::from(self.config.ap.stop.to_string()).bold(),
Span::from(" Stop AP"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
_ => Line::from(""),
};

let help_message = help_message.centered().blue();
frame.render_widget(help_message, help_block);
}

pub fn render_station_mode(
Expand All @@ -431,18 +480,19 @@ impl Adapter {
color_mode: ColorMode,
focused_block: FocusedBlock,
) {
let (device_block, station_block, known_networks_block, new_networks_block) = {
let (device_block, station_block, known_networks_block, new_networks_block, help_block) = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(5),
Constraint::Length(5),
Constraint::Min(5),
Constraint::Min(5),
Constraint::Length(1),
])
.margin(1)
.split(frame.area());
(chunks[0], chunks[1], chunks[2], chunks[3])
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4])
};

// Device
Expand Down Expand Up @@ -984,6 +1034,99 @@ impl Adapter {
new_networks_block,
&mut new_networks_state,
);

let help_message = match focused_block {
FocusedBlock::Device => Line::from(vec![
Span::from(self.config.station.start_scanning.to_string()).bold(),
Span::from(" Scan"),
Span::from(" | "),
Span::from(self.config.device.infos.to_string()).bold(),
Span::from(" Infos"),
Span::from(" | "),
Span::from(self.config.device.toggle_power.to_string()).bold(),
Span::from(" Toggle Power"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::Station => Line::from(vec![
Span::from(self.config.station.start_scanning.to_string()).bold(),
Span::from(" Scan"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::KnownNetworks => Line::from(vec![
Span::from("k,").bold(),
Span::from(" Up"),
Span::from(" | "),
Span::from("j,").bold(),
Span::from(" Down"),
Span::from(" | "),
Span::from(if self.config.station.toggle_connect == ' ' {
"󱁐 ".to_string()
} else {
self.config.station.toggle_connect.to_string()
})
.bold(),
Span::from(" Connect/Disconnect"),
Span::from(" | "),
Span::from(self.config.station.known_network.remove.to_string()).bold(),
Span::from(" Remove"),
Span::from(" | "),
Span::from(
self.config
.station
.known_network
.toggle_autoconnect
.to_string(),
)
.bold(),
Span::from(" Toggle Autoconnect"),
Span::from(" | "),
Span::from("󱊷 ").bold(),
Span::from(" Discard"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::NewNetworks => Line::from(vec![
Span::from("k,").bold(),
Span::from(" Up"),
Span::from(" | "),
Span::from("j,").bold(),
Span::from(" Down"),
Span::from(" | "),
Span::from("󱁐 ").bold(),
Span::from(" Connect"),
Span::from(" | "),
Span::from("󱊷 ").bold(),
Span::from(" Discard"),
Span::from(" | "),
Span::from("ctrl+r").bold(),
Span::from(" Switch Mode"),
Span::from(" | "),
Span::from("⇄").bold(),
Span::from(" Nav"),
]),
FocusedBlock::AdapterInfos | FocusedBlock::AuthKey => {
Line::from(vec![Span::from("󱊷 ").bold(), Span::from(" Discard")])
}
_ => Line::from(""),
};

let help_message = help_message.centered().blue();

frame.render_widget(help_message, help_block);
}

pub fn render_adapter(&self, frame: &mut Frame, color_mode: ColorMode) {
Expand Down
File renamed without changes.
21 changes: 13 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use async_channel::{Receiver, Sender};
use futures::FutureExt;
use iwdrs::{agent::Agent, modes::Mode, session::Session};

use crate::{adapter::Adapter, event::Event, help::Help, notification::Notification};
use crate::{adapter::Adapter, config::Config, event::Event, notification::Notification};

pub type AppResult<T> = std::result::Result<T, Box<dyn Error>>;

Expand All @@ -29,7 +29,6 @@ pub enum FocusedBlock {
AccessPoint,
KnownNetworks,
NewNetworks,
Help,
AuthKey,
AdapterInfos,
AccessPointInput,
Expand All @@ -46,7 +45,6 @@ pub enum ColorMode {
pub struct App {
pub running: bool,
pub focused_block: FocusedBlock,
pub help: Help,
pub color_mode: ColorMode,
pub notifications: Vec<Notification>,
pub session: Arc<Session>,
Expand Down Expand Up @@ -91,7 +89,11 @@ pub async fn request_confirmation(
}

impl App {
pub async fn new(help: Help, mode: Mode, sender: UnboundedSender<Event>) -> AppResult<Self> {
pub async fn new(
config: Arc<Config>,
mode: Mode,
sender: UnboundedSender<Event>,
) -> AppResult<Self> {
let session = {
match iwdrs::session::Session::new().await {
Ok(session) => Arc::new(session),
Expand All @@ -102,7 +104,7 @@ impl App {
}
};

let adapter = match Adapter::new(session.clone(), sender).await {
let adapter = match Adapter::new(session.clone(), sender, config).await {
Ok(v) => v,
Err(e) => {
eprintln!("{e}");
Expand Down Expand Up @@ -144,7 +146,6 @@ impl App {
Ok(Self {
running: true,
focused_block: FocusedBlock::Device,
help,
color_mode,
notifications: Vec::new(),
session,
Expand All @@ -162,15 +163,19 @@ impl App {
})
}

pub async fn reset(mode: Mode, sender: UnboundedSender<Event>) -> AppResult<()> {
pub async fn reset(
mode: Mode,
sender: UnboundedSender<Event>,
config: Arc<Config>,
) -> AppResult<()> {
let session = {
match iwdrs::session::Session::new().await {
Ok(session) => Arc::new(session),
Err(e) => return Err(anyhow!("Can not access the iwd service: {}", e).into()),
}
};

let adapter = match Adapter::new(session.clone(), sender).await {
let adapter = match Adapter::new(session.clone(), sender, config).await {
Ok(v) => v,
Err(e) => {
eprintln!("{e}");
Expand Down
3 changes: 1 addition & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use iwdrs::{device::Device as iwdDevice, modes::Mode, session::Session};
use tokio::sync::mpsc::UnboundedSender;

use crate::{
access_point::AccessPoint, app::AppResult, event::Event, notification::Notification,
station::Station,
ap::AccessPoint, app::AppResult, event::Event, notification::Notification, station::Station,
};

#[derive(Debug, Clone)]
Expand Down
Loading