use std::{collections::HashSet, path::Path};
use super::{
CachedEntry, EntryImageResolver, HistoryLineCache, HistoryLineSlice, HistoryRenderSettings,
};
use crate::tui::{
path_links::{CheckedPaths, PathCheck, PathIndex, PathLinker},
text_links::RowLink,
Entry,
};
pub(super) fn entry_linker(
paths: Option<&PathIndex>,
settings: HistoryRenderSettings,
) -> PathLinker<'_> {
match paths {
Some(paths) if settings.path_icons => PathLinker::new(paths).with_icons(),
Some(paths) => PathLinker::new(paths),
None => PathLinker::off(),
}
}
impl CachedEntry {
pub(super) fn rendered_linked(&self) -> bool {
!self.links.is_empty() || !self.unlinked_paths.is_empty()
}
}
impl HistoryLineCache {
pub(in crate::tui) fn set_path_workspace(&mut self, workspace: &Path) {
if self
.paths
.as_ref()
.is_some_and(|paths| paths.workspace() == workspace)
{
return;
}
self.paths = Some(PathIndex::new(workspace.to_path_buf()));
self.invalidate_from(self.measured_from);
}
pub(in crate::tui) fn take_path_check(&mut self) -> Option<PathCheck> {
self.paths.as_mut()?.take_check()
}
pub(in crate::tui) fn apply_path_check(&mut self, checked: CheckedPaths) {
let Some(paths) = self.paths.as_mut() else {
return;
};
let found: HashSet<String> = paths.apply(checked).into_iter().collect();
if found.is_empty() {
return;
}
let mentioning: Vec<usize> = self
.entries
.iter()
.enumerate()
.filter(|(_, entry)| entry.unlinked_paths.iter().any(|path| found.contains(path)))
.map(|(index, _)| self.measured_from + index)
.collect();
self.resplice_entries(mentioning);
}
pub(in crate::tui) fn recheck_missing_paths(&mut self) {
if let Some(paths) = self.paths.as_mut() {
paths.recheck_missing();
}
}
pub(in crate::tui) fn visible_links(
&mut self,
entries: &[Entry],
settings: HistoryRenderSettings,
slice: HistoryLineSlice,
image_resolver: EntryImageResolver<'_>,
) -> Vec<RowLink> {
self.ensure_current(entries, settings, image_resolver);
let end = slice.start.saturating_add(slice.count);
let first = self
.entry_ranges
.partition_point(|range| range.end <= slice.start);
let mut visible = Vec::new();
for (entry, range) in self.entries[first..]
.iter()
.zip(&self.entry_ranges[first..])
{
if range.start >= end {
break;
}
for link in &entry.links {
let row = range.start + link.row;
if (slice.start..end).contains(&row) {
visible.push(RowLink {
row: row - slice.start,
columns: link.columns.clone(),
url: link.url.clone(),
});
}
}
}
visible
}
}