Skip to content

Commit

Permalink
fix: corrects problems found in the review
Browse files Browse the repository at this point in the history
  • Loading branch information
José Gomes authored and matheuswhite committed May 10, 2024
1 parent d0b8a54 commit 38c61f8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
107 changes: 53 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![deny(warnings)]
#![allow(unused)]
#![feature(utf8_chunks)]

extern crate core;
Expand All @@ -19,7 +18,6 @@ use ratatui::Terminal;
use std::io;
use std::io::Stdout;
use std::path::PathBuf;
use clap::Args;

mod blink_color;
mod command_bar;
Expand All @@ -40,13 +38,13 @@ pub type ConcreteBackend = CrosstermBackend<Stdout>;

#[derive(Subcommand)]
pub enum Commands {
Serial{
port: Option<std::string::String>,
Serial {
port: Option<String>,
baudrate: Option<u32>,
},
Ble{
name_device: std::string::String,
mtu: u32
Ble {
name_device: String,
mtu: u32,
},
}

Expand Down Expand Up @@ -77,61 +75,62 @@ async fn app() -> Result<(), String> {

let view_length = cli.view_length.unwrap_or(CAPACITY);
let cmd_file = cli.cmd_file.unwrap_or(PathBuf::from(CMD_FILEPATH));
let interface;

match cli.command {
Commands::Serial {port, baudrate} => {
Commands::Serial { port, baudrate } => {
let port_select = port.unwrap_or("".to_string());
let baudrate_select= baudrate.unwrap_or(0);

let interface = SerialIF::build_and_connect(port_select, baudrate_select);

enable_raw_mode().map_err(|_| "Cannot enable terminal raw mode".to_string())?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)
.map_err(|_| "Cannot enable alternate screen and mouse capture".to_string())?;
let backend = CrosstermBackend::new(stdout);
let mut terminal =
Terminal::new(backend).map_err(|_| "Cannot create terminal backend".to_string())?;

let datetime = Local::now().format("%Y%m%d_%H%M%S");
let mut command_bar = CommandBar::new(interface, view_length, format!("{}.txt", datetime))
.with_command_file(cmd_file.as_path().to_str().unwrap());

'main: loop {
{
let text_view = command_bar.get_text_view().await;
let interface = command_bar.get_interface().await;

terminal
.draw(|f| command_bar.draw(f, &text_view, &interface))
.map_err(|_| "Fail at terminal draw".to_string())?;
}

if command_bar
.update(terminal.backend().size().unwrap())
.await
.is_err()
{
break 'main;
}
}

disable_raw_mode().map_err(|_| "Cannot disable terminal raw mode".to_string())?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
).map_err(|_| "Cannot disable alternate screen and mouse capture".to_string())?;
terminal
.show_cursor()
.map_err(|_| "Cannot show mouse cursor".to_string())?;
let baudrate_select = baudrate.unwrap_or(0);

Ok(())
interface = SerialIF::build_and_connect(&port_select, baudrate_select);
}
_ => {
Ok(())
unimplemented!()
}
}

enable_raw_mode().map_err(|_| "Cannot enable terminal raw mode".to_string())?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)
.map_err(|_| "Cannot enable alternate screen and mouse capture".to_string())?;
let backend = CrosstermBackend::new(stdout);
let mut terminal =
Terminal::new(backend).map_err(|_| "Cannot create terminal backend".to_string())?;

let datetime = Local::now().format("%Y%m%d_%H%M%S");
let mut command_bar = CommandBar::new(interface, view_length, format!("{}.txt", datetime))
.with_command_file(cmd_file.as_path().to_str().unwrap());

'main: loop {
{
let text_view = command_bar.get_text_view().await;
let interface = command_bar.get_interface().await;

terminal
.draw(|f| command_bar.draw(f, &text_view, &interface))
.map_err(|_| "Fail at terminal draw".to_string())?;
}

if command_bar
.update(terminal.backend().size().unwrap())
.await
.is_err()
{
break 'main;
}
}

disable_raw_mode().map_err(|_| "Cannot disable terminal raw mode".to_string())?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
).map_err(|_| "Cannot disable alternate screen and mouse capture".to_string())?;
terminal
.show_cursor()
.map_err(|_| "Cannot show mouse cursor".to_string())?;

Ok(())
}

#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct SerialIFSynchronized {
}

impl SerialIF {
pub fn build_and_connect(port: String, baudrate: u32) -> Self {
pub fn build_and_connect(port: &str, baudrate: u32) -> Self {
let info = SerialInfo {
port: port.to_string(),
baudrate,
Expand Down

0 comments on commit 38c61f8

Please sign in to comment.