added colours, select and delete ops, refac
This commit is contained in:
@@ -3,6 +3,8 @@ pub mod entry;
|
||||
mod settings;
|
||||
|
||||
pub mod status;
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
use crate::app::{
|
||||
entry::Entry,
|
||||
settings::Settings,
|
||||
@@ -18,6 +20,7 @@ pub struct AppState {
|
||||
pub currentlyEditing: Option<EditingField>,
|
||||
pub entries: Vec<Entry>,
|
||||
pub confDir: String,
|
||||
pub tableState: TableState,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -32,6 +35,7 @@ impl AppState {
|
||||
screen: CurrentScreen::Main,
|
||||
entries: settings.entries,
|
||||
confDir,
|
||||
tableState: TableState::default().with_selected(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +53,8 @@ impl AppState {
|
||||
self.fromPort = String::new();
|
||||
self.toPort = String::new();
|
||||
self.currentlyEditing = None;
|
||||
|
||||
self.tableState
|
||||
.select(Some(self.entries.len() - 1 as usize));
|
||||
EntryCreation::Success
|
||||
}
|
||||
_ => EntryCreation::PortValidationError,
|
||||
@@ -104,4 +109,40 @@ impl AppState {
|
||||
settings.entries = self.entries.clone();
|
||||
settings.save(&self.confDir);
|
||||
}
|
||||
|
||||
pub fn nextRow(&mut self) {
|
||||
let i = match self.tableState.selected() {
|
||||
Some(i) => {
|
||||
if i >= self.entries.len() - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.tableState.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn prevRow(&mut self) {
|
||||
let i = match self.tableState.selected() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
self.entries.len() - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.tableState.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn delCur(&mut self) {
|
||||
if self.entries.is_empty() {
|
||||
self.screen = CurrentScreen::Main;
|
||||
return;
|
||||
}
|
||||
self.entries.remove(self.tableState.selected().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user