Skip to content

Commit 74b9079

Browse files
committed
add help message for station
1 parent 526a785 commit 74b9079

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

src/adapter.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{fmt::format, sync::Arc};
22

33
use anyhow::Context;
44

@@ -7,13 +7,14 @@ use ratatui::{
77
Frame,
88
layout::{Alignment, Constraint, Direction, Flex, Layout},
99
style::{Color, Style, Stylize},
10-
text::Line,
10+
text::{Line, Text},
1111
widgets::{Block, BorderType, Borders, Cell, Clear, List, Padding, Row, Table, TableState},
1212
};
1313
use tokio::sync::mpsc::UnboundedSender;
1414

1515
use crate::{
1616
app::{AppResult, ColorMode, FocusedBlock},
17+
config::Config,
1718
device::Device,
1819
event::Event,
1920
};
@@ -27,10 +28,15 @@ pub struct Adapter {
2728
pub vendor: Option<String>,
2829
pub supported_modes: Vec<String>,
2930
pub device: Device,
31+
pub config: Arc<Config>,
3032
}
3133

3234
impl Adapter {
33-
pub async fn new(session: Arc<Session>, sender: UnboundedSender<Event>) -> AppResult<Self> {
35+
pub async fn new(
36+
session: Arc<Session>,
37+
sender: UnboundedSender<Event>,
38+
config: Arc<Config>,
39+
) -> AppResult<Self> {
3440
let adapter = session.adapter().context("No adapter found")?;
3541

3642
let is_powered = adapter.is_powered().await?;
@@ -48,6 +54,7 @@ impl Adapter {
4854
vendor,
4955
supported_modes,
5056
device,
57+
config,
5158
})
5259
}
5360

@@ -431,18 +438,19 @@ impl Adapter {
431438
color_mode: ColorMode,
432439
focused_block: FocusedBlock,
433440
) {
434-
let (device_block, station_block, known_networks_block, new_networks_block) = {
441+
let (device_block, station_block, known_networks_block, new_networks_block, help_block) = {
435442
let chunks = Layout::default()
436443
.direction(Direction::Vertical)
437444
.constraints([
438445
Constraint::Length(5),
439446
Constraint::Length(5),
440447
Constraint::Min(5),
441448
Constraint::Min(5),
449+
Constraint::Length(3),
442450
])
443451
.margin(1)
444452
.split(frame.area());
445-
(chunks[0], chunks[1], chunks[2], chunks[3])
453+
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4])
446454
};
447455

448456
// Device
@@ -984,6 +992,39 @@ impl Adapter {
984992
new_networks_block,
985993
&mut new_networks_state,
986994
);
995+
996+
let help_message = match focused_block {
997+
FocusedBlock::Device => {
998+
format!(
999+
"⇄: Nav | {}: Scan | {}: Show information | {}: Toggle power",
1000+
self.config.station.start_scanning,
1001+
self.config.device.infos,
1002+
self.config.device.toggle_power
1003+
)
1004+
}
1005+
FocusedBlock::Station => format!(
1006+
"⇄: Nav | {}: Start Scanning",
1007+
self.config.station.start_scanning
1008+
),
1009+
FocusedBlock::KnownNetworks => format!(
1010+
"k,↑: Up | j,↓: Down | ⇄: Nav | {}: Connect/Disconnect | {}: Remove | {}: Toggle autoconnect",
1011+
if self.config.station.toggle_connect == ' ' {
1012+
"Space"
1013+
} else {
1014+
&self.config.station.toggle_connect.to_string()
1015+
},
1016+
self.config.station.known_network.remove,
1017+
self.config.station.known_network.toggle_autoconnect
1018+
),
1019+
FocusedBlock::NewNetworks => {
1020+
"k,↑: Up | j,↓: Down | ⇄: Nav | Space: Connect".to_string()
1021+
}
1022+
_ => "".to_string(),
1023+
};
1024+
1025+
let help_message = Text::from(help_message).centered().bold().blue();
1026+
1027+
frame.render_widget(help_message, help_block);
9871028
}
9881029

9891030
pub fn render_adapter(&self, frame: &mut Frame, color_mode: ColorMode) {

src/app.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use async_channel::{Receiver, Sender};
1818
use futures::FutureExt;
1919
use iwdrs::{agent::Agent, modes::Mode, session::Session};
2020

21-
use crate::{adapter::Adapter, event::Event, notification::Notification};
21+
use crate::{adapter::Adapter, config::Config, event::Event, notification::Notification};
2222

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

@@ -89,7 +89,11 @@ pub async fn request_confirmation(
8989
}
9090

9191
impl App {
92-
pub async fn new(mode: Mode, sender: UnboundedSender<Event>) -> AppResult<Self> {
92+
pub async fn new(
93+
config: Arc<Config>,
94+
mode: Mode,
95+
sender: UnboundedSender<Event>,
96+
) -> AppResult<Self> {
9397
let session = {
9498
match iwdrs::session::Session::new().await {
9599
Ok(session) => Arc::new(session),
@@ -100,7 +104,7 @@ impl App {
100104
}
101105
};
102106

103-
let adapter = match Adapter::new(session.clone(), sender).await {
107+
let adapter = match Adapter::new(session.clone(), sender, config).await {
104108
Ok(v) => v,
105109
Err(e) => {
106110
eprintln!("{e}");
@@ -159,15 +163,19 @@ impl App {
159163
})
160164
}
161165

162-
pub async fn reset(mode: Mode, sender: UnboundedSender<Event>) -> AppResult<()> {
166+
pub async fn reset(
167+
mode: Mode,
168+
sender: UnboundedSender<Event>,
169+
config: Arc<Config>,
170+
) -> AppResult<()> {
163171
let session = {
164172
match iwdrs::session::Session::new().await {
165173
Ok(session) => Arc::new(session),
166174
Err(e) => return Err(anyhow!("Can not access the iwd service: {}", e).into()),
167175
}
168176
};
169177

170-
let adapter = match Adapter::new(session.clone(), sender).await {
178+
let adapter = match Adapter::new(session.clone(), sender, config).await {
171179
Ok(v) => v,
172180
Err(e) => {
173181
eprintln!("{e}");

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ async fn main() -> AppResult<()> {
2929

3030
let mode = Mode::try_from(mode.as_str())?;
3131

32-
if App::reset(mode.clone(), tui.events.sender.clone())
32+
if App::reset(mode.clone(), tui.events.sender.clone(), config.clone())
3333
.await
3434
.is_err()
3535
{
3636
tui.exit()?;
3737
}
3838

39-
let mut app = App::new(mode, tui.events.sender.clone()).await?;
39+
let mut app = App::new(config.clone(), mode, tui.events.sender.clone()).await?;
4040

4141
while app.running {
4242
tui.draw(&mut app)?;
@@ -55,13 +55,13 @@ async fn main() -> AppResult<()> {
5555
app.notifications.push(notification);
5656
}
5757
Event::Reset(mode) => {
58-
if App::reset(mode.clone(), tui.events.sender.clone())
58+
if App::reset(mode.clone(), tui.events.sender.clone(), config.clone())
5959
.await
6060
.is_err()
6161
{
6262
tui.exit()?;
6363
}
64-
app = App::new(mode, tui.events.sender.clone()).await?;
64+
app = App::new(config.clone(), mode, tui.events.sender.clone()).await?;
6565
}
6666
_ => {}
6767
}

0 commit comments

Comments
 (0)