use super::Browser;
use ratatui::text::Line;
pub(crate) struct CachedBlob {
pub(crate) name: String,
pub(crate) lang: String,
pub(crate) text: String,
pub(crate) lines: Vec<Line<'static>>,
}
impl Browser {
pub fn show_at_commit(
&mut self,
sha: &str,
name: &str,
lang: &str,
text: String,
lines: Vec<Line<'static>>,
band: Option<crate::components::preview::BandContext>,
) {
self.note_commit_view();
self.blobs.insert(
sha.to_string(),
CachedBlob {
name: name.to_string(),
lang: lang.to_string(),
text,
lines: lines.clone(),
},
);
self.preview.set_highlighted(name, lang, lines);
let path = name.split(" @ ").next().unwrap_or(name).to_string();
self.preview.set_band(Some(path), band);
}
pub fn commit_view_close(&mut self) {
let Some((path, sha)) = self.at_commit.take() else {
return;
};
if let Some(c) = self.blobs.get(&sha) {
let (lang, lines) = (c.lang.clone(), c.lines.clone());
self.preview.set_highlighted(&path, &lang, lines);
self.preview.set_band(Some(path), None);
} else {
self.preview
.set_bytes(&path, b"reload the file to restore it");
}
}
pub fn blob_loaded(
&mut self,
sha: &str,
name: &str,
lang: &str,
text: String,
lines: Vec<Line<'static>>,
) {
self.blobs.insert(
sha.to_string(),
CachedBlob {
name: name.to_string(),
lang: lang.to_string(),
text,
lines,
},
);
self.pending_blobs.remove(sha);
self.refresh_preview();
if let Some((path, s)) = self.selected_file()
&& s == sha
{
self.preview.set_band(Some(path), None);
}
}
pub fn blob_failed(&mut self, sha: &str, message: &str) {
self.pending_blobs.remove(sha);
self.failed_blobs
.insert(sha.to_string(), message.to_string());
if let Some((_, selected)) = self.selected_file()
&& selected == sha
{
self.preview.content =
crate::components::preview::PreviewContent::Text(format!("error: {message}"));
}
}
pub fn retry_failed_blobs(&mut self) {
self.failed_blobs.clear();
self.refresh_preview();
}
}