handle * ip, ui refac, fix #1 to handle .conf
This commit is contained in:
108
src/ui/mod.rs
108
src/ui/mod.rs
@@ -1,13 +1,15 @@
|
||||
#![allow(non_snake_case)]
|
||||
mod centeredRect;
|
||||
mod entryTable;
|
||||
mod exitPrompt;
|
||||
mod textHints;
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Direction, Layout},
|
||||
style::{Color, Modifier, Style},
|
||||
style::{Color, Style},
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Block, Borders, Cell, Paragraph, Row, Table, Wrap},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
};
|
||||
|
||||
use crate::app::status::{AppStatus, CurrentScreen, EntryValError};
|
||||
@@ -48,104 +50,22 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
||||
|
||||
frame.render_widget(title, titleBodyChunks[0]);
|
||||
|
||||
// Create and add body
|
||||
// let mut currentItems = Vec::<ListItem>::new();
|
||||
// for (i, e) in app.entries.iter().enumerate() {
|
||||
// currentItems.push(ListItem::new(Line::from(Span::styled(
|
||||
// format!("{i}. {e}"),
|
||||
// Style::default().fg(Color::Yellow),
|
||||
// ))));
|
||||
// }
|
||||
|
||||
// table drawing
|
||||
let headers = ["No.", "From IP", "From Port", "-->", "To IP", "To Port"]
|
||||
.into_iter()
|
||||
.map(Cell::from)
|
||||
.collect::<Row>()
|
||||
.style(Style::default().fg(Color::White).bg(Color::Black))
|
||||
.height(2);
|
||||
|
||||
let dataRows = app.entries.iter().enumerate().map(|(i, d)| {
|
||||
let item = d.array(i + 1);
|
||||
item.into_iter()
|
||||
.map(|content| Cell::from(Text::from(format!("\n{content}\n"))))
|
||||
.collect::<Row>()
|
||||
.style(Style::new().fg(Color::White).bg(Color::Black))
|
||||
.height(2)
|
||||
});
|
||||
let selectedColor = match app.screen {
|
||||
CurrentScreen::Delete => Color::Yellow,
|
||||
_ => Color::DarkGray,
|
||||
};
|
||||
let tableBorderColor = match app.appStatus {
|
||||
AppStatus::Saved => Color::LightGreen,
|
||||
AppStatus::Welcome => Color::White,
|
||||
_ => {
|
||||
if app.isHashDifferent() {
|
||||
Color::LightYellow
|
||||
} else {
|
||||
Color::White
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// if app.appStatus == AppStatus::Saved {
|
||||
// Color::LightGreen
|
||||
// } else if app.appStatus == AppStatus::Welcome {
|
||||
// Color::White
|
||||
// } else {
|
||||
// if app.isHashDifferent() {
|
||||
// Color::LightYellow
|
||||
// } else {
|
||||
// Color::White
|
||||
// }
|
||||
// };
|
||||
let table = Table::new(
|
||||
dataRows,
|
||||
[
|
||||
Constraint::Percentage(4),
|
||||
Constraint::Percentage(28),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(4),
|
||||
Constraint::Percentage(28),
|
||||
Constraint::Percentage(18),
|
||||
],
|
||||
)
|
||||
.header(headers)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::all())
|
||||
.style(Style::default().fg(tableBorderColor)),
|
||||
)
|
||||
.row_highlight_style(
|
||||
Style::default()
|
||||
.add_modifier(Modifier::REVERSED)
|
||||
.fg(selectedColor),
|
||||
let table = entryTable::getTableElement(
|
||||
&app.entries,
|
||||
&app.screen,
|
||||
&app.appStatus,
|
||||
app.isHashDifferent(),
|
||||
);
|
||||
|
||||
// let list = List::new(currentItems).block(Block::default().borders(Borders::all()));
|
||||
frame.render_stateful_widget(table, titleBodyChunks[1], &mut app.tableState);
|
||||
|
||||
// Renter exit prompt
|
||||
if let CurrentScreen::Exit = app.screen {
|
||||
let exitPopup = Block::default()
|
||||
.title("Exit Window")
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default().bg(Color::DarkGray))
|
||||
.border_style(Style::default().fg(Color::Red));
|
||||
|
||||
let exitText = Text::from(vec![
|
||||
Line::from("Exit the app?"),
|
||||
Line::from("ANY UNSAVED CHANGES WILL BE DISCARDED."),
|
||||
Line::from("Press (s) to save from the main screen."),
|
||||
]);
|
||||
let exitPara = Paragraph::new(exitText)
|
||||
.block(exitPopup)
|
||||
.wrap(Wrap { trim: false })
|
||||
.style(Style::default().fg(Color::White));
|
||||
|
||||
let area = centered_rect(60, 25, titleBodyChunks[1]);
|
||||
frame.render_widget(exitPara, area);
|
||||
frame.render_widget(
|
||||
exitPrompt::getExitPara(),
|
||||
centered_rect(60, 25, titleBodyChunks[1]),
|
||||
);
|
||||
}
|
||||
|
||||
// Editing screen popup
|
||||
@@ -154,7 +74,7 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
||||
.title("Add an entry")
|
||||
.borders(Borders::NONE)
|
||||
.style(Style::default().bg(Color::DarkGray));
|
||||
let area = centered_rect(75, 20, titleBodyChunks[1]);
|
||||
let area = centered_rect(75, 30, titleBodyChunks[1]);
|
||||
frame.render_widget(popup, area);
|
||||
|
||||
let fields = Layout::default()
|
||||
|
||||
Reference in New Issue
Block a user