use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget};
use crate::app::App;
use crate::doc::links::LinkKind;
pub struct LinksMode<'a> {
app: &'a App,
}
impl<'a> LinksMode<'a> {
pub fn new(app: &'a App) -> Self {
LinksMode { app }
}
fn lines(&self) -> Vec<Line<'static>> {
let theme = &self.app.theme;
let Some(doc) = self.app.tab().doc.as_ref() else {
return vec![Line::styled("no document", theme.sidebar.file_other)];
};
let mut lines = vec![Line::styled("outgoing", theme.sidebar.mode_active)];
if doc.links.is_empty() {
lines.push(Line::styled(" none", theme.sidebar.file_other));
}
for link in &doc.links {
let broken = self.app.is_broken(link);
let style = match (broken, link.kind) {
(true, _) => theme.markdown.link_broken,
(_, LinkKind::Wiki) => theme.markdown.wikilink,
(_, LinkKind::Autolink) => theme.markdown.link_external,
(_, LinkKind::Inline) => theme.markdown.link,
};
let marker = if broken { "✗ " } else { " " };
lines.push(Line::from(vec![
Span::styled(marker, style),
Span::styled(link.text.clone(), style),
]));
}
lines.push(Line::from(""));
lines.push(Line::styled(
self.backlink_heading(),
theme.sidebar.mode_active,
));
if !self.app.wikilinks.enabled {
lines.push(Line::styled(
" wiki-links are off",
theme.sidebar.file_other,
));
return lines;
}
if !self.app.vault.index.ready {
return lines;
}
let backlinks = self.app.backlinks();
if backlinks.is_empty() {
lines.push(Line::styled(" none", theme.sidebar.file_other));
}
for backlink in backlinks {
lines.push(Line::from(Span::styled(
format!(" {}:{}", backlink.source.display(), backlink.line),
theme.sidebar.file,
)));
lines.push(Line::from(Span::styled(
format!(" {}", backlink.context),
theme.sidebar.file_other,
)));
}
lines
}
fn backlink_heading(&self) -> String {
let index = &self.app.vault.index;
match (index.ready, index.total) {
(true, _) => "backlinks".to_string(),
(false, Some(total)) => format!("indexing… ({}/{total} files)", index.indexed),
(false, None) => "indexing…".to_string(),
}
}
}
impl Widget for LinksMode<'_> {
fn render(self, area: Rect, buf: &mut ratatui::buffer::Buffer) {
if area.height == 0 {
return;
}
Paragraph::new(self.lines()).render(area, buf);
}
}