refactoring, clippy fixes
This commit is contained in:
@@ -104,14 +104,6 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn print(&self) {
|
|
||||||
// let tempEntry = &self.entries[0];
|
|
||||||
// let res = serde_json::to_string(tempEntry).unwrap();
|
|
||||||
// println!("{res}");
|
|
||||||
// let resString = tempEntry.to_string();
|
|
||||||
// println!("{resString}");
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn saveConfigToScript(&self) {
|
pub fn saveConfigToScript(&self) {
|
||||||
let mut outputString: String = String::new();
|
let mut outputString: String = String::new();
|
||||||
outputString.push_str("#! /bin/bash\n\n# DO NOT EDIT THIS FILE MANUALLY\n# ANY MODIFICATIONS WILL BE OVERWRITTEN BY STECKBRETT\n# USE THAT STECKBRETT (stb) TO CONFIGURE PORT MAPPINGS.\n\n");
|
outputString.push_str("#! /bin/bash\n\n# DO NOT EDIT THIS FILE MANUALLY\n# ANY MODIFICATIONS WILL BE OVERWRITTEN BY STECKBRETT\n# USE THAT STECKBRETT (stb) TO CONFIGURE PORT MAPPINGS.\n\n");
|
||||||
@@ -122,19 +114,12 @@ impl AppState {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
outputString.push_str("\nwait");
|
outputString.push_str("\nwait");
|
||||||
// for o in 0..1 {
|
|
||||||
// println!("test {o}");
|
|
||||||
// sleep(Duration::from_secs(1));
|
|
||||||
// }
|
|
||||||
println!("Writing config to system");
|
println!("Writing config to system");
|
||||||
sleep(Duration::from_millis(100));
|
sleep(Duration::from_millis(100));
|
||||||
let _ = std::fs::write("./forward.sh", outputString);
|
let _ = std::fs::write("./forward.sh", outputString);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn saveConfigToSettingsFile(&mut self) {
|
pub fn saveConfigToSettingsFile(&mut self) {
|
||||||
// let mut settings = Settings::new(&self.confDir);
|
|
||||||
// settings.entries = self.entries.clone();
|
|
||||||
// settings.save(&self.confDir);
|
|
||||||
self.settings.entries = self.entries.clone();
|
self.settings.entries = self.entries.clone();
|
||||||
self.settings.save(&self.confDir);
|
self.settings.save(&self.confDir);
|
||||||
self.savedHash = self.settings.entryHash();
|
self.savedHash = self.settings.entryHash();
|
||||||
|
|||||||
11
src/ui/entryBox.rs
Normal file
11
src/ui/entryBox.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use ratatui::{
|
||||||
|
style::{Color, Style},
|
||||||
|
widgets::{Block, Borders},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn entryBox(title: String) -> Block<'static> {
|
||||||
|
Block::default()
|
||||||
|
.title(title)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().fg(Color::White))
|
||||||
|
}
|
||||||
66
src/ui/header.rs
Normal file
66
src/ui/header.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
use ratatui::{
|
||||||
|
style::{Color, Style},
|
||||||
|
text::Span,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::app::status::{AppStatus, CurrentScreen, EditingField, EntryValError};
|
||||||
|
|
||||||
|
pub fn getHeaderScreen(scr: &CurrentScreen) -> (Color, Span<'static>) {
|
||||||
|
match scr {
|
||||||
|
CurrentScreen::Main => (
|
||||||
|
Color::LightBlue,
|
||||||
|
Span::styled("Main Screen", Style::default().fg(Color::LightBlue)),
|
||||||
|
),
|
||||||
|
CurrentScreen::Add => (
|
||||||
|
Color::LightGreen,
|
||||||
|
Span::styled("Add Window", Style::default().fg(Color::Green)),
|
||||||
|
),
|
||||||
|
CurrentScreen::Exit => (
|
||||||
|
Color::LightRed,
|
||||||
|
Span::styled("Exit Screen", Style::default().fg(Color::Red)),
|
||||||
|
),
|
||||||
|
CurrentScreen::Settings => (
|
||||||
|
Color::LightBlue,
|
||||||
|
Span::styled("Settings", Style::default().fg(Color::Blue)),
|
||||||
|
),
|
||||||
|
CurrentScreen::Delete => (
|
||||||
|
Color::Magenta,
|
||||||
|
Span::styled("Delete", Style::default().fg(Color::Magenta)),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getHeaderStatus(status: &AppStatus, editingField: &Option<EditingField>) -> Span<'static> {
|
||||||
|
match status {
|
||||||
|
AppStatus::Welcome => Span::styled("Welcome", Style::default().fg(Color::White)),
|
||||||
|
AppStatus::Added => Span::styled("Added", Style::default().fg(Color::Green)),
|
||||||
|
AppStatus::Deleted => Span::styled("Deleted", Style::default().fg(Color::Magenta)),
|
||||||
|
AppStatus::Editing => {
|
||||||
|
if let Some(editing) = editingField {
|
||||||
|
let curEdit = match editing {
|
||||||
|
EditingField::FromIP => "From IP",
|
||||||
|
EditingField::ToIP => "To IP",
|
||||||
|
EditingField::FromPort => "From Port",
|
||||||
|
EditingField::ToPort => "To Port",
|
||||||
|
};
|
||||||
|
Span::styled(
|
||||||
|
format!("Editing: {curEdit}"),
|
||||||
|
Style::default().fg(Color::Green),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Span::styled("Not Editing", Style::default().fg(Color::White))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AppStatus::Error(e) => {
|
||||||
|
let errString = match e {
|
||||||
|
EntryValError::ToPortValError => "To Port Invalid",
|
||||||
|
EntryValError::FromPortValError => "From Port Invalid",
|
||||||
|
EntryValError::ToIPValError => "To IP Invalid",
|
||||||
|
EntryValError::FromIPValError => "From IP Invalid",
|
||||||
|
EntryValError::None => "",
|
||||||
|
};
|
||||||
|
Span::styled(errString, Style::default().fg(Color::Red))
|
||||||
|
}
|
||||||
|
AppStatus::Saved => Span::styled("Saved", Style::default().fg(Color::Green)),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
mod centeredRect;
|
mod centeredRect;
|
||||||
|
mod entryBox;
|
||||||
mod entryTable;
|
mod entryTable;
|
||||||
mod exitPrompt;
|
mod exitPrompt;
|
||||||
|
mod header;
|
||||||
mod textHints;
|
mod textHints;
|
||||||
|
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
@@ -9,15 +11,16 @@ use ratatui::{
|
|||||||
layout::{Constraint, Direction, Layout},
|
layout::{Constraint, Direction, Layout},
|
||||||
style::{Color, Style},
|
style::{Color, Style},
|
||||||
text::{Line, Span, Text},
|
text::{Line, Span, Text},
|
||||||
widgets::{Block, Borders, Clear, Padding, Paragraph},
|
widgets::{Block, Borders, Clear, Paragraph},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::app::status::{AppStatus, CurrentScreen, EntryValError};
|
use crate::app::status::CurrentScreen;
|
||||||
use crate::app::{AppState, status::EditingField};
|
use crate::app::{AppState, status::EditingField};
|
||||||
use crate::ui::centeredRect::centered_rect;
|
use crate::ui::centeredRect::centered_rect;
|
||||||
use crate::ui::textHints::hints;
|
use crate::ui::textHints::hints;
|
||||||
|
|
||||||
pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
||||||
|
// -------------------------------------------
|
||||||
// Split entire body into left title+body and right screen+keybinds chunks
|
// Split entire body into left title+body and right screen+keybinds chunks
|
||||||
let screenChunks = Layout::default()
|
let screenChunks = Layout::default()
|
||||||
.direction(ratatui::layout::Direction::Horizontal)
|
.direction(ratatui::layout::Direction::Horizontal)
|
||||||
@@ -36,7 +39,7 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
.constraints([Constraint::Length(3), Constraint::Min(3)])
|
.constraints([Constraint::Length(3), Constraint::Min(3)])
|
||||||
.split(screenChunks[1]);
|
.split(screenChunks[1]);
|
||||||
|
|
||||||
// ------------------------
|
// -------------------------------------------
|
||||||
// Create and add title block
|
// Create and add title block
|
||||||
let titleBlock = Block::default()
|
let titleBlock = Block::default()
|
||||||
.borders(Borders::all())
|
.borders(Borders::all())
|
||||||
@@ -50,6 +53,7 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
|
|
||||||
frame.render_widget(title, titleBodyChunks[0]);
|
frame.render_widget(title, titleBodyChunks[0]);
|
||||||
|
|
||||||
|
// -------------------------------------------
|
||||||
// table drawing
|
// table drawing
|
||||||
let table = entryTable::getTableElement(
|
let table = entryTable::getTableElement(
|
||||||
&app.entries,
|
&app.entries,
|
||||||
@@ -60,6 +64,7 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
|
|
||||||
frame.render_stateful_widget(table, titleBodyChunks[1], &mut app.tableState);
|
frame.render_stateful_widget(table, titleBodyChunks[1], &mut app.tableState);
|
||||||
|
|
||||||
|
// -------------------------------------------
|
||||||
// Renter exit prompt
|
// Renter exit prompt
|
||||||
if let CurrentScreen::Exit = app.screen {
|
if let CurrentScreen::Exit = app.screen {
|
||||||
let area = centered_rect(60, 25, titleBodyChunks[1]);
|
let area = centered_rect(60, 25, titleBodyChunks[1]);
|
||||||
@@ -67,6 +72,7 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
frame.render_widget(exitPrompt::getExitPara(), area);
|
frame.render_widget(exitPrompt::getExitPara(), area);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------
|
||||||
// Editing screen popup
|
// Editing screen popup
|
||||||
if let Some(edit) = &app.currentlyEditing {
|
if let Some(edit) = &app.currentlyEditing {
|
||||||
let popup = Block::default()
|
let popup = Block::default()
|
||||||
@@ -84,27 +90,14 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
Constraint::Percentage(30),
|
Constraint::Percentage(30),
|
||||||
Constraint::Percentage(20),
|
Constraint::Percentage(20),
|
||||||
Constraint::Percentage(30),
|
Constraint::Percentage(30),
|
||||||
Constraint::Percentage(25),
|
Constraint::Percentage(20),
|
||||||
])
|
])
|
||||||
.split(area);
|
.split(area);
|
||||||
|
|
||||||
let mut fromIPBlock = Block::default()
|
let mut fromIPBlock = entryBox::entryBox("From IP".to_string());
|
||||||
.title("From IP")
|
let mut toIPBlock = entryBox::entryBox("To IP".to_string());
|
||||||
.borders(Borders::ALL)
|
let mut fromPortBlock = entryBox::entryBox("From Port".to_string());
|
||||||
.style(Style::default().fg(Color::White));
|
let mut toPortBlock = entryBox::entryBox("To Port".to_string());
|
||||||
let mut toIPBlock = Block::default()
|
|
||||||
.title("To IP")
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.style(Style::default().fg(Color::White));
|
|
||||||
let mut fromPortBlock = Block::default()
|
|
||||||
.title("From Port")
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.style(Style::default().fg(Color::White));
|
|
||||||
let mut toPortBlock = Block::default()
|
|
||||||
.title("To Port")
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.style(Style::default().fg(Color::White));
|
|
||||||
|
|
||||||
let activeStyle = Style::default().bg(Color::LightYellow).fg(Color::Black);
|
let activeStyle = Style::default().bg(Color::LightYellow).fg(Color::Black);
|
||||||
|
|
||||||
match edit {
|
match edit {
|
||||||
@@ -130,64 +123,13 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
let mut borderColor = Color::White;
|
let mut borderColor = Color::White;
|
||||||
let _ = borderColor; // to suppress warning, remove later
|
let _ = borderColor; // to suppress warning, remove later
|
||||||
let screenHeader = vec![
|
let screenHeader = vec![
|
||||||
match app.screen {
|
|
||||||
CurrentScreen::Main => {
|
|
||||||
borderColor = Color::LightBlue;
|
|
||||||
Span::styled("Main Screen", Style::default().fg(Color::LightBlue))
|
|
||||||
}
|
|
||||||
CurrentScreen::Add => {
|
|
||||||
borderColor = Color::LightGreen;
|
|
||||||
Span::styled("Add Window", Style::default().fg(Color::Green))
|
|
||||||
}
|
|
||||||
CurrentScreen::Exit => {
|
|
||||||
borderColor = Color::LightRed;
|
|
||||||
Span::styled("Exit Screen", Style::default().fg(Color::Red))
|
|
||||||
}
|
|
||||||
CurrentScreen::Settings => {
|
|
||||||
borderColor = Color::LightBlue;
|
|
||||||
Span::styled("Settings", Style::default().fg(Color::Blue))
|
|
||||||
}
|
|
||||||
CurrentScreen::Delete => {
|
|
||||||
borderColor = Color::Magenta;
|
|
||||||
Span::styled("Delete", Style::default().fg(Color::Magenta))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.to_owned(),
|
|
||||||
Span::styled(" | ", Style::default().fg(Color::White)),
|
|
||||||
{
|
{
|
||||||
match &app.appStatus {
|
let (color, span) = header::getHeaderScreen(&app.screen);
|
||||||
AppStatus::Welcome => Span::styled("Welcome", Style::default().fg(Color::White)),
|
borderColor = color;
|
||||||
AppStatus::Added => Span::styled("Added", Style::default().fg(Color::Green)),
|
span
|
||||||
AppStatus::Deleted => Span::styled("Deleted", Style::default().fg(Color::Magenta)),
|
|
||||||
AppStatus::Editing => {
|
|
||||||
if let Some(editing) = &app.currentlyEditing {
|
|
||||||
let curEdit = match editing {
|
|
||||||
EditingField::FromIP => "From IP",
|
|
||||||
EditingField::ToIP => "To IP",
|
|
||||||
EditingField::FromPort => "From Port",
|
|
||||||
EditingField::ToPort => "To Port",
|
|
||||||
};
|
|
||||||
Span::styled(
|
|
||||||
format!("Editing: {curEdit}"),
|
|
||||||
Style::default().fg(Color::Green),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Span::styled("Not Editing", Style::default().fg(Color::White))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AppStatus::Error(e) => {
|
|
||||||
let errString = match e {
|
|
||||||
EntryValError::ToPortValError => "To Port Invalid",
|
|
||||||
EntryValError::FromPortValError => "From Port Invalid",
|
|
||||||
EntryValError::ToIPValError => "To IP Invalid",
|
|
||||||
EntryValError::FromIPValError => "From IP Invalid",
|
|
||||||
EntryValError::None => "",
|
|
||||||
};
|
|
||||||
Span::styled(errString, Style::default().fg(Color::Red))
|
|
||||||
}
|
|
||||||
AppStatus::Saved => Span::styled("Saved", Style::default().fg(Color::Green)),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
Span::styled(" | ", Style::default().fg(Color::White)),
|
||||||
|
header::getHeaderStatus(&app.appStatus, &app.currentlyEditing),
|
||||||
];
|
];
|
||||||
|
|
||||||
let screenHeaderPara = Paragraph::new(Line::from(screenHeader)).block(
|
let screenHeaderPara = Paragraph::new(Line::from(screenHeader)).block(
|
||||||
@@ -195,7 +137,9 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
.borders(Borders::TOP | Borders::LEFT | Borders::RIGHT)
|
.borders(Borders::TOP | Borders::LEFT | Borders::RIGHT)
|
||||||
.style(Style::default().fg(borderColor)),
|
.style(Style::default().fg(borderColor)),
|
||||||
);
|
);
|
||||||
|
frame.render_widget(screenHeaderPara, headerKeybindChunks[0]);
|
||||||
|
|
||||||
|
// -------------------------------------------
|
||||||
// Keybinds Entry
|
// Keybinds Entry
|
||||||
let keybinds = {
|
let keybinds = {
|
||||||
match app.screen {
|
match app.screen {
|
||||||
@@ -213,6 +157,5 @@ pub fn ui(frame: &mut Frame, app: &mut AppState) {
|
|||||||
.style(Style::default().fg(borderColor)),
|
.style(Style::default().fg(borderColor)),
|
||||||
);
|
);
|
||||||
|
|
||||||
frame.render_widget(screenHeaderPara, headerKeybindChunks[0]);
|
|
||||||
frame.render_widget(keyBindFooter, headerKeybindChunks[1]);
|
frame.render_widget(keyBindFooter, headerKeybindChunks[1]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user