Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/log level filter #11

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add AppStates, Add Filter Dialog and create log level vec on st…
…artup, update input events to be state based
  • Loading branch information
hackinghieser committed Feb 17, 2024
commit 590980cb4fabf44ad83526f754c1fc797d8c6f34
71 changes: 57 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
use ratatui::widgets::{Row, TableState};

use crate::clef::ClefLine;
use ratatui::widgets::{ListState, Row, TableState};

#[derive(Debug)]
pub enum AppState {
FILTERING,
ITERATING,
}

impl Default for AppState {
fn default() -> Self {
AppState::ITERATING
}
}
#[derive(Debug, Default)]
pub struct App<'a> {
pub should_quit: bool,
pub counter: u8,
pub lines: Vec<ClefLine<'a>>,
pub rows: Vec<Row<'a>>,
pub table_state: TableState,
pub file_path: String
pub event_table_state: TableState,
pub filter_list_state: ListState,
pub file_path: String,
pub event_types: Vec<String>,
pub app_state: AppState,
}


impl<'a> App<'a> {
pub fn new() -> Self {
Self::default()
Expand All @@ -20,38 +33,68 @@ impl<'a> App<'a> {
pub fn tick(&self) {}

pub fn load_lines(&mut self, lines: &Vec<String>) {
self.lines = Self::create_cells_from_line(lines);
self.lines = self.create_cells_from_line(lines);
}

fn create_cells_from_line(lines: &Vec<String>) -> Vec<ClefLine<'a>> {
fn create_cells_from_line(&mut self, lines: &Vec<String>) -> Vec<ClefLine<'a>> {
let mut clef_lines: Vec<ClefLine<'_>> = vec![];

for line in lines {
clef_lines.push(ClefLine::new(line).unwrap())
}
self.get_event_types(&clef_lines);
clef_lines
}

pub fn get_event_types(&mut self, events: &Vec<ClefLine>) {
for event in events {
if !self.event_types.contains(&event.level) && &event.level != "" {
self.event_types.push(event.level.to_string());
}
}
println!("{:?}", self.event_types);
}

pub fn quit(&mut self) {
self.should_quit = true;
}

pub fn move_row_up(&mut self, range: usize) {
if let Some(selected) = self.table_state.selected() {
if let Some(selected) = self.event_table_state.selected() {
if selected >= range {
self.table_state.select(Some(selected - range));
self.event_table_state.select(Some(selected - range));
} else {
self.table_state.select(Some(self.lines.len() - 1));
self.event_table_state.select(Some(self.lines.len() - 1));
}
}
}

pub fn move_row_down(&mut self, range: usize) {
if let Some(selected) = self.table_state.selected() {
if let Some(selected) = self.event_table_state.selected() {
if selected < self.lines.len() - range {
self.table_state.select(Some(selected + range));
self.event_table_state.select(Some(selected + range));
} else {
self.event_table_state.select(Some(0));
}
}
}

pub fn move_list_up(&mut self) {
if let Some(selected) = self.filter_list_state.selected() {
if selected >= 1 {
self.filter_list_state.select(Some(selected - 1));
} else {
self.filter_list_state.select(Some(0));
}
}
}

pub fn move_list_down(&mut self) {
if let Some(selected) = self.filter_list_state.selected() {
if selected < self.event_types.len() {
self.filter_list_state.select(Some(selected + 1));
} else {
self.table_state.select(Some(0));
self.filter_list_state.select(Some(0));
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/clef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,29 @@ pub struct ClefLine<'a> {
}

impl<'a> ClefLine<'a> {

pub fn new(line: &str) -> Result<Self, Error> {
let mut clef: ClefLine = serde_json::from_str(line).unwrap();
clef.data = line.to_string();
clef.template = clef.render()?;
let time = DateTime::parse_from_rfc3339(clef.time.as_str());
let time = DateTime::parse_from_rfc3339(clef.time.as_str());
clef.time = time.unwrap().format("%d.%m.%y %H:%M:%S").to_string();
clef.row = Row::new(vec![
Cell::from(["[".to_string(),clef.time.to_string(),"|".to_string(), clef.level.to_string(), "]".to_string()].join("")),
Cell::from(
[
"[".to_string(),
clef.time.to_string(),
"|".to_string(),
clef.level.to_string(),
"]".to_string(),
]
.join(""),
),
Cell::from(clef.template.to_string()),
]);
Ok(clef)
}

pub fn render(&mut self) -> Result<String,Error> {
pub fn render(&mut self) -> Result<String, Error> {
let start_bracket = "{";
let end_bracket = "}";
let mut base = self.template.clone();
Expand Down
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ pub mod update;
// clef parser
pub mod clef;

use std::{ fs, io::{self}};
use std::{
fs,
io::{self},
};

use app::App;
use clap::Parser;
use event::{Event, EventHandler};
use ratatui::{backend::CrosstermBackend, widgets::TableState, Terminal};
use ratatui::{backend::CrosstermBackend, widgets::{ListState, TableState}, Terminal};
use tui::Tui;
use update::update;
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -70,8 +73,10 @@ fn create_app(path: String) -> Result<App<'static>, io::Error> {
let lines = read_file(path.as_str())?;
let mut app = App::new();
app.file_path = path;
app.table_state = TableState::new();
app.table_state.select(Some(0));
app.event_table_state = TableState::new();
app.filter_list_state = ListState::default();
app.filter_list_state.select(Some(0));
app.event_table_state.select(Some(0));
app.load_lines(&lines);
Ok(app)
}
Expand Down
Loading