diff --git a/TODO.md b/TODO.md index c0eea74..aab1167 100644 --- a/TODO.md +++ b/TODO.md @@ -3,3 +3,10 @@ - [ ] 修复长文本换行后的缩进 - [ ] 测定并在 CI 固定真实 MSRV - [ ] 支持目录与正文的键盘焦点切换 +- [ ] 文件浏览器 File Browser(`termdown `,yazi 式实时预览,draft PR) + - [ ] **commit(Enter)进入全屏阅读器后渲染有多个问题,待排查修复**(已知,先记着) + - [ ] 阅读器内按键唤回浏览器(提交后目前回不去) + - [ ] 子目录进入 / 递归、隐藏文件、.gitignore + - [ ] 构建缓存(path+mtime,重访秒开) + - [ ] File Browser 与 ToC(目录)面板的共存规则 + - [ ] 预览态可滚动 / 更轻的纯文本「安静模式」 diff --git a/src/main.rs b/src/main.rs index dedfa37..84dfa6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ fn main() { println!(); println!("Arguments:"); println!(" FILE Markdown file to render (use - or omit for stdin)"); + println!(" DIR Directory to browse for Markdown files (yazi-style file browser)"); println!(); println!("Options:"); println!(" -h, --help Show this help message"); @@ -39,7 +40,8 @@ fn main() { println!(" --no-bell Disable the edge-scroll terminal bell"); println!(" -w, --watch Watch the file and live-reload on save (TUI only)"); println!(); - println!("By default, passing FILE opens it in the interactive TUI."); + println!("By default, passing FILE opens it in the interactive TUI, and"); + println!("passing a directory opens the File Browser (j/k move, Enter opens, q quits)."); println!("Piped/redirected stdout and stdin input automatically use cat mode."); println!(); println!("Config: ~/.config/termdown/config.toml"); @@ -106,11 +108,27 @@ fn main() { found }; + let stdout_tty = io::stdout().is_tty(); + let path_is_dir = file_arg + .as_deref() + .map(|p| p != "-" && std::path::Path::new(p).is_dir()) + .unwrap_or(false); + + // `termdown ` on a TTY launches the File Browser: a yazi-style panel + // that scans the directory for Markdown files and live-previews them. + if !cat_flag && stdout_tty && path_is_dir { + let dir = file_arg.expect("path_is_dir implies a path"); + tui::run_browser(&dir, &config, theme); + return; + } + // TUI is the default when we have a real file path and stdout is a // terminal; --cat, piping/redirecting, or stdin input all fall through // to cat mode so scripts like `termdown foo.md | less` keep working. - let want_tui = - !cat_flag && matches!(file_arg.as_deref(), Some(p) if p != "-") && io::stdout().is_tty(); + let want_tui = !cat_flag + && stdout_tty + && !path_is_dir + && matches!(file_arg.as_deref(), Some(p) if p != "-"); // Watching only makes sense for the interactive TUI: cat mode renders once // and stdin has no path to re-read. Warn only when the user explicitly diff --git a/src/render.rs b/src/render.rs index be30af6..54d58c9 100644 --- a/src/render.rs +++ b/src/render.rs @@ -509,6 +509,14 @@ pub fn delete_placement(w: &mut W, id: u32) -> std::io::Result<()> { write!(w, "\x1b_Ga=d,d=i,i={id},p={id},q=2;\x1b\\") } +/// Delete `id`'s placements AND free its cached image data (`d=I`, capital). +/// Unlike `delete_placement` (`d=i`), the terminal reclaims the stored PNG. +/// Used to reap ephemeral File Browser previews the user has scrolled past so +/// a long browse session doesn't accumulate orphaned image data. +pub fn delete_image_data(w: &mut W, id: u32) -> std::io::Result<()> { + write!(w, "\x1b_Ga=d,d=I,i={id},q=2;\x1b\\") +} + /// Delete all placements and image data this client has created. Used at /// TUI exit to clean up the terminal. pub fn delete_all_for_client(w: &mut W) -> std::io::Result<()> { diff --git a/src/tui/browser.rs b/src/tui/browser.rs new file mode 100644 index 0000000..77bad50 --- /dev/null +++ b/src/tui/browser.rs @@ -0,0 +1,83 @@ +//! File Browser state (yazi-style): scans a directory for Markdown files and +//! tracks the cursor + debounce timing that drives the live preview. +//! +//! This is the **filesystem** browser — distinct from the Table of Contents +//! ("Contents") panel, which lists the *headings of one document*. + +use std::path::{Path, PathBuf}; +use std::time::Instant; + +pub struct FileBrowser { + /// Directory being browsed (as given on the CLI; may be relative). + pub dir: PathBuf, + /// Markdown files in `dir`, sorted. Single level, no recursion (HALF 1). + pub entries: Vec, + /// Index into `entries` of the highlighted row. + pub cursor: usize, + /// `Some(t)` = the cursor moved at `t` and we're waiting for it to settle + /// before (re)building the preview. `None` = settled/idle. + pub last_move: Option, + /// Path the current preview was built for; `None` until the first build or + /// after a commit (so re-entering the browser rebuilds). + pub preview_path: Option, +} + +impl FileBrowser { + /// Scan `dir` for `*.md` / `*.markdown` files (single level). Returns an + /// error only if the directory itself can't be read; an empty result is a + /// valid (caller-handled) outcome. + pub fn scan(dir: &Path) -> std::io::Result { + let mut entries: Vec = std::fs::read_dir(dir)? + .filter_map(|e| e.ok()) + .map(|e| e.path()) + .filter(|p| p.is_file() && is_markdown(p)) + .collect(); + entries.sort(); + Ok(FileBrowser { + dir: dir.to_path_buf(), + entries, + cursor: 0, + // None so the first loop iteration treats the selection as + // "settled" and builds the initial preview immediately. + last_move: None, + preview_path: None, + }) + } + + pub fn selected(&self) -> Option<&PathBuf> { + self.entries.get(self.cursor) + } + + /// Display name for a row (file name only). + pub fn name_at(&self, idx: usize) -> String { + self.entries + .get(idx) + .and_then(|p| p.file_name()) + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default() + } +} + +fn is_markdown(p: &Path) -> bool { + matches!( + p.extension() + .and_then(|e| e.to_str()) + .map(|e| e.to_ascii_lowercase()) + .as_deref(), + Some("md") | Some("markdown") + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_markdown_matches_extensions() { + assert!(is_markdown(Path::new("a.md"))); + assert!(is_markdown(Path::new("a.markdown"))); + assert!(is_markdown(Path::new("DIR/A.MD"))); + assert!(!is_markdown(Path::new("a.txt"))); + assert!(!is_markdown(Path::new("README"))); + } +} diff --git a/src/tui/kitty.rs b/src/tui/kitty.rs index e35cd31..7964631 100644 --- a/src/tui/kitty.rs +++ b/src/tui/kitty.rs @@ -73,6 +73,20 @@ impl ImageLifecycle { Ok(()) } + /// Free the cached image *data* for `ids` (not just their placements), so + /// the terminal reclaims memory, and drop them from our tracking. Used to + /// reap ephemeral File Browser previews the user scrolled past — without + /// this, a long browse session leaves orphaned PNGs cached in the terminal + /// until exit. + pub fn forget(&mut self, w: &mut W, ids: &[u32]) -> io::Result<()> { + for &id in ids { + render::delete_image_data(w, id)?; + self.transmitted.remove(&id); + self.placed.remove(&id); + } + Ok(()) + } + /// Delete every placement + cached image data this client created. /// Clears our tracking. Called on TUI exit. pub fn cleanup(&mut self, w: &mut W) -> io::Result<()> { @@ -198,4 +212,33 @@ mod tests { assert!(lc.placed.is_empty()); assert!(lc.transmitted.is_empty()); } + + #[test] + fn forget_frees_data_and_drops_tracking() { + let mut lc = ImageLifecycle::default(); + let mut buf = Vec::new(); + lc.register(&mut buf, 7, b"png").unwrap(); + let mut desired = HashMap::new(); + desired.insert( + 7, + Placement { + col: 0, + row: 0, + scale: None, + crop: None, + }, + ); + lc.sync(&mut buf, &desired).unwrap(); + buf.clear(); + + lc.forget(&mut buf, &[7]).unwrap(); + let s = String::from_utf8(buf).unwrap(); + // d=I (capital) frees the image data, not just the placement. + assert!( + s.contains("a=d,d=I,i=7"), + "expected data delete, got: {s:?}" + ); + assert!(!lc.transmitted.contains(&7)); + assert!(!lc.placed.contains_key(&7)); + } } diff --git a/src/tui/mod.rs b/src/tui/mod.rs index ac8087e..0d653bb 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -1,5 +1,6 @@ //! Interactive TUI mode. +mod browser; mod input; mod kitty; mod search; @@ -7,8 +8,13 @@ mod viewport; use std::collections::HashMap; use std::io::{self, Write}; +use std::path::PathBuf; +use std::sync::mpsc; +use std::thread; use std::time::{Duration, Instant}; +use browser::FileBrowser; + use crossterm::event::{self, DisableFocusChange, EnableFocusChange, Event}; use crossterm::terminal::{ disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, @@ -34,6 +40,79 @@ const KITTY_PREFIX_TIMEOUT: Duration = Duration::from_millis(50); const KITTY_PAYLOAD_TIMEOUT: Duration = Duration::from_millis(250); const FOCUS_IMAGE_REFRESH_DELAY: Duration = Duration::from_millis(250); +/// Width of the File Browser side panel (the filesystem file list). +const BROWSER_PANEL_WIDTH: u16 = 32; + +/// How long the cursor must sit still in the File Browser before we dispatch a +/// preview build. Fast scrolling never triggers a build; the preview is only +/// requested once the selection settles. See `feat/file-browser`. +const PREVIEW_DEBOUNCE: Duration = Duration::from_millis(120); + +/// A request to the preview worker: build the doc at `path`, tagged with the +/// `generation` that was current when it was dispatched. +struct PreviewRequest { + generation: u64, + path: PathBuf, +} + +/// A finished preview build coming back from the worker. `doc` is `None` if the +/// file couldn't be read. `generation` lets the main thread discard results +/// whose selection has since moved on. +struct PreviewResponse { + generation: u64, + path: PathBuf, + doc: Option, +} + +/// Off-thread Markdown builder for the File Browser. Heading-image +/// rasterization (the dominant per-doc cost) runs here so the event loop never +/// blocks on a settle. The worker coalesces queued requests — if several pile +/// up it only builds the most recent — and the main thread additionally drops +/// any result whose `generation` is stale. The worker exits when the request +/// channel closes (i.e. when `App`/`PreviewWorker` is dropped at TUI teardown). +struct PreviewWorker { + req_tx: mpsc::Sender, + res_rx: mpsc::Receiver, +} + +impl PreviewWorker { + fn spawn(config: Config, theme: Theme) -> Self { + let (req_tx, req_rx) = mpsc::channel::(); + let (res_tx, res_rx) = mpsc::channel::(); + thread::spawn(move || { + while let Ok(mut req) = req_rx.recv() { + // Coalesce: if the cursor raced ahead and queued newer + // requests, skip straight to the latest and drop the rest. + while let Ok(newer) = req_rx.try_recv() { + req = newer; + } + let doc = std::fs::read_to_string(&req.path).ok().map(|src| { + layout::build(&src, &config, theme, crate::supports_kitty_graphics()) + }); + if res_tx + .send(PreviewResponse { + generation: req.generation, + path: req.path, + doc, + }) + .is_err() + { + break; // main thread gone + } + } + }); + PreviewWorker { req_tx, res_rx } + } + + fn request(&self, generation: u64, path: PathBuf) { + let _ = self.req_tx.send(PreviewRequest { generation, path }); + } + + fn try_recv(&self) -> Option { + self.res_rx.try_recv().ok() + } +} + enum Mode { Normal, Search { @@ -137,6 +216,26 @@ struct App { /// that may leave stale terminal cells behind (scroll, toc toggle, doc /// switch, resize). needs_full_redraw: bool, + /// File Browser state, present when termdown was pointed at a directory. + /// `None` for the plain single-file reader. Survives a commit so the + /// browser can be re-opened later (HALF 2). + browser: Option, + /// True while the File Browser panel is showing and holds focus (Browse + /// mode). False = Read mode (the normal full-screen reader). + browsing: bool, + /// The ephemeral preview document for the browser's current selection. + /// Rebuilt on settle; never enters the `docs` history stack until the user + /// commits it with Enter. + preview: Option, + /// Background Markdown builder for previews. `None` for the single-file + /// reader. + preview_worker: Option, + /// Monotonic "latest browser interaction" id. Bumped on every cursor move + /// so an in-flight build whose generation no longer matches is discarded. + preview_gen: u64, + /// The generation we have already dispatched a build for, so we don't + /// re-request the same selection every loop iteration while it builds. + dispatched_gen: u64, /// Whether live-reload watching is active for this session. Drives the /// status-bar indicator; the watcher itself lives in `run_ui`/`event_loop`. watch: bool, @@ -165,6 +264,12 @@ impl App { config, theme, needs_full_redraw: true, + browser: None, + browsing: false, + preview: None, + preview_worker: None, + preview_gen: 0, + dispatched_gen: 0, watch: false, kitty_response: crate::is_iterm2().then_some(KittyResponseState::Idle), }; @@ -172,6 +277,43 @@ impl App { app } + /// Construct an App that opens directly into the File Browser (Browse + /// mode) with no committed document yet. The first event-loop iteration + /// builds the preview for the first file. + fn new_browser( + browser: FileBrowser, + body_height: u16, + width: u16, + config: crate::config::Config, + theme: crate::theme::Theme, + ) -> Self { + let worker = PreviewWorker::spawn(config.clone(), theme); + App { + docs: Vec::new(), + cursor: 0, + history: Vec::new(), + forward: Vec::new(), + mode: Mode::Normal, + images: kitty::ImageLifecycle::default(), + next_image_id: 1, + term_size: (width, body_height), + should_quit: false, + config, + theme, + watch: false, + kitty_response: crate::is_iterm2().then_some(KittyResponseState::Idle), + needs_full_redraw: true, + browser: Some(browser), + browsing: true, + preview: None, + preview_worker: Some(worker), + // Start at 1 with dispatched_gen 0 so the first selection is + // eligible to dispatch on the very first iteration. + preview_gen: 1, + dispatched_gen: 0, + } + } + fn active(&self) -> &DocEntry { &self.docs[self.cursor] } @@ -210,6 +352,19 @@ impl App { Ok(()) } + /// Transmit the current preview doc's images (Browse mode). No-op when no + /// preview is built yet. + fn register_preview_images(&mut self, w: &mut W) -> io::Result<()> { + let doc_images: Vec<(u32, Vec)> = match &self.preview { + Some(p) => p.doc.images.iter().map(|i| (i.id, i.png.clone())).collect(), + None => return Ok(()), + }; + for (id, png) in &doc_images { + self.images.register(w, *id, png)?; + } + Ok(()) + } + /// Open a link target. If it's a local `.md` file, pushes a new DocEntry /// onto the history stack and makes it active. Otherwise, spawns the /// platform URL handler. @@ -466,6 +621,140 @@ pub fn run(path: &str, config: &Config, theme: Theme, watch: bool, graphics_supp } } +/// Entry point for `termdown ` — open the File Browser on a directory. +pub fn run_browser(dir: &str, config: &Config, theme: Theme) { + let browser = match FileBrowser::scan(std::path::Path::new(dir)) { + Ok(b) => b, + Err(e) => { + eprintln!("termdown: error reading directory {dir}: {e}"); + std::process::exit(1); + } + }; + if browser.entries.is_empty() { + eprintln!("termdown: no Markdown files found in {dir}"); + std::process::exit(1); + } + if let Err(e) = run_browser_ui(browser, config.clone(), theme) { + eprintln!("termdown: tui error: {e}"); + std::process::exit(1); + } +} + +fn run_browser_ui(browser: FileBrowser, config: Config, theme: Theme) -> io::Result<()> { + enable_raw_mode()?; + let mut stdout = io::stdout(); + crossterm::execute!(stdout, EnterAlternateScreen, EnableFocusChange)?; + let backend = CrosstermBackend::new(stdout); + let mut terminal = Terminal::new(backend)?; + let size = terminal.size()?; + let body_height = size.height.saturating_sub(1); + let mut app = App::new_browser(browser, body_height, size.width, config, theme); + + let result = event_loop(&mut terminal, &mut app, None); + + { + let mut out = io::stdout().lock(); + let _ = app.images.cleanup(&mut out); + let _ = out.flush(); + } + + disable_raw_mode()?; + crossterm::execute!( + terminal.backend_mut(), + DisableFocusChange, + LeaveAlternateScreen + )?; + terminal.show_cursor()?; + result +} + +/// True when the highlighted file differs from the one the current preview was +/// built for — i.e. a build is pending/in-flight and the pane should show a +/// loading indicator rather than stale content or images. +fn browse_selection_pending(app: &App) -> bool { + match app.browser.as_ref() { + Some(b) => { + let sel = b.selected(); + sel.is_some() && sel != b.preview_path.as_ref() + } + None => false, + } +} + +/// Adopt a finished preview build from the worker: renumber ids onto the global +/// allocator, transmit the PNGs, and +/// mark this path as the one currently shown. A read error (`doc == None`) +/// clears the preview but still records the path so the pane shows the +/// "unreadable" message instead of a perpetual spinner. +fn accept_preview(app: &mut App, resp: PreviewResponse) { + let (term_width, body_height) = app.term_size; + let body_width = term_width.saturating_sub(BROWSER_PANEL_WIDTH); + + // Reap the outgoing preview's image data first — it's ephemeral (never + // committed; commit takes `app.preview` via `take()` so we never reach here + // for a kept doc) and would otherwise stay cached in the terminal. + let stale_ids: Vec = app + .preview + .as_ref() + .map(|p| p.doc.images.iter().map(|i| i.id).collect()) + .unwrap_or_default(); + if !stale_ids.is_empty() { + let mut out = io::stdout().lock(); + let _ = app.images.forget(&mut out, &stale_ids); + let _ = out.flush(); + } + + match resp.doc { + Some(mut doc) => { + renumber_doc_images(&mut doc, &mut app.next_image_id); + app.preview = Some(DocEntry { + path: resp.path.display().to_string(), + doc, + viewport: Viewport::new(body_height, body_width), + search: None, + pending_g: false, + toc_open: false, + metadata_expanded: false, + }); + let mut out = io::stdout().lock(); + let _ = app.register_preview_images(&mut out); + let _ = out.flush(); + } + None => { + app.preview = None; + } + } + + if let Some(b) = app.browser.as_mut() { + b.preview_path = Some(resp.path); + } +} + +/// Synchronously build a `DocEntry` for `path` (used on commit when the async +/// preview isn't ready yet — a one-off blocking build on a deliberate Enter is +/// acceptable). Returns `None` if the file can't be read. +fn build_doc_entry_sync(app: &mut App, path: &std::path::Path) -> Option { + let (term_width, body_height) = app.term_size; + let body_width = term_width.saturating_sub(BROWSER_PANEL_WIDTH); + let src = std::fs::read_to_string(path).ok()?; + let mut doc = layout::build( + &src, + &app.config, + app.theme, + crate::supports_kitty_graphics(), + ); + renumber_doc_images(&mut doc, &mut app.next_image_id); + Some(DocEntry { + path: path.display().to_string(), + doc, + viewport: Viewport::new(body_height, body_width), + search: None, + pending_g: false, + toc_open: false, + metadata_expanded: false, + }) +} + fn run_ui( doc: layout::RenderedDoc, path: String, @@ -525,6 +814,42 @@ fn event_loop( let mut previous_area = None; let mut cell_pixels = None; loop { + if app.browsing { + // 1. Adopt any finished builds, discarding ones the cursor has + // since moved past (stale generation). + let mut accepted: Vec = Vec::new(); + if let Some(worker) = app.preview_worker.as_ref() { + while let Some(resp) = worker.try_recv() { + if resp.generation == app.preview_gen { + accepted.push(resp); + } + } + } + // Only the newest accepted result matters; drop the rest. + if let Some(resp) = accepted.pop() { + accept_preview(app, resp); + } + + // 2. Dispatch a build once the selection settles (debounced) and we + // haven't already requested this generation. + let (sel, settled) = match app.browser.as_ref() { + Some(b) => ( + b.selected().cloned(), + b.last_move.is_none_or(|t| t.elapsed() >= PREVIEW_DEBOUNCE), + ), + None => (None, false), + }; + if settled && app.dispatched_gen != app.preview_gen && browse_selection_pending(app) { + // Mark this generation dispatched before borrowing the worker so + // we never re-request it; one build per settle. + let gen = app.preview_gen; + app.dispatched_gen = gen; + if let (Some(worker), Some(path)) = (app.preview_worker.as_ref(), sel) { + worker.request(gen, path); + } + } + } + if focus_refresh_at.is_some_and(|deadline| Instant::now() >= deadline) { app.needs_full_redraw = true; focus_refresh_at = None; @@ -557,7 +882,11 @@ fn event_loop( if app.needs_full_redraw { cell_pixels = CellPixels::query(); } - desired = draw(frame, app, cell_pixels); + desired = if app.browsing { + draw_browse(frame, app, cell_pixels) + } else { + draw(frame, app, cell_pixels) + }; })?; { let mut out = io::stdout().lock(); @@ -566,14 +895,25 @@ fn event_loop( if app.needs_full_redraw { app.images.reset_transmissions(); app.images.reset_placements(&mut out)?; - app.register_active_images(&mut out)?; + if app.browsing { + app.register_preview_images(&mut out)?; + } else { + app.register_active_images(&mut out)?; + } app.needs_full_redraw = false; } app.images.sync(&mut out, &desired)?; out.flush()?; } - if event::poll(Duration::from_millis(50))? { + // Poll faster while a build is in flight so the finished preview pops in + // promptly; idle otherwise to keep CPU low. + let poll_ms = if app.browsing && browse_selection_pending(app) { + 16 + } else { + 50 + }; + if event::poll(Duration::from_millis(poll_ms))? { let ev = event::read()?; if app .kitty_response @@ -596,11 +936,15 @@ fn event_loop( continue; } let cursor_before = app.cursor; - match &mut app.mode { - Mode::Normal => handle_normal_key(app, &ev)?, - Mode::Search { .. } => handle_search_key(app, ev)?, - Mode::LinkSelect { .. } => handle_link_select_key(app, ev)?, - Mode::Help => handle_help_key(app, ev)?, + if app.browsing { + handle_browse_key(app, &ev); + } else { + match &mut app.mode { + Mode::Normal => handle_normal_key(app, &ev)?, + Mode::Search { .. } => handle_search_key(app, ev)?, + Mode::LinkSelect { .. } => handle_link_select_key(app, ev)?, + Mode::Help => handle_help_key(app, ev)?, + } } if app.should_quit { return Ok(()); @@ -623,6 +967,92 @@ fn event_loop( } } +/// Key handling while the File Browser holds focus (Browse mode). +fn handle_browse_key(app: &mut App, ev: &Event) { + let Event::Key(key) = ev else { + return; + }; + if key.kind != event::KeyEventKind::Press { + return; + } + let ctrl = key.modifiers.contains(event::KeyModifiers::CONTROL); + match key.code { + event::KeyCode::Char('q') => app.should_quit = true, + event::KeyCode::Char('c') if ctrl => app.should_quit = true, + event::KeyCode::Char('j') | event::KeyCode::Down => browse_move(app, 1), + event::KeyCode::Char('k') | event::KeyCode::Up => browse_move(app, -1), + event::KeyCode::Enter => browse_commit(app), + event::KeyCode::Esc => { + // No committed doc to return to (launched straight into the + // browser) → quit; otherwise drop back to the reader. + if app.docs.is_empty() { + app.should_quit = true; + } else { + app.browsing = false; + app.needs_full_redraw = true; + } + } + _ => {} + } +} + +/// Move the browser cursor by `delta`, clamped to the list bounds. Records the +/// move time (for debounce) and bumps the generation so any in-flight build for +/// the previous selection is discarded when it returns. +fn browse_move(app: &mut App, delta: i32) { + let moved = if let Some(b) = app.browser.as_mut() { + if b.entries.is_empty() { + false + } else { + let len = b.entries.len() as i32; + let next = (b.cursor as i32 + delta).clamp(0, len - 1) as usize; + if next != b.cursor { + b.cursor = next; + b.last_move = Some(Instant::now()); + true + } else { + false + } + } + } else { + false + }; + if moved { + app.preview_gen += 1; + } +} + +/// Commit the selected file: reuse its preview if the async build already +/// landed, otherwise build it synchronously now (a one-off blocking build on a +/// deliberate Enter is acceptable). Then move it onto the history stack as the +/// active document and leave Browse mode. +fn browse_commit(app: &mut App) { + let Some(sel) = app.browser.as_ref().and_then(|b| b.selected().cloned()) else { + return; + }; + let preview_ready = !browse_selection_pending(app) && app.preview.is_some(); + let entry = if preview_ready { + app.preview.take().expect("preview_ready implies Some") + } else { + match build_doc_entry_sync(app, &sel) { + Some(e) => e, + None => return, // unreadable file — stay in the browser + } + }; + if !app.docs.is_empty() { + app.history.push(app.cursor); + } + app.forward.clear(); + app.docs.push(entry); + app.cursor = app.docs.len() - 1; + app.browsing = false; + // Forget the preview tracking so re-opening the browser rebuilds it. + if let Some(b) = app.browser.as_mut() { + b.preview_path = None; + } + app.needs_full_redraw = true; +} + fn focus_refresh_delay(ev: &Event) -> Option { matches!(ev, Event::FocusGained).then_some(FOCUS_IMAGE_REFRESH_DELAY) } @@ -1242,52 +1672,23 @@ fn clipped_spans( out } -fn draw( - frame: &mut ratatui::Frame, - app: &mut App, - cell_pixels: Option, -) -> HashMap { - use ratatui::layout::{Constraint, Direction, Layout}; - - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Min(1), Constraint::Length(1)]) - .split(frame.area()); - - let (toc_area, body_area) = if app.active().toc_open { - let [toc, body] = - Layout::horizontal([Constraint::Length(TOC_PANEL_WIDTH), Constraint::Min(20)]) - .areas(chunks[0]); - (Some(toc), body) - } else { - (None, chunks[0]) - }; - app.term_size = (chunks[0].width, chunks[0].height); - let show_metadata = app.config.metadata.unwrap_or(true); - let active = app.active_mut(); - active.viewport.width = body_area.width; - active.viewport.height = body_area.height; - active.viewport.cell_pixels = cell_pixels; - active - .viewport - .ensure_wrap(&active.doc, show_metadata, active.metadata_expanded); - let active = app.active(); - - // Body +/// Render one document's visible region into ratatui lines. Shared by the +/// full-screen reader (`draw`) and the File Browser preview (`draw_browse`). +fn render_doc_body(entry: &DocEntry, theme: Theme) -> Vec> { // Precompute the current-match identity for "is this the current one" checks. - let current_logical: Option<(usize, usize)> = active.search.as_ref().and_then(|s| { + let current_logical: Option<(usize, usize)> = entry.search.as_ref().and_then(|s| { s.current.map(|i| { let m = &s.matches[i]; (m.line_index, m.byte_range.start) }) }); - let mut rendered: Vec = Vec::new(); - for vl in active.viewport.visible() { + let mut rendered: Vec> = Vec::new(); + for vl in entry.viewport.visible() { if let Some(role) = vl.metadata_row { - let body_cols = active.viewport.width as usize; + let body_cols = entry.viewport.width as usize; rendered.push(render_metadata_row( - active + entry .doc .metadata .as_ref() @@ -1303,18 +1704,164 @@ fn draw( continue; } - let logical = &active.doc.lines[vl.logical_index]; + let logical = &entry.doc.lines[vl.logical_index]; let matches = visible_matches_for_line( - active.search.as_ref(), + entry.search.as_ref(), vl.logical_index, vl.byte_start, vl.byte_end, current_logical, ); - let rspans = clipped_spans(logical, vl.byte_start, vl.byte_end, &matches, app.theme); + let rspans = clipped_spans(logical, vl.byte_start, vl.byte_end, &matches, theme); rendered.push(RLine::from(rspans)); } + rendered +} + +/// Render the File Browser: file-list panel on the left, live preview on the +/// right, status row at the bottom. +fn draw_browse( + frame: &mut ratatui::Frame, + app: &mut App, + cell_pixels: Option, +) -> HashMap { + use ratatui::layout::{Constraint, Direction, Layout}; + use ratatui::style::{Modifier, Style as RStyle}; + use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph}; + + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Min(1), Constraint::Length(1)]) + .split(frame.area()); + + let split = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Length(BROWSER_PANEL_WIDTH), Constraint::Min(20)]) + .split(chunks[0]); + + app.term_size = (chunks[0].width, chunks[0].height); + let Some(browser) = app.browser.as_ref() else { + return HashMap::new(); + }; + + // File list panel. + let items: Vec = (0..browser.entries.len()) + .map(|i| ListItem::new(browser.name_at(i))) + .collect(); + let list = List::new(items) + .block(Block::default().borders(Borders::RIGHT).title(" Files ")) + .highlight_style(RStyle::default().add_modifier(Modifier::REVERSED)); + let mut state = ListState::default(); + if !browser.entries.is_empty() { + state.select(Some(browser.cursor)); + } + frame.render_stateful_widget(list, split[0], &mut state); + + // Preview pane. While the selection is still building (async), show a + // spinner instead of stale content; otherwise the rendered doc, or an + // "unreadable" note if the build came back empty. + let dim = RStyle::default().add_modifier(Modifier::DIM); + let mut placements = HashMap::new(); + if browse_selection_pending(app) { + let msg = RLine::from(RSpan::styled(" ⟳ 加载中…", dim)); + frame.render_widget(Paragraph::new(msg), split[1]); + } else { + match app.preview.as_mut() { + Some(entry) => { + entry.viewport.width = split[1].width; + entry.viewport.height = split[1].height; + entry.viewport.cell_pixels = cell_pixels; + entry.viewport.ensure_wrap( + &entry.doc, + app.config.metadata.unwrap_or(true), + entry.metadata_expanded, + ); + placements = desired_image_placements(entry, split[1].x, None); + let rendered = render_doc_body(entry, app.theme); + frame.render_widget(Paragraph::new(rendered), split[1]); + } + None => { + let msg = RLine::from(RSpan::styled(" (无法读取该文件)", dim)); + frame.render_widget(Paragraph::new(msg), split[1]); + } + } + } + + render_browse_status(frame, chunks[1], app); + placements +} + +/// Status row for Browse mode: directory + position. +fn render_browse_status(frame: &mut ratatui::Frame, area: ratatui::layout::Rect, app: &App) { + use ratatui::style::Style as RStyle; + use ratatui::widgets::Paragraph; + use unicode_width::UnicodeWidthStr; + + let total = area.width as usize; + if total == 0 { + return; + } + let Some(browser) = app.browser.as_ref() else { + return; + }; + + let (bg, fg) = status_colors(app.theme); + let style = RStyle::default().bg(bg).fg(fg); + + let dir = browser.dir.display().to_string(); + let pos = if browser.entries.is_empty() { + "0/0".to_string() + } else { + format!("{}/{}", browser.cursor + 1, browser.entries.len()) + }; + let right = format!(" {pos} "); + let left = format!(" {dir} "); + let used = left.width() + right.width(); + let pad = total.saturating_sub(used); + let line = if used <= total { + format!("{left}{}{right}", " ".repeat(pad)) + } else { + // Tight: middle-truncate the dir, keep the position. + let max_dir = total.saturating_sub(right.width() + 2); + let t = truncate_middle(&dir, max_dir); + format!(" {t} {right}") + }; + frame.render_widget(Paragraph::new(line).style(style), area); +} + +fn draw( + frame: &mut ratatui::Frame, + app: &mut App, + cell_pixels: Option, +) -> HashMap { + use ratatui::layout::{Constraint, Direction, Layout}; + + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Min(1), Constraint::Length(1)]) + .split(frame.area()); + + let (toc_area, body_area) = if app.active().toc_open { + let [toc, body] = + Layout::horizontal([Constraint::Length(TOC_PANEL_WIDTH), Constraint::Min(20)]) + .areas(chunks[0]); + (Some(toc), body) + } else { + (None, chunks[0]) + }; + app.term_size = (chunks[0].width, chunks[0].height); + let show_metadata = app.config.metadata.unwrap_or(true); + let active = app.active_mut(); + active.viewport.width = body_area.width; + active.viewport.height = body_area.height; + active.viewport.cell_pixels = cell_pixels; + active + .viewport + .ensure_wrap(&active.doc, show_metadata, active.metadata_expanded); + let active = app.active(); + + let rendered = render_doc_body(active, app.theme); if let Some(area) = toc_area { let toc_items: Vec = active @@ -1355,7 +1902,7 @@ fn draw( } render_status_bar(frame, chunks[1], app); - desired_image_placements(app, body_area.x, popup) + desired_image_placements(active, body_area.x, popup) } const HELP_SECTIONS: &[(&str, &[(&str, &str)])] = &[ @@ -1684,11 +2231,10 @@ fn progress_percent(app: &App) -> u32 { } fn desired_image_placements( - app: &App, + active: &DocEntry, col_offset: u16, popup: Option, ) -> HashMap { - let active = app.active(); let mut out = HashMap::new(); let mut row = 0; for group in active @@ -1875,7 +2421,7 @@ mod renumber_tests { active.viewport.cell_pixels = Some(CellPixels::default()); active.viewport.ensure_wrap(&active.doc, true, false); - let placements = desired_image_placements(&app, 0, None); + let placements = desired_image_placements(app.active(), 0, None); assert_eq!( placements[&1], kitty::Placement { @@ -1938,7 +2484,7 @@ mod renumber_tests { // Leave only the last body row available for its first slice. let active = app.active_mut(); active.viewport.ensure_wrap(&active.doc, false, false); - let placement = desired_image_placements(&app, 0, None)[&1]; + let placement = desired_image_placements(app.active(), 0, None)[&1]; assert_eq!(placement.row, 39); assert_eq!( placement.crop, @@ -2014,7 +2560,7 @@ mod renumber_tests { handle_normal_key(&mut app, &Event::Key(event::KeyEvent::from(key))).unwrap(); assert_eq!(app.active().viewport.top, top); assert_eq!( - desired_image_placements(&app, 0, None).contains_key(&1), + desired_image_placements(app.active(), 0, None).contains_key(&1), has_diagram ); }