Skip to content
Draft
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: 3 additions & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod ui;
pub async fn configure() -> anyhow::Result<()> {
let res = app::run(&mut ratatui::init()).await;
ratatui::restore();

// Print any successful message to the user once the TUI is closed
if let Ok(msg) = &res
&& !msg.is_empty()
{
println!("{}", msg);
}
res.map(|_| ())
res.map(|_| ()) // Delete the success message
}
51 changes: 32 additions & 19 deletions src/configure/app/ir_enabler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::helper::*;
use crate::configure::ui::ir_enabler::{IrEnablerCtx, View, ui};
use crate::configure::ui::keys::*;
use crate::configure::ui::{DeviceSettingsCtx, SearchSettingsCtx};
use crate::video::ir::analyzer::{
self, IsIrWorking as AnalyzerResponse, Message as AnalyzerRequest, StreamAnalyzer,
};
Expand All @@ -24,6 +24,13 @@ use tokio::{
task,
};

const KEY_YES: KeyCode = KeyCode::Char('y');
const KEY_NO: KeyCode = KeyCode::Char('n');
const KEY_EXIT: KeyCode = KeyCode::Esc;
const KEY_NAVIGATE: KeyCode = KeyCode::Tab;
const KEY_CONTINUE: KeyCode = KeyCode::Enter;
const KEY_DELETE: KeyCode = KeyCode::Backspace;

