Skip to content

Commit 944ad00

Browse files
authored
Replace help popup with help banner (#48)
1 parent b7d4b6c commit 944ad00

File tree

10 files changed

+173
-261
lines changed

10 files changed

+173
-261
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapter.rs

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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, Span},
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

@@ -82,25 +89,27 @@ impl Adapter {
8289
None => false,
8390
};
8491

85-
let (device_block, access_point_block, connected_devices_block) = {
92+
let (device_block, access_point_block, connected_devices_block, help_block) = {
8693
let chunks = Layout::default()
8794
.direction(Direction::Vertical)
8895
.constraints(if any_connected_devices {
8996
&[
9097
Constraint::Percentage(33),
9198
Constraint::Percentage(33),
9299
Constraint::Percentage(33),
100+
Constraint::Length(1),
93101
]
94102
} else {
95103
&[
96104
Constraint::Percentage(50),
97105
Constraint::Percentage(50),
98106
Constraint::Fill(1),
107+
Constraint::Length(1),
99108
]
100109
})
101110
.margin(1)
102111
.split(frame.area());
103-
(chunks[0], chunks[1], chunks[2])
112+
(chunks[0], chunks[1], chunks[2], chunks[3])
104113
};
105114

106115
// Device
@@ -423,6 +432,46 @@ impl Adapter {
423432

424433
frame.render_widget(connected_devices_list, connected_devices_block);
425434
}
435+
436+
let help_message = match focused_block {
437+
FocusedBlock::Device => Line::from(vec![
438+
Span::from(self.config.device.infos.to_string()).bold(),
439+
Span::from(" Infos"),
440+
Span::from(" | "),
441+
Span::from(self.config.device.toggle_power.to_string()).bold(),
442+
Span::from(" Toggle Power"),
443+
Span::from(" | "),
444+
Span::from("ctrl+r").bold(),
445+
Span::from(" Switch Mode"),
446+
Span::from(" | "),
447+
Span::from("⇄").bold(),
448+
Span::from(" Nav"),
449+
]),
450+
FocusedBlock::AdapterInfos | FocusedBlock::AccessPointInput => Line::from(vec![
451+
Span::from("󱊷 ").bold(),
452+
Span::from(" Discard"),
453+
Span::from(" | "),
454+
Span::from("⇄").bold(),
455+
Span::from(" Nav"),
456+
]),
457+
FocusedBlock::AccessPoint => Line::from(vec![
458+
Span::from(self.config.ap.start.to_string()).bold(),
459+
Span::from(" New AP"),
460+
Span::from(" | "),
461+
Span::from(self.config.ap.stop.to_string()).bold(),
462+
Span::from(" Stop AP"),
463+
Span::from(" | "),
464+
Span::from("ctrl+r").bold(),
465+
Span::from(" Switch Mode"),
466+
Span::from(" | "),
467+
Span::from("⇄").bold(),
468+
Span::from(" Nav"),
469+
]),
470+
_ => Line::from(""),
471+
};
472+
473+
let help_message = help_message.centered().blue();
474+
frame.render_widget(help_message, help_block);
426475
}
427476

