added color coding, new state, improved save logic
This commit is contained in:
@@ -3,13 +3,12 @@ pub mod entry;
|
||||
mod settings;
|
||||
|
||||
pub mod status;
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
use crate::app::{
|
||||
entry::Entry,
|
||||
settings::Settings,
|
||||
status::{AppStatus, CurrentScreen, EditingField, EntryValError},
|
||||
};
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
pub struct AppState {
|
||||
pub fromIP: String,
|
||||
@@ -22,6 +21,9 @@ pub struct AppState {
|
||||
pub confDir: String,
|
||||
pub tableState: TableState,
|
||||
pub appStatus: AppStatus,
|
||||
pub savedHash: String,
|
||||
pub currentHash: String,
|
||||
pub settings: Settings,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -34,13 +36,24 @@ impl AppState {
|
||||
toPort: String::new(),
|
||||
currentlyEditing: None,
|
||||
screen: CurrentScreen::Main,
|
||||
entries: settings.entries,
|
||||
savedHash: settings.entryHash(),
|
||||
currentHash: settings.entryHash(),
|
||||
entries: settings.entries.clone(),
|
||||
settings: settings,
|
||||
confDir,
|
||||
tableState: TableState::default().with_selected(0),
|
||||
appStatus: AppStatus::Welcome,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn genCurrentEntriesHash(&mut self) {
|
||||
let mut entries = String::new();
|
||||
for e in &self.entries {
|
||||
entries.push_str(&format!("{}", e));
|
||||
}
|
||||
self.currentHash = format!("{:x}", md5::compute(entries));
|
||||
}
|
||||
|
||||
pub fn store(&mut self) -> EntryValError {
|
||||
match Entry::new(
|
||||
self.fromIP.clone(),
|
||||
@@ -56,12 +69,17 @@ impl AppState {
|
||||
self.toPort = String::new();
|
||||
self.currentlyEditing = None;
|
||||
self.tableState.select(Some(self.entries.len() - 1_usize));
|
||||
self.genCurrentEntriesHash();
|
||||
EntryValError::None
|
||||
}
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isHashDifferent(&self) -> bool {
|
||||
self.currentHash != self.savedHash
|
||||
}
|
||||
|
||||
pub fn nextField(&mut self) {
|
||||
if let Some(currentField) = &self.currentlyEditing {
|
||||
self.currentlyEditing = match currentField {
|
||||
@@ -92,9 +110,9 @@ impl AppState {
|
||||
println!("{resString}");
|
||||
}
|
||||
|
||||
pub fn writeToConfig(&self) {
|
||||
pub fn saveConfigToScript(&self) {
|
||||
let mut outputString: String = String::new();
|
||||
outputString.push_str("#! /bin/bash\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");
|
||||
for ent in self.entries.iter() {
|
||||
outputString.push_str(&format!(
|
||||
"socat TCP-LISTEN:{},fork,reuseaddr,bind={} TCP:{}:{} &\n",
|
||||
@@ -105,10 +123,13 @@ impl AppState {
|
||||
let _ = std::fs::write("./forward.sh", outputString);
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
let mut settings = Settings::new(&self.confDir);
|
||||
settings.entries = self.entries.clone();
|
||||
settings.save(&self.confDir);
|
||||
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.save(&self.confDir);
|
||||
self.savedHash = self.settings.entryHash();
|
||||
}
|
||||
|
||||
pub fn nextRow(&mut self) {
|
||||
@@ -145,5 +166,7 @@ impl AppState {
|
||||
return;
|
||||
}
|
||||
self.entries.remove(self.tableState.selected().unwrap());
|
||||
self.genCurrentEntriesHash();
|
||||
self.appStatus = AppStatus::Deleted;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user