#[derive(Debug)]
pub struct Config {
/// Path to the video device.
Expand Down Expand Up @@ -334,7 +341,7 @@ impl App {
async fn handle_term_event(&mut self, event: Event) -> Result<()> {
match event {
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
self.handle_key_press(key_event.code.into()).await?;
self.handle_key_press(key_event.code).await?;
}
_ => {}
};
Expand Down Expand Up @@ -422,13 +429,13 @@ impl App {
}

/// Handles a key event based on the current application state.
async fn handle_key_press(&mut self, key: Key) -> Result<()> {
async fn handle_key_press(&mut self, key: KeyCode) -> Result<()> {
match self.state() {
State::Menu => match key {
KEY_EXIT => self.set_state(State::Failure),
KEY_NAVIGATE => self.next_setting(),
KEY_DELETE => self.edit_setting(None),
Key(KeyCode::Char(c)) => self.edit_setting(Some(c)),
KeyCode::Char(c) => self.edit_setting(Some(c)),
KEY_CONTINUE => self.set_state(State::ConfirmStart),
_ => {}
},
Expand Down Expand Up @@ -458,7 +465,7 @@ impl App {
/// In both of the two case, also changes the state to [`State::Running`].
///
/// Otherwise, does nothing.
async fn confirm_working(&mut self, k: Key) -> Result<()> {
async fn confirm_working(&mut self, k: KeyCode) -> Result<()> {
let mut response = IREnablerResponse::No;
if k == KEY_YES {
response = IREnablerResponse::Yes;
Expand All @@ -477,7 +484,7 @@ impl App {
/// and sends [`IREnablerResponse::Abort`] to the configurator task.
///
/// If the key is [`KEY_NO`], change the state back to [`State::Running`].
async fn abort_or_continue(&mut self, k: Key) -> Result<()> {
async fn abort_or_continue(&mut self, k: KeyCode) -> Result<()> {
match k {
KEY_NO | KEY_EXIT => self.set_state(self.prev_state()),
KEY_YES => {
Expand All @@ -498,7 +505,7 @@ impl App {
/// If the key is [`KEY_NO`], change the state back to the previous state.
///
/// Returns directly an error if the video stream is already started.
fn start_or_back(&mut self, k: Key) -> Result<()> {
fn start_or_back(&mut self, k: KeyCode) -> Result<()> {
// check that the path exists
if !self.is_device_valid() {
self.set_state(State::Menu);
Expand Down Expand Up @@ -594,6 +601,18 @@ impl IrEnablerCtx for App {
fn show_menu_start_prompt(&self) -> bool {
self.state() == State::ConfirmStart
}
fn controls_list_state(&mut self) -> &mut ListState {
&mut self.controls_list_state
}
fn controls(&self) -> &[XuControl] {
&self.controls
}
fn image(&self) -> Option<&Image> {
self.image.as_ref()
}
}

impl DeviceSettingsCtx for App {
fn device_settings_list_state(&mut self) -> &mut ListState {
&mut self.device_settings_list_state
}
Expand All @@ -615,6 +634,9 @@ impl IrEnablerCtx for App {
fn fps(&self) -> Option<u32> {
self.config.fps
}
}

impl SearchSettingsCtx for App {
fn search_settings_list_state(&mut self) -> &mut ListState {
&mut self.search_settings_list_state
}
Expand All @@ -633,15 +655,6 @@ impl IrEnablerCtx for App {
fn inc_step(&self) -> u8 {
self.config.inc_step
}
fn controls_list_state(&mut self) -> &mut ListState {
&mut self.controls_list_state
}
fn controls(&self) -> &[XuControl] {
&self.controls
}
fn image(&self) -> Option<&Image> {
self.image.as_ref()
}
}

#[cfg(test)]
Expand All @@ -655,16 +668,16 @@ mod tests {
App::new()
}

fn make_key_event(keycode: Key) -> KeyEvent {
fn make_key_event(keycode: KeyCode) -> KeyEvent {
KeyEvent {
code: keycode.into(),
code: keycode,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
state: crossterm::event::KeyEventState::NONE,
}
}

fn make_term_key_event(keycode: Key) -> Event {
fn make_term_key_event(keycode: KeyCode) -> Event {
Event::Key(make_key_event(keycode))
}

Expand Down
11 changes: 5 additions & 6 deletions src/configure/app/tool_menu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::configure::app::ir_enabler::App as IREnablerApp;
use crate::configure::ui::keys::{KEY_CONTINUE, KEY_EXIT, KEY_NAVIGATE};
use crate::configure::ui::tool_menu::ui;

use anyhow::Result;
use crossterm::event;
use crossterm::event::{self, KeyCode};
use crossterm::event::{Event, KeyEventKind};

/// Application state for the tool menu.
Expand Down Expand Up @@ -31,10 +30,10 @@ impl App {
terminal.draw(|f| ui(f, self))?;
match event::read()? {
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
match key_event.code.into() {
KEY_NAVIGATE => self.next_tool(),
KEY_CONTINUE => return self.start_tool(terminal).await,
KEY_EXIT => return Ok(""),
match key_event.code {
KeyCode::Tab => self.next_tool(),
KeyCode::Enter => return self.start_tool(terminal).await,
KeyCode::Esc => return Ok(""),
_ => {}
}
}
Expand Down
69 changes: 27 additions & 42 deletions src/configure/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,40 @@ use helper::*;
pub mod ir_enabler;
mod shared;
use shared::*;
pub use shared::{DeviceSettingsCtx, SearchSettingsCtx};
pub mod tool_menu;
pub mod tweaker;

pub mod keys {
mod keys {
use crossterm::event::KeyCode;
use derive_more::{From, Into};
use ratatui::{
style::Stylize,
style::{Color, Style},
text::{Line, Span},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, From, Into)]
pub(crate) struct Key(pub KeyCode);
#[derive(Debug, Clone, Copy)]
pub struct Key {
code: KeyCode,
name: &'static str,
color: Color,
}

impl Key {
const fn new(code: KeyCode, name: &'static str, color: Color) -> Self {
Self { code, name, color }
}
}

pub fn keys_to_line(keys: &[Key]) -> Line<'static> {
let mut spans = Vec::with_capacity(keys.len() * 3);
for (i, key) in keys.iter().enumerate() {
match key.0 {
KeyCode::Esc => {
spans.push("Quit <".bold());
spans.push(Span::styled("Esc", Style::default().fg(Color::Red)));
spans.push(">".bold());
}
KeyCode::Tab => {
spans.push("Navigate <".bold());
spans.push(Span::styled("Tab", Style::default().fg(Color::Yellow)));
spans.push(">".bold());
}
KeyCode::Enter => {
spans.push("Continue <".bold());
spans.push(Span::styled("Enter", Style::default().fg(Color::Green)));
spans.push(">".bold());
}
KeyCode::Char('y') => {
spans.push("Yes <".bold());
spans.push(Span::styled("y", Style::default().fg(Color::Green)));
spans.push(">".bold());
}
KeyCode::Char('n') => {
spans.push("No <".bold());
spans.push(Span::styled("n", Style::default().fg(Color::Red)));
spans.push(">".bold());
}
_ => {
spans.push("? <".bold());
spans.push(Span::raw(format!("{:?}", key.0)));
spans.push(">".bold());
}
}
spans.push(format!("{} <", key.name).bold());
spans.push(Span::styled(
key.code.to_string(),
Style::default().fg(key.color),
));
spans.push(">".bold());

if i != keys.len() - 1 {
spans.push(Span::raw(" "));
Expand All @@ -60,12 +45,12 @@ pub mod keys {
Line::from(spans)
}

pub const KEY_EXIT: Key = Key(KeyCode::Esc);
pub const KEY_NAVIGATE: Key = Key(KeyCode::Tab);
pub const KEY_CONTINUE: Key = Key(KeyCode::Enter);
pub const KEY_YES: Key = Key(KeyCode::Char('y'));
pub const KEY_NO: Key = Key(KeyCode::Char('n'));
pub const KEY_DELETE: Key = Key(KeyCode::Backspace);
pub const KEY_EXIT: Key = Key::new(KeyCode::Esc, "Quit", Color::Red);
pub const KEY_NAVIGATE: Key = Key::new(KeyCode::Tab, "Navigate", Color::Yellow);
pub const KEY_CONTINUE: Key = Key::new(KeyCode::Enter, "Continue", Color::Green);
pub const KEY_YES: Key = Key::new(KeyCode::Char('y'), "Yes", Color::Green);
pub const KEY_NO: Key = Key::new(KeyCode::Char('n'), "No", Color::Red);
pub const KEY_EDIT: Key = Key::new(KeyCode::Enter, "Edit", Color::Green);
}

#[cfg(test)]
Expand Down
Loading
Loading