init commit, ui functional

This commit is contained in:
Phani Pavan K
2025-08-25 09:31:17 +05:30
parent f4bdff959e
commit d9b9925d0b
8 changed files with 1479 additions and 0 deletions

164
src/main.rs Normal file
View File

@@ -0,0 +1,164 @@
#[allow(non_snake_case)]
mod app;
mod ui;
use crate::ui::ui;
use app::AppState;
use color_eyre::Result;
use ratatui::Terminal;
use ratatui::crossterm::event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers,
};
use ratatui::crossterm::execute;
use ratatui::crossterm::terminal::{
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use ratatui::prelude::{Backend, CrosstermBackend};
use std::io;
use crate::app::status::{CurrentScreen, EditingField};
fn main() -> Result<()> {
enable_raw_mode()?;
let mut stderr = io::stderr(); // This is a special case. Normally using stdout is fine
execute!(stderr, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stderr);
let mut terminal = Terminal::new(backend)?;
// create app and run it
let mut app = AppState::new("./conf.json".to_string());
// app.load();
let res = runApp(&mut app, &mut terminal);
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Ok(do_print) = res {
if do_print {
app.print();
}
} else if let Err(err) = res {
println!("{err:?}");
}
Ok(())
}
fn runApp<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> Result<bool> {
loop {
terminal.draw(|f| ui(f, app))?; // from ui.rs
if let Event::Key(key) = event::read()? {
// println!("{key:?}");
if key.kind == event::KeyEventKind::Release {
continue;
}
match app.screen {
CurrentScreen::Settings => match key.code {
KeyCode::Home | KeyCode::Char('m') | KeyCode::Esc => {
app.screen = CurrentScreen::Main;
}
_ => {}
},
CurrentScreen::Main => match key.code {
KeyCode::Char('a') => {
app.screen = CurrentScreen::Add;
app.currentlyEditing = Some(EditingField::FromIP);
}
KeyCode::Char('q') | KeyCode::F(10) | KeyCode::Esc => {
app.screen = CurrentScreen::Exit
}
KeyCode::Char('s') | KeyCode::F(2) => app.screen = CurrentScreen::Settings,
_ => {}
},
CurrentScreen::Add => match (key.modifiers, key.code) {
(KeyModifiers::NONE, KeyCode::Enter) => {
if let Some(eF) = &app.currentlyEditing {
match eF {
EditingField::ToPort => {
app.store();
app.screen = CurrentScreen::Main;
app.currentlyEditing = None;
}
_ => app.nextField(),
}
}
}
(KeyModifiers::NONE, KeyCode::Backspace) => {
if let Some(eF) = &app.currentlyEditing {
match eF {
EditingField::FromIP => {
app.fromIP.pop();
}
EditingField::FromPort => {
app.fromPort.pop();
}
EditingField::ToIP => {
app.toIP.pop();
}
EditingField::ToPort => {
app.toPort.pop();
}
};
}
}
(KeyModifiers::NONE, KeyCode::Esc) => {
app.screen = CurrentScreen::Main;
app.currentlyEditing = None;
}
(KeyModifiers::NONE, KeyCode::Tab) => app.nextField(),
(KeyModifiers::SHIFT, KeyCode::Tab) => app.prevField(),
(m, KeyCode::Char(v)) => {
if let Some(e) = &app.currentlyEditing {
let mut isIP = false;
let opField = match e {
EditingField::FromIP => {
isIP = true;
&mut app.fromIP
}
EditingField::FromPort => &mut app.fromPort,
EditingField::ToIP => {
isIP = true;
&mut app.toIP
}
EditingField::ToPort => &mut app.toPort,
};
match v {
'l' => {
if isIP {
opField.clear();
opField.push_str("127.0.0.1");
}
}
'c' => opField.clear(),
'C' => {
app.fromIP.clear();
app.toIP.clear();
app.fromPort.clear();
app.toPort.clear();
}
_ => opField.push(v),
}
}
}
_ => {}
},
CurrentScreen::Exit => match key.code {
KeyCode::Enter => {
app.save();
return Ok(true);
}
KeyCode::Esc | KeyCode::Char('m') => app.screen = CurrentScreen::Main,
_ => {}
},
}
}
}
// app.run(terminal)
}