use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
use crossbeam_channel::Receiver;
use ratatui::layout::Rect;
use crate::action::Action;
use crate::config::keymap::Keymap;
use crate::config::schema::{
EditorConfig, FilesConfig, GeneralConfig, SearchConfig, SessionConfig, ThemeConfig, UiConfig,
WatchConfig, WikiLinkConfig,
};
use crate::doc::document::Document;
use crate::doc::highlight::Highlighter;
use crate::doc::links;
use crate::doc::render::{RenderedDocument, Renderer};
use crate::editor::buffer::{self, EditorState, SaveError};
use crate::search::content::{self, SearchEvent, SearchHandle};
use crate::search::in_doc::FindState;
use crate::search::SearchState;
use crate::terminal::{self, Tui};
use crate::theme::Theme;
use crate::ui;
use crate::ui::layout::{self, SidebarPlacement};
use crate::ui::overlay::prompt::{TextEdit, TextInput};
use crate::ui::sidebar::SidebarMode;
use crate::vault::index::{self, IndexEvent, WikiResolution};
use crate::vault::walker::{WalkEvent, WalkOptions};
use crate::vault::watch::WatchEvent;
use crate::vault::Vault;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Focus {
Sidebar,
#[default]
Viewport,
Overlay,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TabMode {
#[default]
Read,
Edit,
}
impl TabMode {
pub fn label(self) -> &'static str {
match self {
TabMode::Read => "READ",
TabMode::Edit => "EDIT",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
pub path: PathBuf,
pub anchor: Option<String>,
pub scroll: usize,
}
pub const MAX_HISTORY: usize = 100;
#[derive(Debug, Clone, Default)]
pub struct History {
back: Vec<Location>,
forward: Vec<Location>,
}
impl History {
pub fn push(&mut self, from: Location) {
self.forward.clear();
self.back.push(from);
if self.back.len() > MAX_HISTORY {
self.back.remove(0);
}
}
pub fn back(&mut self, current: Location) -> Option<Location> {
let previous = self.back.pop()?;
self.forward.push(current);
Some(previous)
}
pub fn forward(&mut self, current: Location) -> Option<Location> {
let next = self.forward.pop()?;
self.back.push(current);
Some(next)
}
pub fn back_len(&self) -> usize {
self.back.len()
}
pub fn forward_len(&self) -> usize {
self.forward.len()
}
}
#[derive(Debug, Default)]
pub struct Tab {
pub doc: Option<Document>,
pub layout: RenderedDocument,
pub scroll: usize,
pub hscroll: u16,
pub mode: TabMode,
pub history: History,
pub focused_link: Option<usize>,
pub find: Option<FindState>,
pub editor: Option<EditorState>,
pub dirty: bool,
}
impl Tab {
pub fn with_document(doc: Document) -> Self {
Tab {
doc: Some(doc),
..Tab::default()
}
}
pub fn label(&self) -> &str {
match &self.doc {
Some(doc) => doc.label(),
None => "welcome",
}
}
pub fn display_label(&self) -> String {
let label = truncate_middle(self.label(), MAX_TAB_LABEL);
if self.dirty {
format!("{DIRTY_MARKER} {label}")
} else {
label
}
}
fn max_scroll(&self, height: u16) -> Option<usize> {
let doc = self.doc.as_ref()?;
let total = self.layout.total_lines(doc)?;
Some(total.saturating_sub(usize::from(height).max(1)))
}
}
pub const SWITCHER_ROWS: usize = 100;
pub const MAX_RECENT: usize = 50;
pub const MAX_TABS: usize = 20;
pub const MAX_TAB_LABEL: usize = 20;
pub const DIRTY_MARKER: &str = "●";
fn offset_of_source_line(source: &str, line: usize) -> usize {
if line == 0 {
return 0;
}
source
.match_indices('\n')
.nth(line - 1)
.map_or(source.len(), |(at, _)| at + 1)
}
fn with_markdown_extension(query: &str, files: &FilesConfig) -> String {
let query = query.trim();
if files.is_markdown(Path::new(query)) {
return query.to_string();
}
let extension = files.extensions.first().map_or("md", String::as_str);
format!("{query}.{extension}")
}
pub fn truncate_middle(label: &str, width: usize) -> String {
use unicode_width::UnicodeWidthChar;
let total: usize = label.chars().map(|c| c.width().unwrap_or(0)).sum();
if total <= width || width < 3 {
return label.to_string();
}
let keep = width - 1;
let head_width = keep.div_ceil(2);
let tail_width = keep - head_width;
let mut head = String::new();
let mut used = 0;
for c in label.chars() {
let w = c.width().unwrap_or(0);
if used + w > head_width {
break;
}
head.push(c);
used += w;
}
let mut tail = String::new();
let mut used = 0;
for c in label.chars().rev() {
let w = c.width().unwrap_or(0);
if used + w > tail_width {
break;
}
tail.insert(0, c);
used += w;
}
format!("{head}…{tail}")
}
#[derive(Debug, Clone)]
pub struct Sidebar {
pub visible: bool,
pub width: u16,
pub mode: SidebarMode,
pub filter: Option<TextInput>,
pub outline_selected: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
Info,
Warning,
Error,
}
#[derive(Debug, Clone, Default)]
pub struct StatusLine {
pub message: Option<(String, Severity)>,
pub pending: Option<String>,
}
impl StatusLine {
pub fn set(&mut self, text: impl Into<String>, severity: Severity) {
self.message = Some((text.into(), severity));
}
pub fn clear(&mut self) {
self.message = None;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Overlay {
Help {
scroll: u16,
},
Find,
Disambiguate {
page: String,
candidates: Vec<PathBuf>,
anchor: Option<String>,
selected: usize,
},
Prompt {
kind: PromptKind,
input: TextInput,
},
Switcher {
input: TextInput,
rows: Vec<SwitcherRow>,
selected: usize,
},
Confirm {
question: String,
choices: Vec<(char, String)>,
action: ConfirmAction,
},
Hints {
links: Vec<usize>,
typed: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfirmAction {
LeaveEditMode,
Quit,
OverwriteChanged,
CreatePage {
path: PathBuf,
},
RestoreRecovery {
path: PathBuf,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptKind {
ProjectSearch,
NewFile,
RenameDocument,
RenameTreeEntry,
}
impl PromptKind {
pub fn prefix(self) -> &'static str {
match self {
PromptKind::ProjectSearch => "search: ",
PromptKind::NewFile => "new file: ",
PromptKind::RenameDocument | PromptKind::RenameTreeEntry => "rename to: ",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwitcherRow {
pub path: PathBuf,
pub indices: Vec<u32>,
pub create: bool,
}
pub struct App {
pub vault: Vault,
pub files: FilesConfig,
pub general: GeneralConfig,
pub wikilinks: WikiLinkConfig,
pub search_config: SearchConfig,
pub editor_config: EditorConfig,
pub own_writes: crate::vault::watch::OwnWrites,
pub external_edit: Option<PathBuf>,
pub watch_config: WatchConfig,
pub session_config: SessionConfig,
pub theme_config: ThemeConfig,
pub theme_pinned: bool,
watch: Option<crate::vault::watch::WatchHandle>,
theme_watch: Option<crate::vault::watch::WatchHandle>,
pub search: SearchState,
pub recent: Vec<PathBuf>,
search_handle: Option<SearchHandle>,
fuzzy: crate::search::fuzzy::Fuzzy,
search_sink: Option<Arc<dyn Fn(SearchEvent) + Send + Sync>>,
index_sink: Option<Arc<dyn Fn(IndexEvent) + Send + Sync>>,
pub theme: Theme,
pub highlighter: Highlighter,
pub keymap: Keymap,
pub ui: UiConfig,
pub tabs: Vec<Tab>,
pub active_tab: usize,
pub sidebar: Sidebar,
pub focus: Focus,
pub overlay: Option<Overlay>,
pub status: StatusLine,
pub should_quit: bool,
pub should_suspend: bool,
pub exit_code: u8,
pub area: Rect,
pub mouse_capture: bool,
}
impl App {
pub fn new(theme: Theme, keymap: Keymap, ui: UiConfig, files: FilesConfig) -> Self {
let sidebar = Sidebar {
visible: ui.sidebar_visible,
width: ui.sidebar_width,
mode: ui.sidebar_default_mode,
filter: None,
outline_selected: 0,
};
let mouse_capture = ui.mouse;
let mut status = StatusLine::default();
if let Some(warning) = keymap.warnings().first() {
status.set(warning.clone(), Severity::Warning);
}
App {
vault: Vault::new(".", &files),
files,
general: GeneralConfig::default(),
wikilinks: WikiLinkConfig::default(),
search_config: SearchConfig::default(),
editor_config: EditorConfig::default(),
own_writes: crate::vault::watch::OwnWrites::default(),
external_edit: None,
watch_config: WatchConfig::default(),
session_config: SessionConfig::default(),
theme_config: ThemeConfig::default(),
theme_pinned: false,
watch: None,
theme_watch: None,
search: SearchState::default(),
recent: Vec::new(),
search_handle: None,
fuzzy: crate::search::fuzzy::Fuzzy::new(),
search_sink: None,
index_sink: None,
theme,
highlighter: Highlighter::new(),
keymap,
ui,
tabs: vec![Tab::default()],
active_tab: 0,
sidebar,
focus: Focus::Viewport,
overlay: None,
status,
should_quit: false,
should_suspend: false,
exit_code: 0,
area: Rect::default(),
mouse_capture,
}
}
pub fn tab(&self) -> &Tab {
&self.tabs[self.active_tab]
}
pub fn title_path(&self) -> Option<String> {
self.tab()
.doc
.as_ref()
.map(|doc| doc.display_path(&self.vault.root))
}
pub fn active_path(&self) -> Option<PathBuf> {
let doc = self.tab().doc.as_ref()?;
self.vault.relative(&doc.path).map(Path::to_path_buf)
}
pub fn scroll_position(&self) -> Option<(usize, usize)> {
let tab = self.tab();
let doc = tab.doc.as_ref()?;
let total = tab.layout.total_lines(doc)?;
Some((tab.scroll.saturating_add(1).min(total.max(1)), total))
}
pub fn set_vault_root(&mut self, root: impl AsRef<Path>) {
self.vault = Vault::new(root.as_ref(), &self.files);
}
pub fn start_walk(&mut self, sink: impl Fn(WalkEvent) + Send + Sync + 'static) {
let options = WalkOptions {
respect_gitignore: self.files.respect_gitignore,
follow_symlinks: self.general.follow_symlinks,
};
self.vault.start_walk(options, sink);
}
pub fn on_index(&mut self, sink: impl Fn(IndexEvent) + Send + Sync + 'static) {
self.index_sink = Some(Arc::new(sink));
}
pub fn on_search(&mut self, sink: impl Fn(SearchEvent) + Send + Sync + 'static) {
self.search_sink = Some(Arc::new(sink));
}
pub fn renderer(&self, width: u16) -> Renderer {
Renderer::new(&self.theme, self.highlighter.clone(), width)
.with_heading_markers(self.ui.show_heading_markers)
}
pub fn viewport_inner(&self) -> Rect {
let viewport = self.frames().viewport;
Rect {
x: viewport.x.saturating_add(1),
y: viewport.y.saturating_add(1),
width: viewport.width.saturating_sub(2),
height: viewport.height.saturating_sub(2),
}
}
pub fn open(&mut self, doc: Document) {
if let Some(reason) = doc.read_only {
self.status.set(reason.message(), Severity::Warning);
}
let path = doc.path.clone();
let tab = &mut self.tabs[self.active_tab];
tab.doc = Some(doc);
tab.layout = RenderedDocument::new();
tab.scroll = 0;
tab.hscroll = 0;
tab.focused_link = None;
self.remember(&path);
self.reveal_active_document();
}
pub fn frames(&self) -> layout::Frames {
layout::compute(layout::LayoutInput {
area: self.area,
sidebar_visible: self.sidebar.visible,
sidebar_width: self.sidebar.width,
narrow_threshold: self.ui.narrow_threshold,
tab_count: self.tabs.len(),
always_show_tabs: self.ui.always_show_tabs,
show_status_bar: self.ui.show_status_bar,
})
}
pub fn update(&mut self, action: Action) {
if !matches!(action, Action::Resize(..)) {
self.status.clear();
}
match action {
Action::Quit => self.quit(),
Action::ForceQuit(code) => {
self.write_recovery_files();
self.should_quit = true;
self.exit_code = code;
}
Action::Suspend => self.should_suspend = true,
Action::Resize(width, height) => {
self.area = Rect::new(0, 0, width, height);
}
Action::FocusNext => self.cycle_focus(true),
Action::FocusPrev => self.cycle_focus(false),
Action::ToggleSidebar => self.toggle_sidebar(),
Action::SidebarWiden => self.resize_sidebar(1),
Action::SidebarNarrow => self.resize_sidebar(-1),
Action::SetSidebarMode(mode) => {
self.sidebar.mode = mode;
if !self.sidebar.visible {
self.sidebar.visible = true;
}
}
Action::ToggleMouse => self.toggle_mouse(),
Action::ToggleHelp => self.toggle_help(),
Action::Escape => self.escape(),
Action::SyntaxReady => self.status.clear(),
Action::ScrollLineDown => self.scroll_by(1),
Action::ScrollLineUp => self.scroll_by(-1),
Action::ScrollHalfPageDown => self.scroll_by(i64::from(self.page() / 2).max(1)),
Action::ScrollHalfPageUp => self.scroll_by(-i64::from(self.page() / 2).max(1)),
Action::ScrollPageDown => self.scroll_by(i64::from(self.page()).max(1)),
Action::ScrollPageUp => self.scroll_by(-i64::from(self.page()).max(1)),
Action::ScrollWheelDown => {
self.scroll_by(i64::from(self.ui.mouse_scroll_lines).max(1));
}
Action::ScrollWheelUp => {
self.scroll_by(-i64::from(self.ui.mouse_scroll_lines).max(1));
}
Action::ScrollTop => self.scroll_to_top(),
Action::ScrollBottom => self.scroll_to_bottom(),
Action::ScrollLeft => self.scroll_horizontally(-1),
Action::ScrollRight => self.scroll_horizontally(1),
Action::PrevHeading => self.scroll_to_heading(false),
Action::NextHeading => self.scroll_to_heading(true),
Action::VaultEntries(entries) => {
for entry in &entries {
if !entry.is_dir && self.files.is_markdown(&entry.path) {
self.vault
.markdown
.push((entry.path.clone(), entry.mtime, entry.size));
}
}
self.vault.tree.insert_all(entries);
self.reveal_active_document();
}
Action::VaultWalkFinished(total) => {
self.vault.tree.complete = true;
self.vault.tree.entries = total;
self.reveal_active_document();
self.start_indexing();
}
Action::IndexBatch(entries) => {
for (path, entry) in entries {
self.vault.index.insert(path, entry);
}
}
Action::IndexFinished => self.finish_indexing(),
Action::FilesChanged(paths) => self.files_changed(&paths),
Action::FilesRemoved(paths) => self.files_removed(&paths),
Action::WatchStopped(reason) => self.status.set(reason, Severity::Warning),
Action::ReloadTheme => self.reload_theme(),
Action::CycleTheme => self.cycle_theme(),
Action::VaultWalkFailed(reason) => self.status.set(reason, Severity::Error),
Action::SidebarDown => self.move_sidebar_selection(1),
Action::SidebarUp => self.move_sidebar_selection(-1),
Action::SidebarActivate => self.activate_sidebar_selection(),
Action::SidebarBack => self.sidebar_back(),
Action::TreeToggleHidden => {
self.vault.tree.toggle_hidden();
let state = if self.vault.tree.include_hidden {
"shown"
} else {
"hidden"
};
self.status
.set(format!("Dotted entries {state}"), Severity::Info);
}
Action::TreeToggleAllFiles => {
self.vault.tree.toggle_all_files();
let state = if self.vault.tree.show_all {
"All files"
} else {
"Markdown files only"
};
self.status.set(state, Severity::Info);
}
Action::TreeFilter if self.sidebar.mode == SidebarMode::Search => {
self.open_prompt(PromptKind::ProjectSearch);
}
Action::TreeFilter => self.open_tree_filter(),
Action::TreeFilterEdit(edit) => self.edit_tree_filter(edit),
Action::TreeFilterAccept => self.sidebar.filter = None,
Action::TreeFilterCancel => {
self.sidebar.filter = None;
self.vault.tree.set_filter(None);
}
Action::OpenPath(path) => self.open_path(&path),
Action::OpenProjectSearch => self.open_prompt(PromptKind::ProjectSearch),
Action::PromptEdit(edit) => self.edit_prompt(edit),
Action::PromptAccept => self.accept_prompt(),
Action::SearchHits(hits) => {
self.search.hits.extend(hits);
}
Action::SearchFinished { total, truncated } => {
self.search.running = false;
self.search.truncated = truncated;
self.search.elapsed = self.search.started.map(|at| at.elapsed());
let _ = total;
self.search_handle = None;
}
Action::SearchFailed(error) => {
self.search.running = false;
self.search.error = Some(error);
self.search_handle = None;
}
Action::OpenQuickSwitcher => self.open_switcher(),
Action::SwitcherEdit(edit) => self.edit_switcher(edit),
Action::SwitcherMove(delta) => self.move_switcher(delta),
Action::SwitcherAccept { new_tab } => self.accept_switcher(new_tab),
Action::EnterEditMode => self.enter_edit_mode(),
Action::EditInput(key) => self.edit_input(key),
Action::EditPaste(text) => self.edit_paste(&text),
Action::Save => self.save(false),
Action::Undo => self.edit_history(false),
Action::Redo => self.edit_history(true),
Action::ReloadDocument => self.reload_document(),
Action::NewFile => self.open_prompt(PromptKind::NewFile),
Action::RenameDocument => self.open_prompt(PromptKind::RenameDocument),
Action::TreeRename => self.open_prompt(PromptKind::RenameTreeEntry),
Action::OpenInExternalEditor => self.hand_to_external_editor(),
Action::CopyDocumentPath => self.copy_document_path(),
Action::OpenFindInDocument => self.open_find(),
Action::FindEdit(edit) => self.edit_find(edit),
Action::FindNext => self.step_find(true),
Action::FindPrev => self.step_find(false),
Action::CloseFind => self.close_find(),
Action::NewTab => self.new_tab(),
Action::CloseTab => self.close_tab(),
Action::NextTab => self.switch_tab(1),
Action::PrevTab => self.switch_tab(-1),
Action::NextLink => self.cycle_link(true),
Action::PrevLink => self.cycle_link(false),
Action::FollowLink => self.follow_focused_link(),
Action::FollowLinkInNewTab => self.follow_focused_link_in_new_tab(),
Action::HintMode => self.open_hint_mode(),
Action::FollowHintedLink(index) => self.follow_link(index),
Action::ChooseCandidate(index) => self.choose_candidate(index),
Action::Confirm(key) => self.confirm(key),
Action::HistoryBack => self.navigate_history(false),
Action::HistoryForward => self.navigate_history(true),
}
}
fn page(&self) -> u16 {
self.viewport_inner().height.max(1)
}
fn scroll_by(&mut self, delta: i64) {
let height = self.page();
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let wanted = (tab.scroll as i64 + delta).max(0) as usize;
if delta > 0 {
tab.layout.window(doc, &renderer, wanted, height);
}
tab.scroll = match tab.max_scroll(height) {
Some(max) => wanted.min(max),
None => wanted.min(tab.layout.measured_lines()),
};
}
fn scroll_to_top(&mut self) {
self.tabs[self.active_tab].scroll = 0;
}
fn scroll_to_bottom(&mut self) {
let height = self.page();
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
if !tab.layout.resolve_all(doc, &renderer) {
self.status.set("Measuring the document…", Severity::Info);
}
if let Some(max) = tab.max_scroll(height) {
tab.scroll = max;
} else {
tab.scroll = tab.layout.measured_lines();
}
}
fn scroll_horizontally(&mut self, delta: i16) {
let tab = &mut self.tabs[self.active_tab];
if tab.doc.is_none() {
return;
}
tab.hscroll =
(i32::from(tab.hscroll) + i32::from(delta)).clamp(0, i32::from(u16::MAX)) as u16;
}
fn scroll_to_heading(&mut self, forward: bool) {
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let offsets: Vec<usize> = doc.outline.iter().map(|h| h.offset).collect();
let current = tab.scroll;
let mut target = None;
for offset in offsets {
let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) else {
continue;
};
if forward && line > current {
target = Some(line);
break;
}
if !forward && line < current {
target = Some(line);
}
}
if let Some(line) = target {
tab.scroll = line;
}
}
fn tree_expand_or_open(&mut self) {
if self.vault.tree.expand_selected() {
return;
}
let Some(node) = self.vault.tree.selected() else {
return;
};
let path = node.path.clone();
self.open_path(&path);
}
fn open_path(&mut self, relative: &Path) {
let absolute = self.vault.absolute(relative);
if !self.files.is_markdown(&absolute) {
match crate::vault::open_external(&absolute) {
Ok(()) => self.status.set(
format!("Opened {} externally", relative.display()),
Severity::Info,
),
Err(e) => self
.status
.set(format!("Cannot open externally: {e}"), Severity::Error),
}
return;
}
match Document::load(&absolute) {
Ok(document) => {
self.open(document);
if self.sidebar.visible
&& self.frames().sidebar_placement == SidebarPlacement::Overlaid
{
self.sidebar.visible = false;
self.focus = Focus::Viewport;
}
}
Err(e) => self.status.set(
format!("Cannot read {}: {e}", relative.display()),
Severity::Error,
),
}
}
fn reveal_active_document(&mut self) {
if let Some(path) = self.active_path() {
self.vault.tree.reveal(&path);
}
}
fn open_tree_filter(&mut self) {
let existing = self.vault.tree.filter().unwrap_or_default().to_string();
self.sidebar.filter = Some(TextInput::with_value(existing));
self.sidebar.visible = true;
self.focus = Focus::Sidebar;
}
fn edit_tree_filter(&mut self, edit: TextEdit) {
let Some(input) = &mut self.sidebar.filter else {
return;
};
input.apply(edit);
let value = input.value().to_string();
self.vault.tree.set_filter(Some(value));
}
fn location(&self) -> Option<Location> {
let tab = self.tab();
let doc = tab.doc.as_ref()?;
Some(Location {
path: doc.path.clone(),
anchor: None,
scroll: tab.scroll,
})
}
fn cycle_link(&mut self, forward: bool) {
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
let count = doc.links.len();
if count == 0 {
self.status.set("No links in this document", Severity::Info);
return;
}
let next = match self.tab().focused_link {
Some(at) if forward => (at + 1) % count,
Some(at) => (at + count - 1) % count,
None if forward => 0,
None => count - 1,
};
self.tabs[self.active_tab].focused_link = Some(next);
self.scroll_focused_link_into_view();
}
fn scroll_focused_link_into_view(&mut self) {
let height = usize::from(self.page());
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let Some(index) = tab.focused_link else {
return;
};
let Some(link) = doc.links.get(index) else {
return;
};
let Some(line) = tab.layout.line_of_offset(doc, &renderer, link.range.start) else {
return;
};
if line < tab.scroll {
tab.scroll = line;
} else if line >= tab.scroll + height {
tab.scroll = line.saturating_sub(height.saturating_sub(1));
}
}
pub fn follow_focused_link(&mut self) {
let Some(index) = self.tab().focused_link else {
self.status
.set("No link focused; press `n` or `f`", Severity::Info);
return;
};
self.follow_link(index);
}
pub fn follow_link(&mut self, index: usize) {
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
let Some(link) = doc.links.get(index) else {
return;
};
let target = link.target.clone();
if link.kind == crate::doc::links::LinkKind::Wiki {
self.follow_wiki_link(&target);
return;
}
let resolved = links::resolve(&target, doc.dir(), &self.vault.root, &self.files);
match resolved {
links::Resolved::Document { path, anchor } => self.navigate_to(&path, anchor),
links::Resolved::Anchor { slug } => self.jump_to_anchor(&slug),
links::Resolved::Directory { path } => self.reveal_directory(&path),
links::Resolved::External { url } => self.open_externally(Path::new(&url), &url),
links::Resolved::Other { path } => {
let shown = path.display().to_string();
self.open_externally(&path, &shown);
}
links::Resolved::Broken { target } => self
.status
.set(format!("Cannot resolve: {target}"), Severity::Error),
}
}
fn navigate_to(&mut self, path: &Path, anchor: Option<String>) {
let from = self.location();
match Document::load(path) {
Ok(document) => {
if let Some(from) = from {
self.tabs[self.active_tab].history.push(from);
}
self.open(document);
if let Some(slug) = anchor {
self.jump_to_anchor(&slug);
}
}
Err(e) => self.status.set(
format!("Cannot read {}: {e}", path.display()),
Severity::Error,
),
}
}
fn jump_to_anchor(&mut self, slug: &str) {
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let Some(offset) = doc.heading(slug).map(|h| h.offset) else {
self.status
.set(format!("No heading `{slug}`"), Severity::Warning);
return;
};
if let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) {
tab.scroll = line;
}
}
fn reveal_directory(&mut self, path: &Path) {
let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) else {
self.status.set(
format!("{} is outside the vault", path.display()),
Severity::Warning,
);
return;
};
if !self.vault.tree.reveal(&relative) {
self.status.set(
format!("{} is not in the tree yet", relative.display()),
Severity::Info,
);
return;
}
self.sidebar.mode = SidebarMode::Files;
self.sidebar.visible = true;
self.focus = Focus::Sidebar;
}
fn open_externally(&mut self, path: &Path, shown: &str) {
match crate::vault::open_external(path) {
Ok(()) => self.status.set(format!("Opened {shown}"), Severity::Info),
Err(e) => self
.status
.set(format!("Cannot open {shown}: {e}"), Severity::Error),
}
}
fn navigate_history(&mut self, forward: bool) {
let Some(current) = self.location() else {
return;
};
let index = self.active_tab;
let step = if forward {
self.tabs[index].history.forward(current)
} else {
self.tabs[index].history.back(current)
};
let Some(target) = step else {
let direction = if forward { "forward" } else { "back" };
self.status
.set(format!("No further {direction}"), Severity::Info);
return;
};
let same = self
.tab()
.doc
.as_ref()
.is_some_and(|d| d.path == target.path);
if !same {
match Document::load(&target.path) {
Ok(document) => self.open(document),
Err(e) => {
self.status.set(
format!("Cannot read {}: {e}", target.path.display()),
Severity::Error,
);
return;
}
}
}
self.restore_scroll(target.scroll);
}
fn restore_scroll(&mut self, scroll: usize) {
let height = self.page();
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
tab.layout.window(doc, &renderer, scroll, height);
tab.scroll = match tab.max_scroll(height) {
Some(max) => scroll.min(max),
None => scroll.min(tab.layout.measured_lines()),
};
}
fn open_hint_mode(&mut self) {
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
if doc.links.is_empty() {
self.status.set("No links in this document", Severity::Info);
return;
}
let visible = self.visible_links();
if visible.is_empty() {
self.status.set("No links in view", Severity::Info);
return;
}
self.overlay = Some(Overlay::Hints {
links: visible,
typed: String::new(),
});
self.focus = Focus::Overlay;
}
pub fn visible_links(&self) -> Vec<usize> {
let Some(doc) = self.tab().doc.as_ref() else {
return Vec::new();
};
let tab = self.tab();
let height = usize::from(self.page());
let map = tab.layout.line_map();
let window = tab.scroll..tab.scroll + height;
doc.links
.iter()
.enumerate()
.filter(|(_, link)| {
map.line_of_offset(link.range.start)
.is_some_and(|line| window.contains(&line))
})
.map(|(index, _)| index)
.collect()
}
fn enter_edit_mode(&mut self) {
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
if let Some(reason) = doc.read_only {
self.status.set(reason.message(), Severity::Warning);
return;
}
let line = self.source_line_at_scroll();
let mut editor = EditorState::new(doc);
editor
.textarea
.move_cursor(tui_textarea::CursorMove::Jump(line as u16, 0));
let index = self.active_tab;
self.tabs[index].editor = Some(editor);
self.tabs[index].mode = TabMode::Edit;
self.focus = Focus::Viewport;
if let Some(text) = self.pending_recovery() {
let path = self.tab().doc.as_ref().map(|d| d.path.clone());
if let Some(path) = path {
self.overlay = Some(Overlay::Confirm {
question: format!(
"Unsaved text for {} was left behind. Restore it?",
path.file_name().unwrap_or_default().to_string_lossy()
),
choices: vec![('y', "restore".into()), ('n', "discard".into())],
action: ConfirmAction::RestoreRecovery { path },
});
self.focus = Focus::Overlay;
let _ = text;
}
}
}
fn pending_recovery(&self) -> Option<String> {
let doc = self.tab().doc.as_ref()?;
buffer::read_recovery(&self.vault.root, &doc.path)
}
fn source_line_at_scroll(&self) -> usize {
let tab = self.tab();
let Some(doc) = tab.doc.as_ref() else {
return 0;
};
let Some(offset) = tab.layout.line_map().offset_of_line(tab.scroll) else {
return 0;
};
doc.source[..offset.min(doc.source.len())]
.matches('\n')
.count()
}
fn leave_edit_mode(&mut self) {
if self.tab().mode != TabMode::Edit {
return;
}
if self.tab().dirty {
self.overlay = Some(Overlay::Confirm {
question: "This document has unsaved edits.".to_string(),
choices: vec![
('s', "save".into()),
('d', "discard".into()),
('c', "cancel".into()),
],
action: ConfirmAction::LeaveEditMode,
});
self.focus = Focus::Overlay;
return;
}
self.finish_edit_mode();
}
fn finish_edit_mode(&mut self) {
let index = self.active_tab;
let cursor = self.tabs[index].editor.as_ref().map(|e| e.cursor().0);
self.tabs[index].editor = None;
self.tabs[index].mode = TabMode::Read;
self.tabs[index].dirty = false;
self.focus = Focus::Viewport;
if let Some(line) = cursor {
self.scroll_to_source_line(line);
}
}
fn scroll_to_source_line(&mut self, line: usize) {
let Some(offset) = self
.tab()
.doc
.as_ref()
.map(|doc| offset_of_source_line(&doc.source, line))
else {
return;
};
let renderer = self.renderer(self.viewport_inner().width);
let height = usize::from(self.page());
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let Some(rendered) = tab.layout.line_of_offset(doc, &renderer, offset) else {
return;
};
tab.scroll = rendered.saturating_sub(height / 3);
}
fn edit_input(&mut self, key: crossterm::event::KeyEvent) {
let index = self.active_tab;
let Some(editor) = &mut self.tabs[index].editor else {
return;
};
editor.textarea.input(key);
self.tabs[index].dirty = self.tabs[index]
.editor
.as_ref()
.is_some_and(EditorState::is_dirty);
}
fn edit_paste(&mut self, text: &str) {
let index = self.active_tab;
let Some(editor) = &mut self.tabs[index].editor else {
return;
};
editor.textarea.insert_str(text);
self.tabs[index].dirty = self.tabs[index]
.editor
.as_ref()
.is_some_and(EditorState::is_dirty);
}
fn edit_history(&mut self, redo: bool) {
let index = self.active_tab;
let Some(editor) = &mut self.tabs[index].editor else {
return;
};
if redo {
editor.textarea.redo();
} else {
editor.textarea.undo();
}
self.tabs[index].dirty = self.tabs[index]
.editor
.as_ref()
.is_some_and(EditorState::is_dirty);
}
pub fn save(&mut self, force: bool) {
let index = self.active_tab;
let Some(editor) = &self.tabs[index].editor else {
return;
};
let Some(doc) = &self.tabs[index].doc else {
return;
};
let path = doc.path.clone();
let contents = editor.contents();
let expected = (!force).then_some(editor.known_mtime);
match buffer::save(&path, &contents, expected) {
Ok(mtime) => {
self.own_writes.record(path.clone(), mtime);
if let Some(editor) = &mut self.tabs[index].editor {
editor.mark_saved(mtime);
}
self.tabs[index].dirty = false;
self.reload_from(&path, &contents, mtime);
buffer::clear_recovery(&self.vault.root, &path);
if let Some(relative) = self.vault.relative(&path).map(Path::to_path_buf) {
self.reindex(&relative);
}
self.status.set("Saved", Severity::Info);
}
Err(SaveError::Conflict { .. }) => {
self.overlay = Some(Overlay::Confirm {
question: format!(
"{} changed on disk since it was opened.",
path.file_name().unwrap_or_default().to_string_lossy()
),
choices: vec![
('o', "overwrite".into()),
('r', "reload and discard".into()),
('c', "cancel".into()),
],
action: ConfirmAction::OverwriteChanged,
});
self.focus = Focus::Overlay;
}
Err(e) => self
.status
.set(format!("Cannot save: {e}"), Severity::Error),
}
}
fn reload_from(&mut self, path: &Path, source: &str, mtime: SystemTime) {
let index = self.active_tab;
let Some(doc) = &self.tabs[index].doc else {
return;
};
let parsed = Document::from_source(
path.to_path_buf(),
source.to_string(),
mtime,
doc.had_bom,
doc.read_only,
);
self.tabs[index].doc = Some(parsed);
self.tabs[index].layout = RenderedDocument::new();
}
fn reload_document(&mut self) {
let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
return;
};
if self.tab().dirty {
self.status.set(
"This document has unsaved edits; save or discard first",
Severity::Warning,
);
return;
}
let scroll = self.tab().scroll;
match Document::load(&path) {
Ok(document) => {
self.open(document);
self.restore_scroll(scroll);
self.status.set("Reloaded", Severity::Info);
}
Err(e) => self.status.set(
format!("Cannot reload {}: {e}", path.display()),
Severity::Error,
),
}
}
fn copy_document_path(&mut self) {
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
let shown = doc.display_path(&self.vault.root);
match crate::terminal::copy_to_clipboard(&shown) {
Ok(()) => self.status.set(format!("Copied {shown}"), Severity::Info),
Err(e) => self
.status
.set(format!("Cannot copy: {e}"), Severity::Error),
}
}
fn hand_to_external_editor(&mut self) {
let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
return;
};
if self.tab().dirty {
self.status.set(
"Save or discard this buffer before handing it to $EDITOR",
Severity::Warning,
);
return;
}
if self.external_command().is_none() {
self.status.set(
"No editor: set $EDITOR or editor.external_command",
Severity::Error,
);
return;
}
self.external_edit = Some(path);
}
pub fn external_command(&self) -> Option<String> {
let configured = self.editor_config.external_command.trim();
if !configured.is_empty() {
return Some(configured.to_string());
}
std::env::var("EDITOR")
.ok()
.or_else(|| std::env::var("VISUAL").ok())
.filter(|command| !command.trim().is_empty())
}
fn create_file(&mut self, typed: &str) {
let relative_to = self
.active_path()
.and_then(|path| path.parent().map(Path::to_path_buf))
.map_or_else(|| self.vault.root.clone(), |dir| self.vault.absolute(&dir));
let resolved =
crate::editor::resolve_new_path(typed, &relative_to, &self.vault.root, &self.files);
let path = match resolved {
Ok(path) => path,
Err(e) => {
self.status.set(e.to_string(), Severity::Error);
return;
}
};
self.create_and_open(&path);
}
pub fn create_and_open(&mut self, path: &Path) {
let contents = if self.editor_config.new_file_frontmatter {
crate::editor::frontmatter_stub(path)
} else {
String::new()
};
if let Err(e) = crate::editor::create(path, &contents) {
self.status
.set(format!("Cannot create the file: {e}"), Severity::Error);
return;
}
match Document::load(path) {
Ok(document) => {
let wants_tab = self.tab().doc.is_some();
if wants_tab && self.open_in_background_tab(document.clone()) {
self.active_tab = self.tabs.len() - 1;
} else {
self.open(document);
}
if let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) {
self.vault.tree.insert_all([crate::vault::walker::Entry {
path: relative.clone(),
is_dir: false,
mtime: None,
size: 0,
}]);
self.vault.markdown.push((relative.clone(), None, 0));
self.reindex(&relative);
self.vault.tree.reveal(&relative);
}
self.update(Action::EnterEditMode);
self.status
.set(format!("Created {}", path.display()), Severity::Info);
}
Err(e) => self.status.set(
format!("Created but cannot open {}: {e}", path.display()),
Severity::Error,
),
}
}
fn rename(&mut self, existing: &Path, typed: &str) {
let resolved = crate::editor::resolve_rename(typed, existing, &self.vault.root);
let target = match resolved {
Ok(path) => path,
Err(e) => {
self.status.set(e.to_string(), Severity::Error);
return;
}
};
if target == existing {
return;
}
if let Err(e) = std::fs::rename(existing, &target) {
self.status
.set(format!("Cannot rename: {e}"), Severity::Error);
return;
}
let mut followed = 0;
for tab in &mut self.tabs {
if let Some(doc) = &mut tab.doc {
if doc.path == existing {
doc.path = target.clone();
followed += 1;
}
}
}
self.rebuild_after_rename(existing, &target);
let broken = self.count_inbound(existing);
let name = target.file_name().unwrap_or_default().to_string_lossy();
if broken > 0 {
self.status.set(
format!("Renamed to {name}; {broken} documents now link to the old name"),
Severity::Warning,
);
} else {
self.status.set(
format!("Renamed to {name} ({followed} tabs)"),
Severity::Info,
);
}
}
fn rebuild_after_rename(&mut self, from: &Path, to: &Path) {
let (Some(old), Some(new)) = (
self.vault.relative(from).map(Path::to_path_buf),
self.vault.relative(to).map(Path::to_path_buf),
) else {
return;
};
self.vault.index.remove(&old);
self.vault.markdown.retain(|(path, _, _)| path != &old);
self.vault.tree.insert_all([crate::vault::walker::Entry {
path: new.clone(),
is_dir: false,
mtime: None,
size: 0,
}]);
self.vault.markdown.push((new.clone(), None, 0));
self.vault.tree.remove(&old);
self.reindex(&new);
self.vault.tree.reveal(&new);
}
fn count_inbound(&self, path: &Path) -> usize {
let Some(relative) = self.vault.relative(path) else {
return 0;
};
self.vault.index.backlinks(relative, &self.wikilinks).len()
}
fn quit(&mut self) {
if !self.any_dirty() {
self.should_quit = true;
return;
}
let dirty = self.tabs.iter().filter(|tab| tab.dirty).count();
self.overlay = Some(Overlay::Confirm {
question: format!("{dirty} documents have unsaved edits. Quit anyway?"),
choices: vec![('d', "discard and quit".into())],
action: ConfirmAction::Quit,
});
self.focus = Focus::Overlay;
}
fn confirm(&mut self, key: char) {
let Some(Overlay::Confirm { action, .. }) = self.overlay.clone() else {
return;
};
match (&action, key) {
(ConfirmAction::LeaveEditMode, 's') => {
self.close_overlay();
self.save(false);
if self.overlay.is_none() && !self.tab().dirty {
self.finish_edit_mode();
}
}
(ConfirmAction::LeaveEditMode, 'd') => {
self.close_overlay();
self.finish_edit_mode();
}
(ConfirmAction::Quit, 'd') => {
self.close_overlay();
self.should_quit = true;
}
(ConfirmAction::OverwriteChanged, 'o') => {
self.close_overlay();
self.save(true);
}
(ConfirmAction::OverwriteChanged, 'r') => {
self.close_overlay();
self.tabs[self.active_tab].dirty = false;
self.finish_edit_mode();
self.reload_document();
}
(ConfirmAction::CreatePage { path }, 'y') => {
let path = path.clone();
self.close_overlay();
self.create_and_open(&path);
}
(ConfirmAction::RestoreRecovery { path }, 'y') => {
let path = path.clone();
self.close_overlay();
self.restore_recovery(&path);
}
(ConfirmAction::RestoreRecovery { path }, 'n') => {
let path = path.clone();
self.close_overlay();
buffer::clear_recovery(&self.vault.root, &path);
}
_ => self.close_overlay(),
}
}
fn restore_recovery(&mut self, path: &Path) {
let Some(text) = buffer::read_recovery(&self.vault.root, path) else {
return;
};
let index = self.active_tab;
if let Some(editor) = &mut self.tabs[index].editor {
editor.textarea.select_all();
editor.textarea.insert_str(text);
self.tabs[index].dirty = true;
}
buffer::clear_recovery(&self.vault.root, path);
self.status.set("Restored unsaved text", Severity::Info);
}
pub fn write_recovery_files(&self) {
for tab in &self.tabs {
let (Some(doc), Some(editor)) = (&tab.doc, &tab.editor) else {
continue;
};
if !editor.is_dirty() {
continue;
}
if let Err(e) = buffer::write_recovery(&self.vault.root, &doc.path, &editor.text()) {
tracing::warn!("cannot write a recovery file: {e}");
}
}
}
pub fn any_dirty(&self) -> bool {
self.tabs.iter().any(|tab| tab.dirty)
}
fn open_prompt(&mut self, kind: PromptKind) {
let prefill = match kind {
PromptKind::ProjectSearch => self.search.query.clone(),
PromptKind::NewFile => self.new_file_prefix(),
PromptKind::RenameDocument => self
.tab()
.doc
.as_ref()
.and_then(|doc| doc.path.file_name())
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default(),
PromptKind::RenameTreeEntry => self
.vault
.tree
.selected()
.map(|node| node.name.clone())
.unwrap_or_default(),
};
self.overlay = Some(Overlay::Prompt {
kind,
input: TextInput::with_value(prefill),
});
self.focus = Focus::Overlay;
}
fn edit_prompt(&mut self, edit: TextEdit) {
if let Some(Overlay::Prompt { input, .. }) = &mut self.overlay {
input.apply(edit);
}
}
fn accept_prompt(&mut self) {
let Some(Overlay::Prompt { kind, input }) = self.overlay.clone() else {
return;
};
self.close_overlay();
match kind {
PromptKind::ProjectSearch => self.start_search(input.value()),
PromptKind::NewFile => self.create_file(input.value()),
PromptKind::RenameDocument => {
let path = self.tab().doc.as_ref().map(|doc| doc.path.clone());
if let Some(path) = path {
self.rename(&path, input.value());
}
}
PromptKind::RenameTreeEntry => {
let path = self
.vault
.tree
.selected()
.map(|node| self.vault.absolute(&node.path));
if let Some(path) = path {
self.rename(&path, input.value());
}
}
}
}
fn new_file_prefix(&self) -> String {
let from_tree = (self.focus == Focus::Sidebar)
.then(|| self.vault.tree.selected())
.flatten()
.map(|node| {
if node.is_dir {
node.path.clone()
} else {
node.path.parent().unwrap_or(Path::new("")).to_path_buf()
}
});
let directory = from_tree.or_else(|| {
self.active_path()
.and_then(|path| path.parent().map(Path::to_path_buf))
});
match directory {
Some(dir) if !dir.as_os_str().is_empty() => format!("{}/", dir.display()),
_ => String::new(),
}
}
pub fn start_search(&mut self, query: &str) {
self.search_handle = None;
if query.trim().is_empty() {
self.search = SearchState::default();
return;
}
self.search.begin(query.to_string());
self.sidebar.mode = SidebarMode::Search;
self.sidebar.visible = true;
let Some(sink) = self.search_sink.clone() else {
return;
};
let walk = WalkOptions {
respect_gitignore: self.files.respect_gitignore,
follow_symlinks: self.general.follow_symlinks,
};
self.search_handle = Some(content::spawn(
self.vault.root.clone(),
content::Query::parse(query, &self.search_config),
self.search_config.clone(),
self.files.clone(),
walk,
move |event| sink(event),
));
}
pub fn search_now(&mut self, query: &str) {
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;
self.search.begin(query.to_string());
self.sidebar.mode = SidebarMode::Search;
let walk = WalkOptions {
respect_gitignore: self.files.respect_gitignore,
follow_symlinks: self.general.follow_symlinks,
};
let collected = Mutex::new(Vec::new());
let outcome = Mutex::new((false, None));
content::run(
&self.vault.root,
&content::Query::parse(query, &self.search_config),
&self.search_config,
&self.files,
walk,
&AtomicBool::new(false),
&|event| match event {
SearchEvent::Hits(hits) => collected.lock().unwrap().extend(hits),
SearchEvent::Finished { truncated, .. } => {
outcome.lock().unwrap().0 = truncated;
}
SearchEvent::BadPattern(e) => outcome.lock().unwrap().1 = Some(e),
},
);
self.search.hits = collected.into_inner().unwrap();
let (truncated, error) = outcome.into_inner().unwrap();
self.search.truncated = truncated;
self.search.error = error;
self.search.running = false;
self.search.elapsed = self.search.started.map(|at| at.elapsed());
}
fn open_selected_hit(&mut self) {
let Some(hit) = self.search.hits.get(self.search.selected).cloned() else {
return;
};
let absolute = self.vault.absolute(&hit.path);
self.navigate_to(&absolute, None);
if let Some(offset) = self.offset_of_line(hit.line) {
self.scroll_to_offset(offset);
}
}
fn offset_of_line(&self, line: u64) -> Option<usize> {
let source = &self.tab().doc.as_ref()?.source;
let wanted = line.saturating_sub(1) as usize;
if wanted == 0 {
return Some(0);
}
source
.match_indices('\n')
.nth(wanted - 1)
.map(|(at, _)| at + 1)
}
fn open_switcher(&mut self) {
let rows = self.switcher_rows("");
self.overlay = Some(Overlay::Switcher {
input: TextInput::new(),
rows,
selected: 0,
});
self.focus = Focus::Overlay;
}
fn edit_switcher(&mut self, edit: TextEdit) {
let Some(Overlay::Switcher { input, .. }) = &mut self.overlay else {
return;
};
input.apply(edit);
let query = input.value().to_string();
let rows = self.switcher_rows(&query);
if let Some(Overlay::Switcher {
rows: slot,
selected,
..
}) = &mut self.overlay
{
*slot = rows;
*selected = 0;
}
}
fn switcher_rows(&mut self, query: &str) -> Vec<SwitcherRow> {
if query.trim().is_empty() {
return self
.recent
.iter()
.map(|path| SwitcherRow {
path: path.clone(),
indices: Vec::new(),
create: false,
})
.collect();
}
let paths: Vec<PathBuf> = self
.vault
.markdown
.iter()
.map(|(path, _, _)| path.clone())
.collect();
let mut rows: Vec<SwitcherRow> = self
.fuzzy
.search(&paths, query, SWITCHER_ROWS)
.into_iter()
.map(|candidate| SwitcherRow {
path: candidate.path,
indices: candidate.indices,
create: false,
})
.collect();
if rows.is_empty() {
rows.push(SwitcherRow {
path: PathBuf::from(with_markdown_extension(query, &self.files)),
indices: Vec::new(),
create: true,
});
}
rows
}
fn move_switcher(&mut self, delta: isize) {
let Some(Overlay::Switcher { rows, selected, .. }) = &mut self.overlay else {
return;
};
if rows.is_empty() {
*selected = 0;
return;
}
let last = rows.len() as isize - 1;
*selected = (*selected as isize + delta).clamp(0, last) as usize;
}
fn accept_switcher(&mut self, new_tab: bool) {
let Some(Overlay::Switcher { rows, selected, .. }) = self.overlay.clone() else {
return;
};
let Some(row) = rows.get(selected).cloned() else {
return;
};
self.close_overlay();
if row.create {
self.status.set(
format!("`{}` does not exist yet", row.path.display()),
Severity::Warning,
);
return;
}
let absolute = self.vault.absolute(&row.path);
if new_tab {
match Document::load(&absolute) {
Ok(document) => {
self.open_in_background_tab(document);
}
Err(e) => self.status.set(
format!("Cannot read {}: {e}", row.path.display()),
Severity::Error,
),
}
return;
}
self.navigate_to(&absolute, None);
}
fn remember(&mut self, path: &Path) {
let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) else {
return;
};
self.recent.retain(|seen| seen != &relative);
self.recent.insert(0, relative);
self.recent.truncate(MAX_RECENT);
}
pub fn restore_session(&mut self) {
if !self.session_config.restore {
return;
}
let Some(session) = crate::config::session::Session::load(&self.vault.root) else {
return;
};
self.sidebar.visible = session.sidebar_visible;
self.sidebar.width = session.sidebar_width;
self.sidebar.mode = session.sidebar_mode;
self.recent = session.recent;
self.recent.truncate(self.session_config.max_recent);
if let Some(name) = session.theme.filter(|_| !self.theme_pinned) {
if name != self.theme.name {
let dir = self.theme_dir();
let mut warnings = Vec::new();
let theme = Theme::resolve(&name, dir.as_deref(), &mut warnings);
if warnings.is_empty() {
self.theme_config.name = name;
self.apply_theme(theme);
}
}
}
let mut restored = Vec::new();
for saved in &session.tabs {
let absolute = self.vault.absolute(&saved.path);
let Ok(document) = Document::load(&absolute) else {
continue;
};
let mut tab = Tab::with_document(document);
tab.scroll = saved.scroll;
restored.push(tab);
}
if restored.is_empty() {
return;
}
self.active_tab = session.active_tab.min(restored.len() - 1);
self.tabs = restored;
}
pub fn save_session(&self) {
if !self.session_config.restore {
return;
}
let session = crate::config::session::Session {
version: crate::config::session::VERSION,
tabs: self
.tabs
.iter()
.filter_map(|tab| {
let doc = tab.doc.as_ref()?;
Some(crate::config::session::SavedTab {
path: self.vault.relative(&doc.path)?.to_path_buf(),
scroll: tab.scroll,
})
})
.collect(),
active_tab: self.active_tab,
sidebar_visible: self.sidebar.visible,
sidebar_width: self.sidebar.width,
sidebar_mode: self.sidebar.mode,
theme: Some(self.theme.name.clone()),
recent: self.recent.clone(),
};
if let Err(e) = session.save(&self.vault.root) {
tracing::warn!("cannot write the session: {e}");
}
}
pub fn start_watch(&mut self, sink: impl Fn(WatchEvent) + Send + Sync + 'static) {
if !self.watch_config.enabled {
return;
}
self.watch = Some(crate::vault::watch::spawn(
self.vault.root.clone(),
std::time::Duration::from_millis(self.watch_config.debounce_ms),
sink,
));
}
pub fn theme_dir(&self) -> Option<PathBuf> {
if self.theme_config.dir.as_os_str().is_empty() {
crate::config::user_theme_dir()
} else {
Some(self.theme_config.dir.clone())
}
}
pub fn start_theme_watch(&mut self, sink: impl Fn(WatchEvent) + Send + Sync + 'static) {
let Some(dir) = self.theme_dir().filter(|dir| dir.is_dir()) else {
return;
};
self.theme_watch = Some(crate::vault::watch::spawn(
dir,
std::time::Duration::from_millis(self.watch_config.debounce_ms),
sink,
));
}
pub fn available_themes(&self) -> Vec<String> {
let mut names: Vec<String> = crate::theme::builtin::ALL
.iter()
.map(|(name, _)| (*name).to_string())
.collect();
let Some(dir) = self.theme_dir() else {
return names;
};
let Ok(entries) = std::fs::read_dir(dir) else {
return names;
};
let mut mine: Vec<String> = entries
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().is_some_and(|e| e == "toml"))
.filter_map(|path| path.file_stem()?.to_str().map(str::to_string))
.filter(|stem| !names.contains(stem))
.collect();
mine.sort();
names.extend(mine);
names
}
fn cycle_theme(&mut self) {
let names = self.available_themes();
if names.is_empty() {
return;
}
let next = names
.iter()
.position(|name| *name == self.theme.name)
.map_or(0, |at| (at + 1) % names.len());
let wanted = names[next].clone();
let dir = self.theme_dir();
let mut warnings = Vec::new();
let mut theme = Theme::resolve(&wanted, dir.as_deref(), &mut warnings);
if let Some(warning) = warnings.first() {
self.status.set(warning.clone(), Severity::Warning);
return;
}
if theme.code_theme.is_none() {
theme.code_theme = Some(self.theme_config.code_theme.clone());
}
self.theme_config.name = wanted;
self.apply_theme(theme);
self.status
.set(format!("Theme: {}", self.theme.name), Severity::Info);
}
fn apply_theme(&mut self, theme: Theme) {
self.theme = theme;
for tab in &mut self.tabs {
tab.layout = RenderedDocument::new();
}
}
pub fn reload_theme(&mut self) {
let dir = self.theme_dir();
let mut warnings = Vec::new();
let mut theme = Theme::resolve(&self.theme_config.name, dir.as_deref(), &mut warnings);
if theme.code_theme.is_none() {
theme.code_theme = Some(self.theme_config.code_theme.clone());
}
if let Some(warning) = warnings.first() {
self.status.set(warning.clone(), Severity::Warning);
return;
}
self.apply_theme(theme);
self.status.set(
format!("Theme reloaded: {}", self.theme.name),
Severity::Info,
);
}
pub fn files_changed(&mut self, paths: &[PathBuf]) {
for relative in paths {
let absolute = self.vault.absolute(relative);
let mtime = std::fs::metadata(&absolute).and_then(|m| m.modified()).ok();
if self.own_writes.claim(&absolute, mtime) {
continue;
}
if !self.files.is_markdown(&absolute) {
continue;
}
let size = std::fs::metadata(&absolute).map(|m| m.len()).unwrap_or(0);
self.vault.tree.insert_all([crate::vault::walker::Entry {
path: relative.clone(),
is_dir: false,
mtime,
size,
}]);
self.reindex(relative);
if self.active_path().as_deref() == Some(relative.as_path()) {
self.active_document_changed();
}
}
}
pub fn files_removed(&mut self, paths: &[PathBuf]) {
for relative in paths {
self.vault.tree.remove(relative);
self.vault.index.remove(relative);
self.vault.markdown.retain(|(path, _, _)| path != relative);
}
}
fn active_document_changed(&mut self) {
if self.tab().dirty {
self.status
.set("File changed on disk (r to reload)", Severity::Warning);
return;
}
let scroll = self.tab().scroll;
let focused = self.tab().focused_link;
let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
return;
};
match Document::load(&path) {
Ok(document) => {
self.open(document);
self.restore_scroll(scroll);
let count = self.tab().doc.as_ref().map_or(0, |doc| doc.links.len());
self.tabs[self.active_tab].focused_link = focused.filter(|at| *at < count);
}
Err(e) => self.status.set(
format!("Cannot reload {}: {e}", path.display()),
Severity::Warning,
),
}
}
fn move_sidebar_selection(&mut self, delta: isize) {
match self.sidebar.mode {
SidebarMode::Files => self.vault.tree.move_selection(delta),
SidebarMode::Outline => {
let count = self.tab().doc.as_ref().map_or(0, |doc| doc.outline.len());
if count == 0 {
return;
}
let at = self.sidebar.outline_selected.min(count - 1) as isize;
self.sidebar.outline_selected = (at + delta).clamp(0, count as isize - 1) as usize;
}
SidebarMode::Search => self.search.move_selection(delta),
SidebarMode::Links => {}
}
}
fn activate_sidebar_selection(&mut self) {
match self.sidebar.mode {
SidebarMode::Files => self.tree_expand_or_open(),
SidebarMode::Outline => self.jump_to_selected_heading(),
SidebarMode::Search => self.open_selected_hit(),
SidebarMode::Links => {}
}
}
fn sidebar_back(&mut self) {
if self.sidebar.mode == SidebarMode::Files {
self.vault.tree.collapse_selected();
}
}
pub fn current_heading(&self) -> usize {
let tab = self.tab();
let Some(doc) = tab.doc.as_ref() else {
return 0;
};
let map = tab.layout.line_map();
let mut current = 0;
for (index, heading) in doc.outline.iter().enumerate() {
match map.line_of_offset(heading.offset) {
Some(line) if line <= tab.scroll => current = index,
_ => break,
}
}
current
}
fn jump_to_selected_heading(&mut self) {
let at = self.sidebar.outline_selected;
let slug = self
.tab()
.doc
.as_ref()
.and_then(|doc| doc.outline.get(at))
.map(|heading| heading.slug.clone());
if let Some(slug) = slug {
self.jump_to_anchor(&slug);
}
}
fn open_find(&mut self) {
if self.tab().doc.is_none() {
return;
}
let index = self.active_tab;
let find = self.tabs[index].find.get_or_insert_with(FindState::new);
find.input.apply(TextEdit::End);
self.overlay = Some(Overlay::Find);
self.focus = Focus::Overlay;
}
fn edit_find(&mut self, edit: TextEdit) {
let index = self.active_tab;
let Some(source) = self.tabs[index].doc.as_ref().map(|d| d.source.clone()) else {
return;
};
let Some(find) = &mut self.tabs[index].find else {
return;
};
find.input.apply(edit);
find.refresh(&source);
if let Some(offset) = find.current_offset() {
self.scroll_to_offset(offset);
}
}
fn step_find(&mut self, forward: bool) {
let index = self.active_tab;
let Some(find) = &mut self.tabs[index].find else {
return;
};
let Some(offset) = find.step(forward) else {
self.status.set("No matches", Severity::Info);
return;
};
self.scroll_to_offset(offset);
}
fn close_find(&mut self) {
self.tabs[self.active_tab].find = None;
if self.overlay == Some(Overlay::Find) {
self.close_overlay();
}
}
fn scroll_to_offset(&mut self, offset: usize) {
let height = usize::from(self.page());
let renderer = self.renderer(self.viewport_inner().width);
let tab = &mut self.tabs[self.active_tab];
let Some(doc) = &tab.doc else { return };
let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) else {
return;
};
if line < tab.scroll {
tab.scroll = line;
} else if line >= tab.scroll + height {
tab.scroll = line.saturating_sub(height / 2);
}
}
fn new_tab(&mut self) {
self.leave_active_tab();
if self.tabs.len() >= MAX_TABS {
self.status.set(
format!("{MAX_TABS} tabs is the maximum; reusing this one"),
Severity::Warning,
);
self.tabs[self.active_tab] = Tab::default();
return;
}
self.tabs.push(Tab::default());
self.active_tab = self.tabs.len() - 1;
self.focus = Focus::Viewport;
}
fn close_tab(&mut self) {
if self.tabs.len() == 1 {
self.update(Action::Quit);
return;
}
self.leave_active_tab();
self.tabs.remove(self.active_tab);
self.active_tab = self.active_tab.saturating_sub(1);
self.reveal_active_document();
}
fn switch_tab(&mut self, delta: isize) {
let count = self.tabs.len() as isize;
if count <= 1 {
return;
}
self.leave_active_tab();
self.active_tab = (self.active_tab as isize + delta).rem_euclid(count) as usize;
self.reveal_active_document();
}
fn leave_active_tab(&mut self) {
if self.overlay == Some(Overlay::Find) {
self.close_overlay();
}
}
fn open_in_background_tab(&mut self, document: Document) -> bool {
if self.tabs.len() >= MAX_TABS {
self.status
.set(format!("{MAX_TABS} tabs is the maximum"), Severity::Warning);
return false;
}
let label = document.label().to_string();
let mut tab = Tab::with_document(document);
tab.scroll = 0;
self.tabs.push(tab);
self.status
.set(format!("Opened {label} in a new tab"), Severity::Info);
true
}
fn follow_focused_link_in_new_tab(&mut self) {
let Some(index) = self.tab().focused_link else {
self.status
.set("No link focused; press `n` or `f`", Severity::Info);
return;
};
let Some(doc) = self.tab().doc.as_ref() else {
return;
};
let Some(link) = doc.links.get(index) else {
return;
};
let target = link.target.clone();
if link.kind == crate::doc::links::LinkKind::Wiki {
self.follow_wiki_link(&target);
return;
}
let resolved = links::resolve(&target, doc.dir(), &self.vault.root, &self.files);
match resolved {
links::Resolved::Document { path, .. } => match Document::load(&path) {
Ok(document) => {
self.open_in_background_tab(document);
}
Err(e) => self.status.set(
format!("Cannot read {}: {e}", path.display()),
Severity::Error,
),
},
_ => self.follow_link(index),
}
}
fn follow_wiki_link(&mut self, target: &str) {
if !self.wikilinks.enabled {
self.status
.set("Wiki-links are disabled", Severity::Warning);
return;
}
if !self.vault.index.ready {
self.status.set("Still indexing…", Severity::Info);
return;
}
let source = self.active_path().unwrap_or_default();
match self
.vault
.index
.resolve_wiki(target, &source, &self.wikilinks)
{
WikiResolution::Found { path, anchor } => {
let absolute = self.vault.absolute(&path);
self.navigate_to(&absolute, anchor);
}
WikiResolution::Ambiguous { candidates, anchor } => {
self.overlay = Some(Overlay::Disambiguate {
page: target.to_string(),
candidates,
anchor,
selected: 0,
});
self.focus = Focus::Overlay;
}
WikiResolution::Missing { page, .. } => self.offer_to_create(&page),
}
}
fn offer_to_create(&mut self, page: &str) {
let configured = &self.wikilinks.new_file_dir;
let directory = if configured.as_os_str().is_empty() {
self.active_path()
.and_then(|path| path.parent().map(Path::to_path_buf))
.unwrap_or_default()
} else {
configured.clone()
};
let resolved = crate::editor::resolve_new_path(
page,
&self.vault.absolute(&directory),
&self.vault.root,
&self.files,
);
let path = match resolved {
Ok(path) => path,
Err(e) => {
self.status.set(e.to_string(), Severity::Warning);
return;
}
};
let shown = self
.vault
.relative(&path)
.unwrap_or(&path)
.display()
.to_string();
self.overlay = Some(Overlay::Confirm {
question: format!("Create \"{shown}\"?"),
choices: vec![('y', "create".into())],
action: ConfirmAction::CreatePage { path },
});
self.focus = Focus::Overlay;
}
pub fn choose_candidate(&mut self, index: usize) {
let Some(Overlay::Disambiguate {
candidates, anchor, ..
}) = self.overlay.clone()
else {
return;
};
let Some(path) = candidates.get(index) else {
return;
};
let absolute = self.vault.absolute(path);
self.close_overlay();
self.navigate_to(&absolute, anchor);
}
fn start_indexing(&mut self) {
if !self.wikilinks.enabled || !self.wikilinks.index_on_start {
self.vault.index.ready = true;
return;
}
if self.wikilinks.cache {
if let Some(cache) = index::load_cache(&self.vault.root) {
self.vault.index = crate::vault::index::Index::from_cache(cache);
}
}
let live: std::collections::BTreeSet<PathBuf> = self
.vault
.markdown
.iter()
.map(|(path, _, _)| path.clone())
.collect();
self.vault.index.retain_paths(&live);
let stale: Vec<PathBuf> = self
.vault
.markdown
.iter()
.filter(|(path, mtime, size)| !self.vault.index.is_current(path, *mtime, *size))
.map(|(path, _, _)| path.clone())
.collect();
self.vault.index.total = Some(self.vault.markdown.len());
self.vault.index.indexed = self.vault.markdown.len() - stale.len();
if stale.is_empty() {
self.vault.index.ready = true;
return;
}
let Some(sink) = self.index_sink.clone() else {
return;
};
self.vault.start_index(stale, move |event| sink(event));
}
fn finish_indexing(&mut self) {
self.vault.index.ready = true;
self.vault.cancel_index();
if !self.wikilinks.cache {
return;
}
if let Err(e) = index::save_cache(&self.vault.root, &self.vault.index.to_cache()) {
tracing::warn!("cannot write the index cache: {e}");
}
}
pub fn index_now(&mut self) {
let files: Vec<PathBuf> = self
.vault
.markdown
.iter()
.map(|(path, _, _)| path.clone())
.collect();
for path in files {
self.reindex(&path);
}
self.vault.index.total = Some(self.vault.markdown.len());
self.vault.index.ready = true;
}
pub fn reindex(&mut self, relative: &Path) {
let absolute = self.vault.absolute(relative);
let Ok(bytes) = std::fs::read(&absolute) else {
self.vault.index.remove(relative);
return;
};
let metadata = std::fs::metadata(&absolute).ok();
let entry = index::entry_for(
&String::from_utf8_lossy(&bytes),
metadata.as_ref().and_then(|m| m.modified().ok()),
metadata.as_ref().map_or(0, |m| m.len()),
);
self.vault.index.insert(relative.to_path_buf(), entry);
}
pub fn is_broken(&self, link: &crate::doc::links::Link) -> bool {
let Some(doc) = self.tab().doc.as_ref() else {
return false;
};
if link.kind == crate::doc::links::LinkKind::Wiki {
if !self.wikilinks.enabled || !self.vault.index.ready {
return false;
}
let source = self.active_path().unwrap_or_default();
return matches!(
self.vault
.index
.resolve_wiki(&link.target, &source, &self.wikilinks),
WikiResolution::Missing { .. }
);
}
matches!(
links::resolve(&link.target, doc.dir(), &self.vault.root, &self.files),
links::Resolved::Broken { .. }
)
}
pub fn backlinks(&self) -> Vec<crate::vault::index::Backlink> {
let Some(path) = self.active_path() else {
return Vec::new();
};
self.vault.index.backlinks(&path, &self.wikilinks)
}
fn cycle_focus(&mut self, _forward: bool) {
if self.overlay.is_some() || self.tab().mode == TabMode::Edit {
return;
}
self.focus = match self.focus {
Focus::Viewport if self.sidebar.visible => Focus::Sidebar,
Focus::Sidebar => Focus::Viewport,
other => other,
};
}
fn toggle_sidebar(&mut self) {
self.sidebar.visible = !self.sidebar.visible;
if self.sidebar.visible {
if self.frames().sidebar_placement == SidebarPlacement::Overlaid {
self.focus = Focus::Sidebar;
}
} else if self.focus == Focus::Sidebar {
self.focus = Focus::Viewport;
}
}
fn resize_sidebar(&mut self, delta: i32) {
let requested =
(i32::from(self.sidebar.width) + delta).clamp(0, i32::from(u16::MAX)) as u16;
self.sidebar.width = if self.area.width > 0 {
layout::clamp_sidebar_width(requested, self.area.width)
} else {
requested
};
}
fn toggle_mouse(&mut self) {
self.mouse_capture = !self.mouse_capture;
match terminal::set_mouse_capture(self.mouse_capture) {
Ok(()) => {
let state = if self.mouse_capture { "on" } else { "off" };
self.status
.set(format!("Mouse capture {state}"), Severity::Info);
}
Err(e) => {
self.mouse_capture = !self.mouse_capture;
self.status
.set(format!("Cannot change mouse capture: {e}"), Severity::Error);
}
}
}
fn toggle_help(&mut self) {
match self.overlay {
Some(Overlay::Help { .. }) => self.close_overlay(),
_ => {
self.overlay = Some(Overlay::Help { scroll: 0 });
self.focus = Focus::Overlay;
}
}
}
fn escape(&mut self) {
if self.overlay.is_some() {
self.close_overlay();
return;
}
if self.sidebar.filter.is_some() {
self.update(Action::TreeFilterCancel);
return;
}
if self.tab().mode == TabMode::Edit {
self.leave_edit_mode();
return;
}
if !self.keymap.pending().is_empty() {
self.keymap.clear_pending();
self.status.pending = None;
}
}
fn close_overlay(&mut self) {
if self.overlay == Some(Overlay::Find) {
if let Some(find) = &mut self.tabs[self.active_tab].find {
find.current = None;
}
}
self.overlay = None;
self.focus = if self.sidebar.visible
&& self.frames().sidebar_placement == SidebarPlacement::Overlaid
{
Focus::Sidebar
} else {
Focus::Viewport
};
}
}
#[derive(Debug)]
pub enum Message {
Input(crossterm::event::Event),
Signal(i32),
SyntaxReady,
Walk(WalkEvent),
Index(IndexEvent),
Search(SearchEvent),
Watch(WatchEvent),
ThemeChanged,
}
pub fn run(terminal: &mut Tui, app: &mut App, messages: &Receiver<Message>) -> anyhow::Result<()> {
let size = terminal.size()?;
app.update(Action::Resize(size.width, size.height));
loop {
terminal.draw(|frame| ui::draw(app, frame))?;
let Ok(message) = messages.recv() else {
break;
};
for action in crate::event::translate(app, message) {
app.update(action);
}
if app.should_suspend {
app.should_suspend = false;
suspend(terminal)?;
}
if let Some(path) = app.external_edit.take() {
match hand_over(terminal, app, &path) {
Ok(()) => app.update(Action::ReloadDocument),
Err(e) => app
.status
.set(format!("$EDITOR failed: {e}"), Severity::Error),
}
}
if app.should_quit {
break;
}
}
Ok(())
}
fn hand_over(terminal: &mut Tui, app: &App, path: &Path) -> anyhow::Result<()> {
use std::process::Command;
let command = app
.external_command()
.ok_or_else(|| anyhow::anyhow!("no editor configured"))?;
let mut parts = command.split_whitespace();
let program = parts
.next()
.ok_or_else(|| anyhow::anyhow!("no editor configured"))?;
let mouse = terminal::mouse_capture_active();
terminal::restore()?;
let status = Command::new(program).args(parts).arg(path).status();
*terminal = terminal::setup(mouse)?;
terminal.clear()?;
let status = status?;
if !status.success() {
anyhow::bail!("the editor exited with {status}");
}
Ok(())
}
fn suspend(terminal: &mut Tui) -> anyhow::Result<()> {
let mouse = terminal::mouse_capture_active();
terminal::restore()?;
#[cfg(unix)]
signal_hook::low_level::raise(signal_hook::consts::SIGTSTP)?;
*terminal = terminal::setup(mouse)?;
terminal.clear()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn app() -> App {
let mut app = App::new(
Theme::dark(),
Keymap::defaults(),
UiConfig::default(),
FilesConfig::default(),
);
app.update(Action::Resize(120, 40));
app
}
#[test]
fn opens_in_read_mode_with_the_viewport_focused() {
let app = app();
assert_eq!(app.tab().mode, TabMode::Read);
assert_eq!(app.focus, Focus::Viewport);
assert!(app.overlay.is_none());
}
#[test]
fn focus_cycles_between_the_sidebar_and_the_viewport() {
let mut app = app();
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Sidebar);
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Viewport);
app.update(Action::FocusPrev);
assert_eq!(app.focus, Focus::Sidebar);
}
#[test]
fn hiding_the_sidebar_moves_focus_out_of_it() {
let mut app = app();
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Sidebar);
app.update(Action::ToggleSidebar);
assert!(!app.sidebar.visible);
assert_eq!(app.focus, Focus::Viewport);
}
#[test]
fn focus_does_not_move_into_a_hidden_sidebar() {
let mut app = app();
app.update(Action::ToggleSidebar);
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Viewport);
}
#[test]
fn an_overlaid_sidebar_takes_focus_when_it_opens() {
let mut app = app();
app.update(Action::Resize(70, 40));
app.update(Action::ToggleSidebar); app.update(Action::ToggleSidebar); assert_eq!(app.frames().sidebar_placement, SidebarPlacement::Overlaid);
assert_eq!(app.focus, Focus::Sidebar);
}
#[test]
fn a_split_sidebar_does_not_steal_focus_when_it_opens() {
let mut app = app();
app.update(Action::ToggleSidebar);
app.update(Action::ToggleSidebar);
assert_eq!(app.frames().sidebar_placement, SidebarPlacement::Split);
assert_eq!(app.focus, Focus::Viewport);
}
#[test]
fn resizing_the_sidebar_stays_within_bounds() {
let mut app = app();
for _ in 0..200 {
app.update(Action::SidebarWiden);
}
assert!(app.sidebar.width <= 60, "{}", app.sidebar.width);
for _ in 0..200 {
app.update(Action::SidebarNarrow);
}
assert_eq!(app.sidebar.width, layout::MIN_SIDEBAR_WIDTH);
}
#[test]
fn a_shrunken_terminal_narrows_the_drawn_sidebar_but_keeps_the_choice() {
let mut app = app();
for _ in 0..40 {
app.update(Action::SidebarWiden);
}
let chosen = app.sidebar.width;
assert_eq!(chosen, 60);
app.update(Action::Resize(60, 20));
assert_eq!(app.frames().sidebar.unwrap().width, 30);
assert_eq!(app.sidebar.width, chosen, "the chosen width was destroyed");
app.update(Action::Resize(120, 40));
assert_eq!(app.frames().sidebar.unwrap().width, chosen);
}
#[test]
fn help_opens_takes_focus_and_closes_again() {
let mut app = app();
app.update(Action::ToggleHelp);
assert_eq!(app.overlay, Some(Overlay::Help { scroll: 0 }));
assert_eq!(app.focus, Focus::Overlay);
app.update(Action::ToggleHelp);
assert!(app.overlay.is_none());
assert_eq!(app.focus, Focus::Viewport);
}
#[test]
fn escape_closes_an_overlay() {
let mut app = app();
app.update(Action::ToggleHelp);
app.update(Action::Escape);
assert!(app.overlay.is_none());
assert_eq!(app.focus, Focus::Viewport);
}
#[test]
fn focus_does_not_move_while_an_overlay_is_open() {
let mut app = app();
app.update(Action::ToggleHelp);
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Overlay);
}
#[test]
fn switching_sidebar_mode_reveals_a_hidden_sidebar() {
let mut app = app();
app.update(Action::ToggleSidebar);
assert!(!app.sidebar.visible);
app.update(Action::SetSidebarMode(SidebarMode::Outline));
assert!(app.sidebar.visible);
assert_eq!(app.sidebar.mode, SidebarMode::Outline);
}
#[test]
fn quit_sets_the_flag() {
let mut app = app();
app.update(Action::Quit);
assert!(app.should_quit);
assert_eq!(app.exit_code, 0);
}
}