use ratatui::text::Line;
use shep_core::protocol::ProcessInfo;
use shep_core::status::ProcStatus;
use crate::lookout::app::{App, KeyPress, Msg};
use crate::lookout::level::Level;
use crate::lookout::tail::{Stream, Tail, TailLine};
use crate::lookout::theme::Palette;
use super::flock::{app_with, flock_of, with_selection};
use super::palette::plain;
pub fn with_feed(tail: Tail) -> App {
let mut app = app_with(flock_of(3, 0), plain());
app.update(Msg::Bleats { tail });
app
}
pub fn with_feed_and_selection(tail: Tail, id: u32) -> App {
let mut app = app_with(flock_of(3, 0), plain());
for _ in 0..id {
app.update(Msg::Key(KeyPress::SelectDown));
}
app.update(Msg::Bleats { tail });
app
}
pub fn with_feed_and_palette(tail: Tail, palette: Palette) -> App {
let mut app = app_with(flock_of(3, 0), palette);
app.update(Msg::Bleats { tail });
app
}
pub fn bleats_pane_with_filters() -> App {
let mut app = with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
app.update(Msg::Bleats {
tail: Tail {
lines: vec![
line(Stream::Err, "ERROR pool exhausted"), line(Stream::Out, "ERROR pool exhausted"), line(Stream::Err, "INFO pool warming"), line(Stream::Err, "ERROR disk full"), ],
missed_lines: 0,
missed_bytes: 0,
read_bytes: 128,
note: None,
},
});
app.update(Msg::Key(KeyPress::Bleats));
let pane = app
.bleats_pane_mut_for_tests()
.expect("Msg::Key(KeyPress::Bleats) opened the pane on the sheep selected above");
pane.set_stream(Some(Stream::Err));
pane.set_min_level(Some(Level::Warn));
pane.set_match("pool".to_string());
app
}
pub fn bleats_pane_with_lines(n: u32) -> App {
let mut app = with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
app.update(Msg::Bleats {
tail: Tail {
lines: (0..n)
.map(|i| line(Stream::Out, &format!("line-{i}")))
.collect(),
missed_lines: 0,
missed_bytes: 0,
read_bytes: 1_024,
note: None,
},
});
app.update(Msg::Key(KeyPress::Bleats));
app
}
#[must_use]
pub fn bleats_pane_with_mixed_line_lengths() -> App {
let mut app = with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
let mut lines: Vec<TailLine> = (0..40)
.map(|i| {
let padding = "y".repeat(50 * (i % 4));
line(Stream::Out, &format!("old-{i} {padding}"))
})
.collect();
lines.extend((0..40).map(|i| line(Stream::Out, &format!("new-{i}"))));
app.update(Msg::Bleats {
tail: Tail {
lines,
missed_lines: 0,
missed_bytes: 0,
read_bytes: 1_024,
note: None,
},
});
app.update(Msg::Key(KeyPress::Bleats));
app
}
#[must_use]
pub fn bleats_pane_with_a_wide_line() -> App {
let mut app = with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
app.update(Msg::Bleats {
tail: Tail {
lines: vec![line(Stream::Out, &"\u{5e83}".repeat(60))],
missed_lines: 0,
missed_bytes: 0,
read_bytes: 1_024,
note: None,
},
});
app.update(Msg::Key(KeyPress::Bleats));
app
}
pub fn bleats_pane_with_long_line() -> App {
let mut app = with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
app.update(Msg::Bleats {
tail: Tail {
lines: vec![
line(Stream::Out, "short line"),
line(Stream::Out, &"x".repeat(200)),
],
missed_lines: 0,
missed_bytes: 0,
read_bytes: 1_024,
note: None,
},
});
app.update(Msg::Key(KeyPress::Bleats));
app
}
pub fn draw_lines(app: &App, width: u16, rows: usize) -> Vec<Line<'static>> {
crate::lookout::view::bleats_full::draw_lines(app, width, rows)
}
pub fn app_fixture() -> App {
let dir = tempfile::Builder::new()
.prefix("shep-fx-")
.tempdir()
.expect("a tempdir for the fixture's logs");
let out_path = dir.path().join("catcher-out.log");
let err_path = dir.path().join("catcher-err.log");
std::fs::write(&out_path, b"listening on :8080\n").expect("write the out log");
std::fs::write(&err_path, b"warn: retrying upstream\n").expect("write the err log");
let _ = dir.keep();
let info = ProcessInfo::builder(7, "catcher", ProcStatus::Online)
.pid(Some(48_107))
.uptime_ms(4_512_000)
.out_file(Some(out_path.display().to_string()))
.err_file(Some(err_path.display().to_string()))
.build();
let mut app = with_selection(info);
app.update(Msg::Bleats {
tail: Tail {
lines: vec![
line(Stream::Out, "listening on :8080"),
line(Stream::Err, "warn: retrying upstream"),
],
missed_lines: 0,
missed_bytes: 0,
read_bytes: 1_024,
note: None,
},
});
let total_bytes = std::fs::metadata(&out_path)
.ok()
.zip(std::fs::metadata(&err_path).ok())
.map(|(out, err)| out.len() + err.len());
app.update(Msg::LogSize { id: 7, total_bytes });
app
}
pub fn line(stream: Stream, text: &str) -> TailLine {
TailLine::new(stream, text)
}