428477
pub fn render_station_mode(
@@ -431,18 +480,19 @@ impl Adapter {
431480
color_mode: ColorMode,
432481
focused_block: FocusedBlock,
433482
) {
434-
let (device_block, station_block, known_networks_block, new_networks_block) = {
483+
let (device_block, station_block, known_networks_block, new_networks_block, help_block) = {
435484
let chunks = Layout::default()
436485
.direction(Direction::Vertical)
437486
.constraints([
438487
Constraint::Length(5),
439488
Constraint::Length(5),
440489
Constraint::Min(5),
441490
Constraint::Min(5),
491+
Constraint::Length(1),
442492
])
443493
.margin(1)
444494
.split(frame.area());
445-
(chunks[0], chunks[1], chunks[2], chunks[3])
495+
(chunks[0], chunks[1], chunks[2], chunks[3], chunks[4])
446496
};
447497

448498
// Device
@@ -984,6 +1034,99 @@ impl Adapter {
9841034
new_networks_block,
9851035
&mut new_networks_state,
9861036
);
1037+
1038+
let help_message = match focused_block {
1039+
FocusedBlock::Device => Line::from(vec![
1040+
Span::from(self.config.station.start_scanning.to_string()).bold(),
1041+
Span::from(" Scan"),
1042+
Span::from(" | "),
1043+
Span::from(self.config.device.infos.to_string()).bold(),
1044+
Span::from(" Infos"),
1045+
Span::from(" | "),
1046+
Span::from(self.config.device.toggle_power.to_string()).bold(),
1047+
Span::from(" Toggle Power"),
1048+
Span::from(" | "),
1049+
Span::from("ctrl+r").bold(),
1050+
Span::from(" Switch Mode"),
1051+
Span::from(" | "),
1052+
Span::from("⇄").bold(),
1053+
Span::from(" Nav"),
1054+
]),
1055+
FocusedBlock::Station => Line::from(vec![
1056+
Span::from(self.config.station.start_scanning.to_string()).bold(),
1057+
Span::from(" Scan"),
1058+
Span::from(" | "),
1059+
Span::from("ctrl+r").bold(),
1060+
Span::from(" Switch Mode"),
1061+
Span::from(" | "),
1062+
Span::from("⇄").bold(),
1063+
Span::from(" Nav"),
1064+
]),
1065+
FocusedBlock::KnownNetworks => Line::from(vec![
1066+
Span::from("k,").bold(),
1067+
Span::from(" Up"),
1068+
Span::from(" | "),
1069+
Span::from("j,").bold(),
1070+
Span::from(" Down"),
1071+
Span::from(" | "),
1072+
Span::from(if self.config.station.toggle_connect == ' ' {
1073+
"󱁐 ".to_string()
1074+
} else {
1075+
self.config.station.toggle_connect.to_string()
1076+
})
1077+
.bold(),
1078+
Span::from(" Connect/Disconnect"),
1079+
Span::from(" | "),
1080+
Span::from(self.config.station.known_network.remove.to_string()).bold(),
1081+
Span::from(" Remove"),
1082+
Span::from(" | "),
1083+
Span::from(
1084+
self.config
1085+
.station
1086+
.known_network
1087+
.toggle_autoconnect
1088+
.to_string(),
1089+
)
1090+
.bold(),
1091+
Span::from(" Toggle Autoconnect"),
1092+
Span::from(" | "),
1093+
Span::from("󱊷 ").bold(),
1094+
Span::from(" Discard"),
1095+
Span::from(" | "),
1096+
Span::from("ctrl+r").bold(),
1097+
Span::from(" Switch Mode"),
1098+
Span::from(" | "),
1099+
Span::from("⇄").bold(),
1100+
Span::from(" Nav"),
1101+
]),
1102+
FocusedBlock::NewNetworks => Line::from(vec![
1103+
Span::from("k,").bold(),
1104+
Span::from(" Up"),
1105+
Span::from(" | "),
1106+
Span::from("j,").bold(),
1107+
Span::from(" Down"),
1108+
Span::from(" | "),
1109+
Span::from("󱁐 ").bold(),
1110+
Span::from(" Connect"),
1111+
Span::from(" | "),
1112+
Span::from("󱊷 ").bold(),
1113+
Span::from(" Discard"),
1114+
Span::from(" | "),
1115+
Span::from("ctrl+r").bold(),
1116+
Span::from(" Switch Mode"),
1117+
Span::from(" | "),
1118+
Span::from("⇄").bold(),
1119+
Span::from(" Nav"),
1120+
]),
1121+
FocusedBlock::AdapterInfos | FocusedBlock::AuthKey => {
1122+
Line::from(vec![Span::from("󱊷 ").bold(), Span::from(" Discard")])
1123+
}
1124+
_ => Line::from(""),
1125+
};
1126+
1127+
let help_message = help_message.centered().blue();
1128+
1129+
frame.render_widget(help_message, help_block);
9871130
}
9881131

9891132
pub fn render_adapter(&self, frame: &mut Frame, color_mode: ColorMode) {
File renamed without changes.

src/app.rs

Lines changed: 13 additions & 8 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, help::Help, 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

@@ -29,7 +29,6 @@ pub enum FocusedBlock {
2929
AccessPoint,
3030
KnownNetworks,
3131
NewNetworks,
32-
Help,
3332
AuthKey,
3433
AdapterInfos,
3534
AccessPointInput,
@@ -46,7 +45,6 @@ pub enum ColorMode {
4645
pub struct App {
4746
pub running: bool,
4847
pub focused_block: FocusedBlock,
49-
pub help: Help,
5048
pub color_mode: ColorMode,
5149
pub notifications: Vec<Notification>,
5250
pub session: Arc<Session>,
@@ -91,7 +89,11 @@ pub async fn request_confirmation(
9189
}
9290

9391
impl App {
94-
pub async fn new(help: Help, 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> {
9597
let session = {
9698
match iwdrs::session::Session::new().await {
9799
Ok(session) => Arc::new(session),
@@ -102,7 +104,7 @@ impl App {
102104
}
103105
};
104106

105-
let adapter = match Adapter::new(session.clone(), sender).await {
107+
let adapter = match Adapter::new(session.clone(), sender, config).await {
106108
Ok(v) => v,
107109
Err(e) => {
108110
eprintln!("{e}");
@@ -144,7 +146,6 @@ impl App {
144146
Ok(Self {
145147
running: true,
146148
focused_block: FocusedBlock::Device,
147-
help,
148149
color_mode,
149150
notifications: Vec::new(),
150151
session,
@@ -162,15 +163,19 @@ impl App {
162163
})
163164
}
164165

165-
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<()> {
166171
let session = {
167172
match iwdrs::session::Session::new().await {
168173
Ok(session) => Arc::new(session),
169174
Err(e) => return Err(anyhow!("Can not access the iwd service: {}", e).into()),
170175
}
171176
};
172177

173-
let adapter = match Adapter::new(session.clone(), sender).await {
178+
let adapter = match Adapter::new(session.clone(), sender, config).await {
174179
Ok(v) => v,
175180
Err(e) => {
176181
eprintln!("{e}");

src/device.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use iwdrs::{device::Device as iwdDevice, modes::Mode, session::Session};
55
use tokio::sync::mpsc::UnboundedSender;
66

77
use crate::{
8-
access_point::AccessPoint, app::AppResult, event::Event, notification::Notification,
9-
station::Station,
8+
ap::AccessPoint, app::AppResult, event::Event, notification::Notification, station::Station,
109
};
1110

1211
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)