First menu done
This commit is contained in:
parent
8f8e4e4f6e
commit
5ed8d3a7a9
|
|
@ -23,3 +23,12 @@ pub enum TinyError {
|
|||
#[error("A runtime error occured :\n{0}")]
|
||||
TUIRuntimeError(color_eyre::eyre::Error),
|
||||
}
|
||||
|
||||
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
|
||||
/// The direction the input was received, usefull for menu where R/L U/D give similar results and implementation
|
||||
pub(crate) enum InputDirection {
|
||||
Up,
|
||||
Down,
|
||||
Right,
|
||||
Left,
|
||||
}
|
||||
|
|
|
|||
323
src/ui.rs
323
src/ui.rs
|
|
@ -4,7 +4,7 @@ use color_eyre::{owo_colors::OwoColorize, Result};
|
|||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
|
||||
layout::{Constraint, Layout, Rect},
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::{
|
||||
palette::tailwind::{BLUE, GREEN, SLATE},
|
||||
Color, Modifier, Style, Stylize,
|
||||
|
|
@ -13,13 +13,16 @@ use ratatui::{
|
|||
text::Line,
|
||||
widgets::{
|
||||
Block, Borders, HighlightSpacing, List, ListDirection, ListItem, ListState, Padding,
|
||||
Paragraph, StatefulWidget, Widget, Wrap,
|
||||
Paragraph, ScrollDirection, StatefulWidget, Widget, Wrap,
|
||||
},
|
||||
DefaultTerminal, Frame,
|
||||
};
|
||||
use serialport::SerialPortInfo;
|
||||
use strum::EnumIter;
|
||||
use strum::IntoEnumIterator;
|
||||
use std::{cmp::min, fmt::Pointer, iter::Iterator};
|
||||
use strum::{EnumCount, EnumIter, EnumProperty};
|
||||
use strum::{FromRepr, IntoEnumIterator};
|
||||
|
||||
use crate::structs::InputDirection;
|
||||
|
||||
/// The header style
|
||||
const HEADER_STYLE: Style = Style::new().fg(SLATE.c100).bg(BLUE.c800);
|
||||
|
|
@ -31,6 +34,10 @@ const ALT_ROW_BG_COLOR: Color = SLATE.c900;
|
|||
const SELECTED_STYLE: Style = Style::new().bg(SLATE.c800).add_modifier(Modifier::BOLD);
|
||||
/// Foreground color
|
||||
const TEXT_FG_COLOR: Color = SLATE.c200;
|
||||
/// Color of a selected border
|
||||
const SELECTED_COLOR: Color = Color::White;
|
||||
/// Color of an unselected item
|
||||
const UNSELECTED_COLOR: Color = Color::DarkGray;
|
||||
|
||||
/// This struct holds the current state of the app.
|
||||
#[derive(Debug)]
|
||||
|
|
@ -41,8 +48,135 @@ pub(crate) struct AppInfo {
|
|||
baudrate: BaudRateList,
|
||||
/// The list of ports
|
||||
port_list: PortList,
|
||||
/// State of the configuration, akin to tab or window
|
||||
flow_state: AppState,
|
||||
// /// Selected item
|
||||
// item_selected: ItemSelectedState
|
||||
/// A debug message string
|
||||
status_message: String,
|
||||
}
|
||||
|
||||
// #[derive(ItemSelectedState)]
|
||||
// /// The item currently selected
|
||||
|
||||
#[derive(Debug)]
|
||||
/// The inner state of the app, useful for display and keystrokes
|
||||
enum AppState {
|
||||
/// Initial config, baudrate and port
|
||||
InitialConfig(SelectedConfig),
|
||||
/// Final config, frequencies and points
|
||||
FinalConfig(SelectedFinalConfig),
|
||||
/// Popup for text entering
|
||||
Popup,
|
||||
/// Save window
|
||||
Finished,
|
||||
}
|
||||
|
||||
impl Default for AppState {
|
||||
fn default() -> Self {
|
||||
Self::InitialConfig(SelectedConfig::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortcut for move right
|
||||
macro_rules! mright {
|
||||
($v:ident, $cname:ident,$fname:ident) => {
|
||||
Some(AppState::$cname(
|
||||
$fname::from_repr(min($v + 1, $fname::COUNT - 1)).unwrap(),
|
||||
))
|
||||
};
|
||||
}
|
||||
|
||||
/// Shortcut to move left
|
||||
macro_rules! mleft {
|
||||
($v:ident, $cname:ident,$fname:ident) => {
|
||||
if $v == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(AppState::$cname($fname::from_repr($v - 1).unwrap()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl AppInfo {
|
||||
/// Goes to the next menu depending on the position in the program
|
||||
fn next_menu(&mut self, direction: InputDirection) {
|
||||
if let Some(s) = match self.flow_state {
|
||||
AppState::InitialConfig(ref r) => {
|
||||
let v = *r as usize;
|
||||
match direction {
|
||||
InputDirection::Right => mright!(v, InitialConfig, SelectedConfig),
|
||||
InputDirection::Left => mleft!(v, InitialConfig, SelectedConfig),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
AppState::FinalConfig(ref r) => {
|
||||
let v = *r as usize;
|
||||
match direction {
|
||||
InputDirection::Right => mright!(v, FinalConfig, SelectedFinalConfig),
|
||||
InputDirection::Left => mleft!(v, FinalConfig, SelectedFinalConfig),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
AppState::Popup => None,
|
||||
AppState::Finished => None,
|
||||
} {
|
||||
self.flow_state = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected item in the config
|
||||
#[derive(Debug, EnumCount, FromRepr, Clone, Copy)]
|
||||
enum SelectedConfig {
|
||||
/// Selects the ports
|
||||
Port,
|
||||
/// Selects the baudrate
|
||||
Baudrate,
|
||||
// /// Selects <Ok> to proceed
|
||||
// Proceed,
|
||||
}
|
||||
|
||||
impl Default for SelectedConfig {
|
||||
fn default() -> Self {
|
||||
Self::Port
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected item in the final menu
|
||||
#[derive(Debug, EnumCount, FromRepr, Copy, Clone)]
|
||||
enum SelectedFinalConfig {
|
||||
/// Selects the start frequency
|
||||
StartFreq,
|
||||
/// Selects the final frequency
|
||||
FinalFreq,
|
||||
/// Selects the number of points
|
||||
Points,
|
||||
// /// Selects <Ok> to proceed
|
||||
// Proceed,
|
||||
}
|
||||
|
||||
impl Default for SelectedFinalConfig {
|
||||
fn default() -> Self {
|
||||
Self::StartFreq
|
||||
}
|
||||
}
|
||||
|
||||
// impl SelectedFinalConfig {
|
||||
// fn change_menu(&mut self, direction: InputDirection) {
|
||||
// match direction {
|
||||
// InputDirection::Right => match self {
|
||||
// SelectedFinalConfig::StartFreq => Self::FinalFreq,
|
||||
// SelectedFinalConfig::FinalFreq => Self::Points,
|
||||
// SelectedFinalConfig::Points => Self::Proceed,
|
||||
// SelectedFinalConfig::Proceed => (),
|
||||
// },
|
||||
// InputDirection::Left => todo!(),
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// The list of all baudrates selectables
|
||||
#[derive(Debug)]
|
||||
struct BaudRateList {
|
||||
|
|
@ -61,43 +195,27 @@ impl BaudRateList {
|
|||
.map(|i| format!("{}", *i as usize))
|
||||
.collect()
|
||||
}
|
||||
// fn to_itemlist(&self) -> Vec<ListItem> {
|
||||
// self.baudrates
|
||||
// .iter()
|
||||
// .map(|i| ListItemformat!("{}", *i as usize))
|
||||
// .collect()
|
||||
// }
|
||||
|
||||
/// Pick the baudrate above
|
||||
fn above(&mut self) {
|
||||
// self.selected = (self.selected + 1) % self.baudrates.len();
|
||||
self.state.select_previous();
|
||||
}
|
||||
|
||||
/// Pick the baudrate below
|
||||
fn below(&mut self) {
|
||||
// self.selected = (self.selected - 1) % self.baudrates.len();
|
||||
self.state.select_next();
|
||||
/// Change the cursor position on the baudrates
|
||||
fn change_cursor(&mut self, direction: InputDirection) {
|
||||
match direction {
|
||||
InputDirection::Down => self.state.select_next(),
|
||||
InputDirection::Up => self.state.select_previous(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BaudRateList {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
selected: 3,
|
||||
selected: 0,
|
||||
baudrates: BaudRate::iter().collect::<Vec<_>>(),
|
||||
state: ListState::default(),
|
||||
state: ListState::default().with_selected(Some(3)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// impl Iterator for BaudRateList {
|
||||
// type Item = &'str;
|
||||
|
||||
// fn next(&mut self) -> Option<Self::Item> {
|
||||
// todo!()
|
||||
// }
|
||||
// }
|
||||
/// A list of possible baudrates
|
||||
#[derive(Debug, EnumIter, Copy, Clone)]
|
||||
enum BaudRate {
|
||||
|
|
@ -132,20 +250,29 @@ impl PortList {
|
|||
Self {
|
||||
ports,
|
||||
selected,
|
||||
state: ListState::default(),
|
||||
state: ListState::default().with_selected(Some(selected)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the list of ports
|
||||
/// TODO: Remove the clone part
|
||||
// TODO: Remove the clone part
|
||||
fn to_vecstr(&self) -> Vec<String> {
|
||||
self.ports.clone()
|
||||
}
|
||||
|
||||
/// Change the selected item
|
||||
fn change_cursor(&mut self, direction: InputDirection) {
|
||||
match direction {
|
||||
InputDirection::Up => self.state.select_previous(),
|
||||
InputDirection::Down => self.state.select_next(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to guess the best port to preselect
|
||||
fn find_best_port(ports: &[String]) -> usize {
|
||||
let best_ports = ["/dev/ttyACM0", "COM3", "COM5"];
|
||||
let best_ports = ["/dev/ttyACM0", "COM3", "COM5", "/dev/ttyS16"];
|
||||
for port in best_ports {
|
||||
if let Some(p) = ports.iter().position(|v| v == port) {
|
||||
return p;
|
||||
|
|
@ -161,6 +288,8 @@ impl AppInfo {
|
|||
should_quit: false,
|
||||
baudrate: BaudRateList::default(),
|
||||
port_list: PortList::from_serial(info),
|
||||
flow_state: AppState::default(),
|
||||
status_message: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -171,9 +300,10 @@ impl AppInfo {
|
|||
}
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
|
||||
// KeyCode::Char('h') | KeyCode::Left => self.baudrate.
|
||||
KeyCode::Char('j') | KeyCode::Down => self.baudrate.above(),
|
||||
KeyCode::Char('k') | KeyCode::Up => self.baudrate.below(),
|
||||
KeyCode::Char('j') | KeyCode::Down => self.change_cursor_position(InputDirection::Down),
|
||||
KeyCode::Char('k') | KeyCode::Up => self.change_cursor_position(InputDirection::Up),
|
||||
KeyCode::Char('h') | KeyCode::Left => self.next_menu(InputDirection::Left),
|
||||
KeyCode::Char('l') | KeyCode::Right => self.next_menu(InputDirection::Right),
|
||||
// KeyCode::Char('g') | KeyCode::Home => self.select_first(),
|
||||
// KeyCode::Char('G') | KeyCode::End => self.select_last(),
|
||||
// KeyCode::Enter => {
|
||||
|
|
@ -197,68 +327,103 @@ impl AppInfo {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
// fn init_config(&self) -> &Buffer {
|
||||
// let [_title_area, main_area, _status_area] = Layout::vertical([
|
||||
// Constraint::Length(3),
|
||||
// Constraint::Min(10),
|
||||
// Constraint::Length(3),
|
||||
// ])
|
||||
// .areas(frame.area());
|
||||
// let [_, area_ports, area_baud, _] = Layout::horizontal([
|
||||
// Constraint::Min(10),
|
||||
// Constraint::Percentage(50),
|
||||
// Constraint::Percentage(50),
|
||||
// Constraint::Min(10),
|
||||
// ])
|
||||
// .areas(main_area);
|
||||
// main_area
|
||||
// }
|
||||
/// Initialisation of the config window
|
||||
fn init_config(&mut self) {
|
||||
// self.flow_state = AppState::InitialConfig();
|
||||
}
|
||||
|
||||
/// Draw the config info
|
||||
// TODO: This config doesn't need to be reinit every frame
|
||||
fn draw_config(&mut self, frame: &mut Frame) {
|
||||
let [_title_area, main_area, _status_area] = Layout::vertical([
|
||||
Constraint::Length(3),
|
||||
let [title_area, main_area, status_area] = Layout::vertical([
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(3),
|
||||
// Constraint::Min(8),
|
||||
// Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(frame.area());
|
||||
let [_, area_ports, area_baud, _] = Layout::horizontal([
|
||||
Constraint::Min(10),
|
||||
Constraint::Min(7),
|
||||
Constraint::Percentage(50),
|
||||
Constraint::Percentage(50),
|
||||
Constraint::Min(10),
|
||||
Constraint::Min(7),
|
||||
])
|
||||
.areas(main_area);
|
||||
|
||||
let list_baudrates = List::new(self.baudrate.to_vecstr())
|
||||
.block(Block::bordered().title("Baudrates"))
|
||||
.style(Style::default().fg(Color::White))
|
||||
.highlight_style(Style::default().add_modifier(Modifier::ITALIC))
|
||||
.highlight_symbol(">>")
|
||||
.repeat_highlight_symbol(true);
|
||||
let (b_color, p_color) = match self.flow_state {
|
||||
AppState::InitialConfig(SelectedConfig::Port) => (UNSELECTED_COLOR, SELECTED_COLOR),
|
||||
AppState::InitialConfig(SelectedConfig::Baudrate) => (SELECTED_COLOR, UNSELECTED_COLOR),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let list_ports = List::new(self.port_list.to_vecstr())
|
||||
.block(Block::bordered().title("COM Ports"))
|
||||
.style(Style::default().fg(Color::White))
|
||||
.highlight_style(Style::default().add_modifier(Modifier::UNDERLINED))
|
||||
.highlight_symbol(">>")
|
||||
.block(Block::bordered().title("COM Ports").border_style(p_color))
|
||||
.style(Style::default().fg(p_color))
|
||||
.highlight_style(
|
||||
Style::default()
|
||||
.add_modifier(Modifier::ITALIC)
|
||||
.bg(Color::Blue),
|
||||
)
|
||||
.repeat_highlight_symbol(true);
|
||||
|
||||
// frame.render_widget(list_ports, area_ports);
|
||||
// frame.render_widget(list_baudrates, area_baud);
|
||||
frame.render_stateful_widget(list_baudrates, area_baud, &mut self.baudrate.state);
|
||||
let list_baudrates = List::new(self.baudrate.to_vecstr())
|
||||
.block(Block::bordered().title("Baudrates").border_style(b_color))
|
||||
.style(Style::default().fg(b_color))
|
||||
.highlight_style(
|
||||
Style::default()
|
||||
.add_modifier(Modifier::ITALIC)
|
||||
.bg(Color::Blue),
|
||||
)
|
||||
.repeat_highlight_symbol(true);
|
||||
self.status_message = format!("{:?} ", self.flow_state);
|
||||
frame.render_stateful_widget(list_ports, area_ports, &mut self.port_list.state);
|
||||
// self.render_footer(_status_area, frame);
|
||||
frame.render_stateful_widget(list_baudrates, area_baud, &mut self.baudrate.state);
|
||||
frame.render_widget(Paragraph::new(self.status_message.clone()), title_area);
|
||||
self.render_footer(frame, status_area)
|
||||
}
|
||||
|
||||
/// Render the selection footer
|
||||
fn render_footer(&self, area: Rect, buf: &mut Buffer) {
|
||||
Paragraph::new(
|
||||
"Use ↓↑ to move, ←→ to change status widget and ↲ to validate configuration.",
|
||||
)
|
||||
.centered()
|
||||
.render(area, buf);
|
||||
} // Handle code after validation
|
||||
fn render_footer(&self, frame: &mut Frame, area: Rect) {
|
||||
frame.render_widget(
|
||||
Paragraph::new(
|
||||
"Use ↓↑ to move, ←→ to change status widget and ↲ to validate configuration.",
|
||||
)
|
||||
.centered(),
|
||||
area,
|
||||
);
|
||||
}
|
||||
|
||||
/// A directionnal input was received
|
||||
fn change_cursor_position(&mut self, direction: InputDirection) {
|
||||
//Where in the menus are we
|
||||
match self.flow_state {
|
||||
AppState::InitialConfig(ref selected_menu) => {
|
||||
// self.status_message = format!(
|
||||
// "{:?} {:?} {:?}",
|
||||
// selected_menu, self.baudrate.state, self.port_list.state
|
||||
// );
|
||||
match selected_menu {
|
||||
SelectedConfig::Baudrate => self.baudrate.change_cursor(direction),
|
||||
SelectedConfig::Port => self.port_list.change_cursor(direction),
|
||||
}
|
||||
}
|
||||
AppState::FinalConfig(ref selected_menu) => match selected_menu {
|
||||
SelectedFinalConfig::StartFreq => todo!(),
|
||||
SelectedFinalConfig::FinalFreq => todo!(),
|
||||
SelectedFinalConfig::Points => todo!(),
|
||||
},
|
||||
AppState::Popup => (),
|
||||
AppState::Finished => (),
|
||||
}
|
||||
}
|
||||
|
||||
// /// Change the selected menu
|
||||
// fn change_menu(&mut self, direction: InputDirection) {
|
||||
// match self.flow_state {
|
||||
// AppState::InitialConfig(ref menu) => self,
|
||||
// AppState::FinalConfig(_) => todo!(),
|
||||
// AppState::Popup => (),
|
||||
// AppState::Finished => (),
|
||||
// }
|
||||
// } // Handle code after validation
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_docs_in_private_items)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue