fix some iteration bugs

This commit is contained in:
Jana Dönszelmann 2026-02-25 17:51:31 +01:00
parent de51666742
commit 8eab2502c7
No known key found for this signature in database
8 changed files with 143 additions and 89 deletions

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, fs::File, io::Write, iter, mem, sync::Arc};
use std::{collections::HashMap, iter, mem, path::PathBuf, sync::Arc};
use crate::tui::{
filter::Filter,
@ -9,6 +9,7 @@ use crate::tui::{
},
model::LogEntry,
processing::{FilterList, IntoLogStream, LogStream},
widgets::last_error::LastError,
};
use tui_widget_list::ListState;
@ -34,7 +35,9 @@ pub struct LogViewer {
}
impl LogViewer {
pub fn new(stream: impl LogStream) -> Self {
pub fn new(stream: impl LogStream, filters_path: PathBuf, error: LastError) -> Self {
let filters = Filters::new(filters_path, error);
let stream = stream.with_filters(filters.get());
Self {
stack: Vec::new(),
curr: LogView {
@ -50,7 +53,7 @@ impl LogViewer {
last_fields_offset: 0,
last_fields_height: 0,
filters: Filters::new(),
filters,
input_state: InputState::None,
}
}
@ -71,20 +74,20 @@ impl LogViewer {
)))
.collect();
let mut log = File::options()
.create(true)
.append(true)
.open("./log")
.unwrap();
// let mut log = File::options()
// .create(true)
// .append(true)
// .open("./log")
// .unwrap();
writeln!(&mut log, "active filters: {:?}", self.filters.get()).unwrap();
// writeln!(&mut log, "active filters: {:?}", self.filters.get()).unwrap();
writeln!(
&mut log,
"{:?}",
offsets_list.iter().map(|i| i.1).collect::<Vec<_>>()
)
.unwrap();
// writeln!(
// &mut log,
// "{:?}",
// offsets_list.iter().map(|i| i.1).collect::<Vec<_>>()
// )
// .unwrap();
let find_elem_in_stream =
|stream: &dyn LogStream, elem: &Arc<LogEntry>| -> Option<Box<dyn LogStream>> {
@ -104,35 +107,35 @@ impl LogViewer {
for (elem, old_offset) in offsets_list {
let Some((elem, inline_depth)) = elem else {
writeln!(&mut log, "no elem").unwrap();
// writeln!(&mut log, "no elem").unwrap();
break;
};
writeln!(
&mut log,
"reconstruction {:?} prev at {old_offset:?}",
elem.message_or_name()
)
.unwrap();
// writeln!(
// &mut log,
// "reconstruction {:?} prev at {old_offset:?}",
// elem.message_or_name()
// )
// .unwrap();
// find the nearest stream in which this element can be found
let mut curr = current_stream.as_ref();
let mut parents = new_stack.iter().rev();
let mut found_in_toplevel = true;
let mut stream = loop {
writeln!(&mut log, "find in stream").unwrap();
// writeln!(&mut log, "find in stream").unwrap();
if let Some(stream) = find_elem_in_stream(curr, &elem) {
writeln!(&mut log, "found (toplevel={found_in_toplevel})").unwrap();
// writeln!(&mut log, "found (toplevel={found_in_toplevel})").unwrap();
break stream;
}
found_in_toplevel = false;
if let Some(parent) = parents.next() {
writeln!(&mut log, "searching parent").unwrap();
// writeln!(&mut log, "searching parent").unwrap();
curr = parent.iter.as_ref();
continue;
}
writeln!(&mut log, "no more parents").unwrap();
// writeln!(&mut log, "no more parents").unwrap();
missing = true;
// TODO: better guess at how far down we need to go
@ -155,7 +158,7 @@ impl LogViewer {
0
};
writeln!(&mut log, "new offset: {offset:?}").unwrap();
// writeln!(&mut log, "new offset: {offset:?}").unwrap();
let nested_stream = elem.from_start(inline_depth);
new_stack.push(LogView {
@ -174,29 +177,29 @@ impl LogViewer {
}
}
writeln!(
&mut log,
"{:?}",
new_stack
.iter()
.map(|i| (
i.iter
.clone()
.next(self.filters.get())
.map(|i| i.0.message_or_name()),
i.selection_offset
))
.collect::<Vec<_>>()
)
.unwrap();
// writeln!(
// &mut log,
// "{:?}",
// new_stack
// .iter()
// .map(|i| (
// i.iter
// .clone()
// .next(self.filters.get())
// .map(|i| i.0.message_or_name()),
// i.selection_offset
// ))
// .collect::<Vec<_>>()
// )
// .unwrap();
// Take the top of the stack as `curr`, unless
// the new stack is empty, then just reset the current view.
if let Some(curr) = new_stack.pop() {
writeln!(&mut log, "popped new curr off stack").unwrap();
// writeln!(&mut log, "popped new curr off stack").unwrap();
self.curr = curr;
} else {
writeln!(&mut log, "reconstructing root, somehow").unwrap();
// writeln!(&mut log, "reconstructing root, somehow").unwrap();
self.curr = LogView {
iter: self.filtered_root_stream().clone(),
selection_offset: self.stack.first().unwrap_or(&self.curr).selection_offset,