Skip to content

Commit

Permalink
Made it so we can switch between views!
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeaster30 committed Dec 12, 2024
1 parent ee464ea commit c9ca59a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/simple_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl SimpleDatabase {
}

pub fn get_all_cached_records(&self) -> Result<Vec<CachedDnsRecord>> {
//todo!();
Ok(Vec::new())
}

Expand Down
9 changes: 8 additions & 1 deletion src/tui/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl App {
frame.render_stateful_widget(self, frame.area(), state);
}

pub fn handle_events(&mut self, state: &AppState) -> Result<()> {
pub fn handle_events(&mut self, state: &mut AppState) -> Result<()> {
let mut current_view = &mut self.views[state.current_view()];
match event::poll(current_view.poll_rate()) {
Ok(true) => {
Expand All @@ -89,6 +89,13 @@ impl App {
SimpleEvent::Key(key) if key.kind == KeyEventKind::Press && key.code == KeyCode::Esc => {
self.exit = true;
}
SimpleEvent::Key(key) if key.kind == KeyEventKind::Press => {
for (idx, view) in self.views.iter().enumerate() {
if key.code == view.open_view_control() {
state.selected_view = ListState::default().with_selected(Some(idx))
}
}
}
_ => {}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/tui/cache_list_view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::{Block, Chart, Dataset, Paragraph, Row, Table, Widget}};
use ratatui::{buffer::Buffer, crossterm::event::KeyCode, layout::Rect, text::{Line, Text}, widgets::{Block, Chart, Dataset, Paragraph, Row, Table, Widget}};
use ratatui::prelude::Stylize;
use ratatui::prelude::Style;

Expand Down Expand Up @@ -30,7 +30,7 @@ impl View for CacheListView {
Ok(records) => {
Table::default()
.rows(records.iter().map(|x| x.into()).collect::<Vec<Row<'_>>>())
.header(Row::new(vec!["Query Type", "Domain", "Host/IP", "Priority", "TTL", "Class"]).underlined().cyan())
.header(Row::new(vec!["Query Type", "Domain", "Host/IP", "Priority", "Expires In", "Class"]).underlined().cyan())
.row_highlight_style(Style::new().underlined())
.highlight_symbol("->")
.block(block)
Expand All @@ -53,6 +53,10 @@ impl View for CacheListView {
SimpleEventResult::Bubble
}

fn open_view_control(&self) -> KeyCode {
KeyCode::Char('c')
}

fn name(&self) -> Line {
Line::from(vec![
" ".into(),
Expand Down
6 changes: 5 additions & 1 deletion src/tui/record_list_view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::{Block, Chart, Dataset, Paragraph, Row, Table, Widget}};
use ratatui::{buffer::Buffer, crossterm::event::KeyCode, layout::Rect, text::{Line, Text}, widgets::{Block, Chart, Dataset, Paragraph, Row, Table, Widget}};
use ratatui::prelude::Stylize;
use ratatui::prelude::Style;

Expand Down Expand Up @@ -53,6 +53,10 @@ impl View for RecordListView {
SimpleEventResult::Bubble
}

fn open_view_control(&self) -> KeyCode {
KeyCode::Char('r')
}

fn name(&self) -> Line {
Line::from(vec![
" ".into(),
Expand Down
3 changes: 2 additions & 1 deletion src/tui/view.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::time::Duration;

use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::{Block, Widget}};
use ratatui::{buffer::Buffer, crossterm::event::KeyCode, layout::Rect, text::{Line, Text}, widgets::{Block, Widget}};

use super::event::{SimpleEvent, SimpleEventResult};

pub trait View {
fn draw(&self, block: Block, area: Rect, buf: &mut Buffer);
fn handle_event(&mut self, event: SimpleEvent) -> SimpleEventResult;
fn open_view_control(&self) -> KeyCode;
fn name(&self) -> Line;
fn help(&self) -> Text;
fn poll_rate(&self) -> Duration;
Expand Down

0 comments on commit c9ca59a

Please sign in to comment.