diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1400c1b..fe16311 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 components: rustfmt @@ -84,7 +84,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-audit@0.22.1 # Acknowledged advisories live in .cargo/audit.toml (see docs/AUDIT.md), so @@ -101,12 +101,12 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 components: clippy - run: sudo apt-get update && sudo apt-get install --yes libfuse3-dev pkg-config - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: save-if: ${{ github.ref == 'refs/heads/main' }} # `--features bench` is required for the tui bench target: its @@ -130,11 +130,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 - run: sudo apt-get update && sudo apt-get install --yes gnome-keyring dbus-bin - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo build -p notema-storage --tests --locked @@ -158,12 +158,12 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 - run: brew install pkg-config - run: brew install --cask fuse-t - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo check --workspace --all-targets --locked @@ -182,10 +182,10 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: save-if: ${{ github.ref == 'refs/heads/main' }} # FUSE doesn't exist on Windows (notema-fuse needs libfuse) and the Windows @@ -246,11 +246,11 @@ jobs: with: persist-credentials: false - run: sudo apt-get update && sudo apt-get install --yes ${{ matrix.packages }} - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: key: ${{ matrix.target }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -285,7 +285,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: aarch64-linux-android @@ -297,7 +297,7 @@ jobs: echo "CC_aarch64_linux_android=$TOOLCHAIN/aarch64-linux-android24-clang" echo "AR_aarch64_linux_android=$TOOLCHAIN/llvm-ar" } >> "$GITHUB_ENV" - - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo check -p notema --target aarch64-linux-android --locked diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6480076..619f199 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -156,7 +156,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} @@ -170,7 +170,7 @@ jobs: with: version: 0.16.0 cache-key: ${{ matrix.target }} - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: ${{ matrix.zigbuild && 'cargo-about@0.9.1,cargo-zigbuild@0.23.0' || 'cargo-about@0.9.1' }} # The glibc_floor suffix pins the glibc floor; the binary still lands in @@ -267,11 +267,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-about@0.9.1 - run: cargo build --release -p notema --locked --target ${{ matrix.target }} --features fuse @@ -315,7 +315,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: i586-unknown-linux-musl @@ -342,7 +342,7 @@ jobs: # name for the x86_64 *host* linker (used by build scripts); on this # runner the host toolchain is plain gcc, so bridge that name too. sudo ln -sf "$(command -v gcc)" /usr/local/bin/x86_64-unknown-linux-gnu-gcc - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-make@0.37.24,cargo-about@0.9.1 # setup-cross-toolchain-action sets CARGO_BUILD_TARGET to i686-musl; the @@ -389,7 +389,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: aarch64-linux-android @@ -401,7 +401,7 @@ jobs: echo "CC_aarch64_linux_android=$TOOLCHAIN/aarch64-linux-android24-clang" echo "AR_aarch64_linux_android=$TOOLCHAIN/llvm-ar" } >> "$GITHUB_ENV" - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-about@0.9.1 - run: cargo build --release -p notema --target aarch64-linux-android --locked @@ -463,7 +463,7 @@ jobs: with: persist-credentials: false # Tier 3 has no rustup target to add; build-std needs rust-src instead. - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: ${{ matrix.toolchain }} targets: ${{ matrix.build_std && '' || matrix.target }} @@ -473,7 +473,7 @@ jobs: with: version: 0.16.0 cache-key: ${{ matrix.target }} - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-about@0.9.1,cargo-zigbuild@0.23.0 - if: ${{ !matrix.build_std }} @@ -542,11 +542,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-about@0.9.1 - run: cargo build --release -p notema --target ${{ matrix.target }} --locked @@ -589,7 +589,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} @@ -614,7 +614,7 @@ jobs: TEAM_ID="$(printf '%s' "$APPLE_DEVELOPER_ID" | sed -n 's/.*(\(.*\)).*/\1/p')" xcrun notarytool store-credentials notema-notary \ --apple-id "$APPLE_USERNAME" --password "$APPLE_PASSWORD" --team-id "$TEAM_ID" >/dev/null - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-make@0.37.24,cargo-about@0.9.1 - run: cargo make ${{ matrix.task }} @@ -677,7 +677,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # master + - uses: dtolnay/rust-toolchain@6c977a6ca4077a0ceb28ffbe03f59d46e9ac8772 # master with: toolchain: 1.96.0 targets: ${{ matrix.target }} @@ -704,7 +704,7 @@ jobs: TEAM_ID="$(printf '%s' "$APPLE_DEVELOPER_ID" | sed -n 's/.*(\(.*\)).*/\1/p')" xcrun notarytool store-credentials notema-notary \ --apple-id "$APPLE_USERNAME" --password "$APPLE_PASSWORD" --team-id "$TEAM_ID" >/dev/null - - uses: taiki-e/install-action@1beb33eee6d086258184383af9a538940be190ed # v2 + - uses: taiki-e/install-action@742a3317eac7bd62f91cd888b4eead5e784ba833 # v2 with: tool: cargo-make@0.37.24,cargo-about@0.9.1 - run: cargo make ${{ matrix.task }} @@ -803,13 +803,13 @@ jobs: # Provenance links each zip to this workflow run; verify with # `gh attestation verify --repo /`. - name: Attest build provenance - uses: actions/attest-build-provenance@0f67c3f4856b2e3261c31976d6725780e5e4c373 # v4.1.1 + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 with: subject-path: | assets/*.zip assets/SHA256SUMS - name: Publish - uses: softprops/action-gh-release@3d0d9888cb7fd7b750713d6e236d1fcb99157228 # v3.0.2 + uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3 with: tag_name: ${{ github.ref_name }} fail_on_unmatched_files: true diff --git a/Cargo.lock b/Cargo.lock index 7866ef1..c66b373 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2466,6 +2466,8 @@ dependencies = [ "tree-sitter-toml-ng", "tree-sitter-typescript", "tree-sitter-yaml", + "unicode-linebreak", + "unicode-segmentation", "unicode-width", "zeroize", ] @@ -2609,12 +2611,13 @@ dependencies = [ [[package]] name = "notema-textarea" -version = "0.9.2" -source = "git+https://github.com/paviro/notema-textarea?rev=cbf1b84#cbf1b8448599c981a398975c8bc4581286f019e8" +version = "0.9.3" +source = "git+https://github.com/paviro/notema-textarea?rev=6e7fa5139d1d306b3043880e926394351dfcd13b#6e7fa5139d1d306b3043880e926394351dfcd13b" dependencies = [ "ratatui-core", "ratatui-crossterm", "ratatui-widgets", + "unicode-linebreak", "unicode-segmentation", "unicode-width", ] @@ -4830,6 +4833,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + [[package]] name = "unicode-segmentation" version = "1.13.3" diff --git a/Cargo.toml b/Cargo.toml index 909ddc5..13b8076 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,14 +52,11 @@ pkg-config = "0.3" rand = "0.10" ratatui = "0.30" ratatui-image = { version = "11", default-features = false, features = ["crossterm"] } -# Fork of ratatui-textarea 0.9.2 the editor depends on: public accessors -# (scroll_offset, horizontal_scroll_offset, screen_line_count, cursor_at_screen) -# for its scrollbar, scroll-aware mouse mapping, and native-cursor placement in -# horizontally-scrolling single-line fields, plus set_syntax_spans for the markdown -# highlighter's base styling layer. Upstream keeps these pub(crate). Also -# set_top_padding and layout_for, so the editor can frame the body like the viewer, -# and set_glyph_substitutions for the search field's chips. -ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "cbf1b84", features = ["crossterm"] } +# Fork of ratatui-textarea 0.9.3, pinned to a tested commit. Upstream supplies +# scroll_offset and screen_to_data; the fork adds padding-aware mouse mapping, +# screen-line counts, pre-render layout, syntax spans, glyph substitutions, +# Unicode wrapping, and grapheme cursor geometry. +ratatui-textarea = { package = "notema-textarea", git = "https://github.com/paviro/notema-textarea", rev = "6e7fa5139d1d306b3043880e926394351dfcd13b", features = ["crossterm"] } pulldown-cmark = { version = "0.13.4", default-features = false } rayon = "1" regex = "1" @@ -90,6 +87,8 @@ tree-sitter-toml-ng = "0.7" tree-sitter-typescript = "0.23" tree-sitter-yaml = "0.7" tzf-rs = { version = "1", default-features = false, features = ["bundled"] } +unicode-linebreak = "0.1.5" +unicode-segmentation = "1.13.3" unicode-width = "0.2" ureq = { version = "3", default-features = false } zbus = "5" @@ -181,6 +180,8 @@ tree-sitter-swift.workspace = true tree-sitter-toml-ng.workspace = true tree-sitter-typescript.workspace = true tree-sitter-yaml.workspace = true +unicode-linebreak.workspace = true +unicode-segmentation.workspace = true unicode-width.workspace = true zeroize.workspace = true terminal-colorsaurus.workspace = true diff --git a/src/tui/editor_state.rs b/src/tui/editor_state.rs index aac83f6..e379920 100644 --- a/src/tui/editor_state.rs +++ b/src/tui/editor_state.rs @@ -185,9 +185,9 @@ impl EntryEditor { { return None; } - let screen_row = self.textarea.scroll_offset() as usize + (row - rect.y) as usize; + let screen_row = self.textarea.scroll_offset().0 as usize + (row - rect.y) as usize; let screen_col = (col - rect.x) as usize; - let DataCursor(line, column) = self.textarea.cursor_at_screen(screen_row, screen_col); + let DataCursor(line, column) = self.textarea.screen_to_data(screen_row, screen_col); Some((line as u16, column as u16)) } @@ -197,7 +197,7 @@ impl EntryEditor { pub(crate) fn scroll_lines(&mut self, delta: i16) { let height = self.text_rect.height as usize; let max_top = self.textarea.screen_line_count().saturating_sub(height) as i64; - let cur = self.textarea.scroll_offset() as i64; + let cur = self.textarea.scroll_offset().0 as i64; let target = (cur + delta as i64).clamp(0, max_top); if target != cur { self.textarea.scroll(((target - cur) as i16, 0)); @@ -267,6 +267,41 @@ fn new_textarea(body: &str, placeholder: Option<&str>) -> TextArea<'static> { #[cfg(test)] mod tests { use super::*; + use ratatui::{buffer::Buffer, widgets::Widget as _}; + use ratatui_textarea::CursorMove; + + #[test] + fn padded_editor_clicks_follow_wrapping_scrolling_and_resize() { + let body = "e\u{301}ζ—₯πŸ‘©πŸ½β€πŸ’»\tend\nsecond line\nlast"; + let mut editor = EntryEditor::for_new("work".to_string()); + editor.textarea.insert_str(body); + editor.textarea.set_top_padding(2); + for rect in [Rect::new(4, 3, 12, 6), Rect::new(4, 3, 5, 3)] { + editor.text_rect = rect; + editor.textarea.layout_for(rect); + editor.textarea.move_cursor(CursorMove::Top); + editor.textarea.render(rect, &mut Buffer::empty(rect)); + assert_eq!(editor.text_pos_at(rect.x, rect.y), Some((0, 0))); + assert_eq!( + editor.text_pos_at(rect.x + 2, rect.y + editor.textarea.top_padding()), + Some((0, 2)) + ); + assert_eq!(editor.text_pos_at(rect.x - 1, rect.y), None); + assert_eq!(editor.text_pos_at(rect.x, rect.bottom()), None); + + editor.textarea.move_cursor(CursorMove::Bottom); + editor.textarea.move_cursor(CursorMove::End); + editor.textarea.render(rect, &mut Buffer::empty(rect)); + let screen = editor.textarea.screen_cursor(); + let y = rect.y + screen.row as u16 + editor.textarea.top_padding() + - editor.textarea.scroll_offset().0; + assert_eq!( + editor.text_pos_at(rect.x + screen.col as u16, y), + Some((2, 4)) + ); + assert_eq!(editor.text(), body); + } + } /// A false positive here means the editor keeps painting a stale highlight, /// so the in-place comparison has to agree with `lines.join("\n")` exactly. diff --git a/src/tui/image/graphics.rs b/src/tui/image/graphics.rs index f72ce52..af89721 100644 --- a/src/tui/image/graphics.rs +++ b/src/tui/image/graphics.rs @@ -1,12 +1,13 @@ //! Terminal-graphics backend: protocol detection and encoding a decoded image -//! into a [`SlicedProtocol`] sized to fit the viewer. +//! into a [`Protocol`] sized to fit the viewer. use notema_storage::JournalStore; use notema_timing as timing; use ratatui::layout::Size; use ratatui_image::{ + Resize, picker::{Picker, ProtocolType}, - sliced::SlicedProtocol, + protocol::Protocol, }; use super::CacheKey; @@ -51,7 +52,7 @@ pub(super) fn build_protocol( store: &JournalStore, picker: &Picker, key: &CacheKey, -) -> Option { +) -> Option { let bytes = store .read_entry_asset_bytes(&key.entry_path, &key.file_name) .ok()??; @@ -68,8 +69,9 @@ pub(super) fn build_protocol( let image = notema_storage::decode_image_with_orientation(&bytes, Some((max_width, max_height))) .ok()?; - // `SlicedProtocol` fits the image into the area preserving aspect ratio, + // A single whole-image protocol (one OSC 1337 for iTerm2, not one per row): + // `Resize::Fit` fits the image into `bounds` preserving aspect ratio, // downscaling the already-capped image to the cell footprint. let bounds = Size::new(key.width.max(1), key.height.max(1)); - SlicedProtocol::new(picker, image, Some(bounds)).ok() + picker.new_protocol(image, bounds, Resize::Fit(None)).ok() } diff --git a/src/tui/image/mod.rs b/src/tui/image/mod.rs index aea2fe8..8687ab9 100644 --- a/src/tui/image/mod.rs +++ b/src/tui/image/mod.rs @@ -27,10 +27,7 @@ use ratatui::{ layout::{Rect, Size}, widgets::Paragraph, }; -use ratatui_image::{ - picker::ProtocolType, - sliced::{SignedPosition, SlicedImage, SlicedProtocol}, -}; +use ratatui_image::{Image, picker::ProtocolType, protocol::Protocol}; use ascii::AsciiArt; use worker::{BuildMode, BuildRequest, Built, Worker}; @@ -96,7 +93,7 @@ pub(super) struct CacheKey { enum CacheState { Loading, - ReadyGraphics(Rc), + ReadyGraphics(Rc), ReadyAscii(Rc), Failed, } @@ -104,7 +101,7 @@ enum CacheState { /// `Rc`-backed handle to a finished build, cloned out of the cache so the /// borrow can be dropped before painting. enum Ready { - Graphics(Rc), + Graphics(Rc), Ascii(Rc), } @@ -257,11 +254,16 @@ impl ImageRuntime { match ready { Some(Ready::Graphics(proto)) => { // Aspect-preserving fit may letterbox β€” center the rendered cells. + // `Image` draws at the top-left of its area and skips rendering if + // the protocol is larger than it, so pass an exact centered rect. let rendered = proto.size(); - let x = (area.width.saturating_sub(rendered.width) / 2) as i16; - let y = (area.height.saturating_sub(rendered.height) / 2) as i16; - let position = SignedPosition::from((x, y)); - frame.render_widget(SlicedImage::new(&proto, position), area); + let rect = Rect { + x: area.x + area.width.saturating_sub(rendered.width) / 2, + y: area.y + area.height.saturating_sub(rendered.height) / 2, + width: rendered.width.min(area.width), + height: rendered.height.min(area.height), + }; + frame.render_widget(Image::new(&proto), rect); } Some(Ready::Ascii(art)) => { // Already sized to fit inside `area`; center it. diff --git a/src/tui/image/worker.rs b/src/tui/image/worker.rs index 91ad281..8f77950 100644 --- a/src/tui/image/worker.rs +++ b/src/tui/image/worker.rs @@ -9,7 +9,7 @@ use std::{ }; use notema_storage::JournalStore; -use ratatui_image::{picker::Picker, sliced::SlicedProtocol}; +use ratatui_image::{picker::Picker, protocol::Protocol}; use super::CacheKey; use super::ascii::{self, AsciiArt}; @@ -25,7 +25,7 @@ pub(super) enum BuildMode { /// A finished build coming back from the worker, tagged by backend. pub(super) enum Built { - Graphics(SlicedProtocol), + Graphics(Protocol), Ascii(AsciiArt), } diff --git a/src/tui/render/editor.rs b/src/tui/render/editor.rs index a7f6457..ff83ef1 100644 --- a/src/tui/render/editor.rs +++ b/src/tui/render/editor.rs @@ -95,7 +95,7 @@ pub(crate) fn draw_entry_editor( // maps directly. Valid only after render. if !selecting && matches!(editor.prompt, EditorPrompt::None) { let sc = editor.textarea.screen_cursor(); - let scroll = editor.textarea.scroll_offset() as usize; + let scroll = editor.textarea.scroll_offset().0 as usize; let row = sc.row + editor.textarea.top_padding() as usize; if let Some(rel) = row.checked_sub(scroll) { let x = text_rect.x + sc.col as u16; @@ -114,7 +114,7 @@ pub(crate) fn draw_entry_editor( area, editor.textarea.screen_line_count(), text_rect.height, - editor.textarea.scroll_offset() as usize, + editor.textarea.scroll_offset().0 as usize, // The editor is always the active surface while shown. true, ); diff --git a/src/tui/render/markdown.rs b/src/tui/render/markdown.rs index d5a4171..9e8057a 100644 --- a/src/tui/render/markdown.rs +++ b/src/tui/render/markdown.rs @@ -884,6 +884,51 @@ mod wrap_tests { .lines } + #[test] + fn unicode_links_keep_cell_ranges_after_wrapping_and_prefixes() { + let rendered = render_text_chunk( + &Theme::terminal_default(), + "> [πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ](https://example.com)", + 4, + false, + None, + false, + None, + ); + assert_eq!(rendered.links.len(), 2); + for hit in &rendered.links { + assert_eq!((hit.start, hit.end), (2, 4)); + assert_eq!(hit.group, rendered.links[0].group); + assert_eq!(rendered.lines[hit.line].width(), 4); + } + } + + #[test] + fn unicode_code_wraps_keep_graphemes_and_indentation() { + let rows = render_lines("```\n πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ\n```", 5); + let rail = Theme::terminal_default() + .glyphs() + .markdown + .code_rail + .clone(); + let body: Vec<_> = rows + .iter() + .map(text) + .filter(|row| row.starts_with(&rail)) + .collect(); + assert_eq!(body, [format!("{rail} πŸ‘©πŸ½β€πŸ’»"), format!("{rail}πŸ‡©πŸ‡ͺ")]); + } + + #[test] + fn unicode_table_columns_stay_aligned() { + let rows = render_lines("| Emoji | Value |\n| --- | ---: |\n| πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ | ❀️ |", 18); + let table: Vec<_> = rows.iter().filter(|row| !text(row).is_empty()).collect(); + let width = table[0].width(); + assert!(width <= 18); + assert!(table.iter().all(|row| row.width() == width)); + assert!(table.iter().any(|row| text(row).contains("πŸ‘©πŸ½β€πŸ’»"))); + } + #[test] fn wraps_at_words_instead_of_the_panel_edge() { let wrapped = wrap_line(Line::from("alpha beta"), 7); diff --git a/src/tui/render/markdown/wrapping.rs b/src/tui/render/markdown/wrapping.rs index 07b03e5..b474334 100644 --- a/src/tui/render/markdown/wrapping.rs +++ b/src/tui/render/markdown/wrapping.rs @@ -1,8 +1,12 @@ +use std::ops::Range; + use ratatui::{ style::Style, text::{Line, Span}, }; -use unicode_width::UnicodeWidthChar; +use unicode_linebreak::{BreakClass, BreakOpportunity, break_property, linebreaks}; +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; use super::RichSpan; @@ -11,58 +15,68 @@ pub(super) struct WrappedLine { pub(super) links: Vec<(usize, usize, usize)>, } -#[derive(Clone, Copy)] -struct StyledCharacter { - character: char, +struct StyledGrapheme<'a> { + text: &'a str, + end: usize, width: usize, style: Style, link: Option, } -/// Wrap link-tagged runs to `width`, returning display lines that carry both the -/// collapsed spans and the link regions the wrap produced. `hard_wrap` breaks -/// anywhere (rules, code); otherwise it breaks at whitespace, splitting a lone -/// token only when it alone exceeds the width. +/// Wrap styled text without separating a grapheme, including across input spans. +/// Prose uses Unicode line breaks; code and rules use grapheme boundaries. pub(super) fn wrap_rich(spans: Vec, width: usize, hard_wrap: bool) -> Vec { - let mut characters = Vec::new(); - for span in spans { - for character in span.content.chars() { - characters.push(StyledCharacter { - character, - width: character.width().unwrap_or(0), - style: span.style, - link: span.link, - }); - } - } - let char_lines = if hard_wrap { - hard_wrap_chars(characters, width) + let text: String = spans.iter().map(|span| span.content.as_str()).collect(); + let mut runs = spans.iter(); + let mut run = runs.next(); + let mut run_end = run.map_or(0, |span| span.content.len()); + let graphemes: Vec<_> = text + .grapheme_indices(true) + .map(|(start, text)| { + while start >= run_end { + run = runs.next(); + run_end += run.map_or(0, |span| span.content.len()); + } + let run = run.expect("each grapheme belongs to a source span"); + StyledGrapheme { + text, + end: start + text.len(), + width: text.width(), + style: run.style, + link: run.link, + } + }) + .collect(); + let width = width.max(1); + let mut rows = Vec::new(); + if hard_wrap { + split_graphemes(&graphemes, 0..graphemes.len(), width, &mut rows); } else { - soft_wrap_chars(characters, width) - }; - char_lines.iter().map(|line| finalize_line(line)).collect() + rows = prose_rows(&text, &graphemes, width); + } + if rows.is_empty() { + rows.push(0..0); + } + rows.into_iter() + .map(|mut row| { + if !hard_wrap { + while row.end > row.start && is_break_space(graphemes[row.end - 1].text) { + row.end -= 1; + } + } + finalize_line(&graphemes[row]) + }) + .collect() } -/// Word-wrap the table path, which has no link semantics; the surviving caller of -/// the plain-`Line` shape. Preserves the line-level style/alignment tables set. +/// Wrap a table cell while retaining its line-level style and alignment. pub(super) fn wrap_line(line: Line<'static>, width: usize) -> Vec> { let style = line.style; let alignment = line.alignment; - let mut characters = Vec::new(); - for span in line.spans { - for character in span.content.chars() { - characters.push(StyledCharacter { - character, - width: character.width().unwrap_or(0), - style: span.style, - link: None, - }); - } - } - soft_wrap_chars(characters, width) - .iter() - .map(|chars| { - let mut line = Line::from(finalize_line(chars).spans); + wrap_rich(rich_from_line(line), width, false) + .into_iter() + .map(|wrapped| { + let mut line = Line::from(wrapped.spans); line.style = style; line.alignment = alignment; line @@ -70,120 +84,174 @@ pub(super) fn wrap_line(line: Line<'static>, width: usize) -> Vec> .collect() } -fn soft_wrap_chars(characters: Vec, width: usize) -> Vec> { - let mut wrapped: Vec> = vec![Vec::new()]; - let mut used = 0usize; - let mut pending_whitespace: Option<&[StyledCharacter]> = None; - let mut cursor = 0; +fn is_break_space(text: &str) -> bool { + text.chars().all(|c| { + c == '\t' + || matches!( + break_property(c as u32), + BreakClass::Space + | BreakClass::Mandatory + | BreakClass::CarriageReturn + | BreakClass::LineFeed + | BreakClass::NextLine + ) + }) +} + +fn cells(graphemes: &[StyledGrapheme<'_>]) -> usize { + graphemes.iter().map(|g| g.width).sum() +} - while cursor < characters.len() { - let whitespace = characters[cursor].character.is_whitespace(); - let end = characters[cursor..] - .iter() - .position(|character| character.character.is_whitespace() != whitespace) - .map_or(characters.len(), |offset| cursor + offset); - let token = &characters[cursor..end]; - - if whitespace { - pending_whitespace = Some(token); - cursor = end; +fn prose_rows(text: &str, graphemes: &[StyledGrapheme<'_>], width: usize) -> Vec> { + let mut rows = Vec::new(); + let mut cursor = 0; + let mut chunk_start = 0; + let mut row_start = 0; + let mut row_width = 0; + for (byte_end, opportunity) in linebreaks(text) { + while cursor < graphemes.len() && graphemes[cursor].end < byte_end { + cursor += 1; + } + if cursor == graphemes.len() || graphemes[cursor].end != byte_end { continue; } + let end = cursor + 1; + let mut content_end = end; + while content_end > chunk_start && is_break_space(graphemes[content_end - 1].text) { + content_end -= 1; + } + let content_width = cells(&graphemes[chunk_start..content_end]); + if row_width + content_width > width && chunk_start > row_start { + rows.push(row_start..chunk_start); + row_start = chunk_start; + row_width = 0; + } + if content_width > width { + split_word(graphemes, chunk_start..content_end, width, &mut rows); + let last = rows.pop().expect("an oversized segment produces a row"); + row_start = last.start; + row_width = cells(&graphemes[row_start..end]); + } else { + row_width += cells(&graphemes[chunk_start..end]); + } + chunk_start = end; + if opportunity == BreakOpportunity::Mandatory { + rows.push(row_start..end); + row_start = end; + row_width = 0; + } + } + if row_start < chunk_start { + rows.push(row_start..chunk_start); + } + rows +} - let whitespace_width = pending_whitespace.map_or(0, token_width); - let word_width = token_width(token); - if used > 0 - && used - .saturating_add(whitespace_width) - .saturating_add(word_width) - > width - { - wrapped.push(Vec::new()); - used = 0; - } else if let Some(whitespace) = pending_whitespace { - wrapped - .last_mut() - .expect("wrapped output always has a line") - .extend_from_slice(whitespace); - used = used.saturating_add(whitespace_width); - } - pending_whitespace = None; - - for character in token { - if used > 0 && used.saturating_add(character.width) > width { - wrapped.push(Vec::new()); - used = 0; +fn split_word( + graphemes: &[StyledGrapheme<'_>], + range: Range, + width: usize, + rows: &mut Vec>, +) { + let mut tail = range.end; + for index in range.clone().rev() { + let punctuation = graphemes[index].text.chars().next().is_some_and(|c| { + matches!( + break_property(c as u32), + BreakClass::ClosePunctuation + | BreakClass::CloseParenthesis + | BreakClass::Exclamation + | BreakClass::InfixSeparator + | BreakClass::Inseparable + | BreakClass::NonStarter + | BreakClass::Quotation + | BreakClass::Postfix + | BreakClass::Symbol + ) + }); + if punctuation { + tail = index; + } else { + if tail < range.end && !graphemes[index].text.chars().all(char::is_whitespace) { + tail = index; } - wrapped - .last_mut() - .expect("wrapped output always has a line") - .push(*character); - used = used.saturating_add(character.width); + break; } - cursor = end; } - - wrapped + if tail == range.end || cells(&graphemes[tail..range.end]) > width { + split_graphemes(graphemes, range, width, rows); + return; + } + let first = rows.len(); + split_graphemes(graphemes, range.start..tail, width, rows); + if rows.len() > first { + let last = rows.last_mut().expect("the prefix produced a row"); + if cells(&graphemes[last.start..range.end]) <= width { + last.end = range.end; + return; + } + } + rows.push(tail..range.end); } -fn hard_wrap_chars(characters: Vec, width: usize) -> Vec> { - let mut wrapped: Vec> = vec![Vec::new()]; - let mut used = 0usize; - for character in characters { - if used > 0 && used.saturating_add(character.width) > width { - wrapped.push(Vec::new()); +fn split_graphemes( + graphemes: &[StyledGrapheme<'_>], + range: Range, + width: usize, + rows: &mut Vec>, +) { + let mut start = range.start; + let mut used = 0; + for index in range.clone() { + if index > start && used + graphemes[index].width > width { + rows.push(start..index); + start = index; used = 0; } - wrapped - .last_mut() - .expect("wrapped output always has a line") - .push(character); - used = used.saturating_add(character.width); + used += graphemes[index].width; + } + if start < range.end { + rows.push(start..range.end); } - wrapped } -/// Collapse a wrapped display line's characters into style-merged spans while -/// recording each contiguous same-id link run's column span. -fn finalize_line(characters: &[StyledCharacter]) -> WrappedLine { +fn finalize_line(graphemes: &[StyledGrapheme<'_>]) -> WrappedLine { let mut spans: Vec> = Vec::new(); let mut links = Vec::new(); - let mut column = 0usize; - let mut run: Option<(usize, usize)> = None; - for character in characters { + let mut column = 0; + let mut run = None; + let mut previous_link = None; + for grapheme in graphemes { if let Some(span) = spans.last_mut() - && span.style == character.style + && span.style == grapheme.style + && previous_link == grapheme.link { - span.content.to_mut().push(character.character); + span.content.to_mut().push_str(grapheme.text); } else { - spans.push(Span::styled( - character.character.to_string(), - character.style, - )); + spans.push(Span::styled(grapheme.text.to_owned(), grapheme.style)); } - match (run, character.link) { + previous_link = grapheme.link; + match (run, grapheme.link) { (Some((id, _)), Some(current)) if id == current => {} (Some((id, start)), _) => { - links.push((start, column, id)); - run = character.link.map(|current| (current, column)); + if start < column { + links.push((start, column, id)); + } + run = grapheme.link.map(|current| (current, column)); } (None, Some(current)) => run = Some((current, column)), (None, None) => {} } - column = column.saturating_add(character.width); + column += grapheme.width; } - if let Some((id, start)) = run { + if let Some((id, start)) = run + && start < column + { links.push((start, column, id)); } WrappedLine { spans, links } } -fn token_width(token: &[StyledCharacter]) -> usize { - token.iter().fold(0usize, |width, character| { - width.saturating_add(character.width) - }) -} - /// Convert a plain `Line` (code, rules, stacked-table rows β€” none carry link /// semantics) into the renderer's tagged runs for emission. pub(super) fn rich_from_line(line: Line<'static>) -> Vec { @@ -196,3 +264,193 @@ pub(super) fn rich_from_line(line: Line<'static>) -> Vec { }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + use ratatui::{buffer::Buffer, layout::Rect, style::Modifier, widgets::Widget}; + + fn visible(text: &str, width: usize) -> Vec { + wrap_rich( + vec![RichSpan { + content: text.to_owned(), + style: Style::default(), + link: None, + }], + width, + false, + ) + .into_iter() + .map(|line| { + line.spans + .iter() + .map(|span| span.content.as_ref()) + .collect() + }) + .collect() + } + + #[test] + fn unicode_breaks_and_punctuation_match_editor_policy() { + for (text, width, expected) in [ + ("a (word) end", 7, vec!["a", "(word)", "end"]), + ("δ½ ε₯½οΌŒδΈ–η•Œγ€‚", 6, vec!["δ½ ε₯½οΌŒ", "δΈ–η•Œγ€‚"]), + ("δ½ ε₯½οΌŒδΈ–η•Œγ€‚", 4, vec!["δ½ ", "ε₯½οΌŒ", "δΈ–", "η•Œγ€‚"]), + ("hello,", 5, vec!["hell", "o,"]), + ("abcdef?!", 3, vec!["abc", "de", "f?!"]), + ("abcdef?! next", 6, vec!["abcde", "f?!", "next"]), + ("abcde\u{301}!", 5, vec!["abcd", "e\u{301}!"]), + ("hello,", 1, vec!["h", "e", "l", "l", "o", ","]), + ("abc?!", 2, vec!["ab", "c?", "!"]), + ("hello world", 5, vec!["hello", "world"]), + ("", 0, vec![""]), + (" ", 0, vec![""]), + ] { + assert_eq!(visible(text, width), expected, "{text:?} at {width}"); + } + for punctuation in [ + ",", ".", "!", "?", ":", ";", "…", "?!", "...", ")", "]", "}", "\"", "”", "’", "。」", + ] { + let word = format!("word{punctuation}"); + assert_eq!( + visible(&format!("a {word} next"), word.width().max(6)), + ["a".to_owned(), word, "next".to_owned()] + ); + } + } + + #[test] + fn nonbreaking_spaces_and_explicit_breaks_survive() { + for joined in ["ab\u{a0}cd", "ab\u{202f}cd", "ab\u{2060}cd"] { + assert_eq!(visible(&format!("x {joined}"), 5), ["x", joined]); + } + assert_eq!(visible("ab\u{200b}cd", 2), ["ab\u{200b}", "cd"]); + for separator in ["\n", "\r\n", "\u{85}", "\u{2028}", "\u{2029}"] { + assert_eq!( + visible(&format!("one{separator}two{separator}"), 80), + ["one", "two"] + ); + } + } + + #[test] + fn style_boundaries_cannot_split_a_grapheme_or_its_link() { + let style = Style::new().add_modifier(Modifier::BOLD); + let rows = wrap_rich( + vec![ + RichSpan { + content: "aπŸ‘©".into(), + style, + link: Some(0), + }, + RichSpan { + content: "πŸ½β€πŸ’»".into(), + style: Style::default(), + link: Some(1), + }, + RichSpan { + content: "b".into(), + style: Style::default(), + link: Some(1), + }, + ], + 3, + false, + ); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0].links, [(0, 3, 0)]); + assert_eq!(rows[1].links, [(0, 1, 1)]); + let area = Rect::new(0, 0, 3, 1); + let mut buffer = Buffer::empty(area); + Line::from(rows[0].spans.clone()).render(area, &mut buffer); + assert_eq!(buffer[(1, 0)].symbol(), "πŸ‘©πŸ½β€πŸ’»"); + assert_eq!(buffer[(1, 0)].style().add_modifier, Modifier::BOLD); + } + + #[test] + fn same_style_different_links_keep_separate_spans() { + let rows = wrap_rich( + vec![ + RichSpan { + content: "πŸ‘©πŸ½β€πŸ’»".into(), + style: Style::default(), + link: Some(0), + }, + RichSpan { + content: "πŸ‡©πŸ‡ͺ".into(), + style: Style::default(), + link: Some(1), + }, + ], + 4, + false, + ); + assert_eq!(rows[0].links, [(0, 2, 0), (2, 4, 1)]); + assert_eq!(rows[0].spans.len(), 2); + } + + #[test] + fn hard_wrapping_preserves_text_and_all_wraps_preserve_clusters() { + for text in [ + "a e\u{301}! πŸ‘©πŸ½β€πŸ’» πŸ‡©πŸ‡ͺ end", + "❀\u{fe0f} 1️⃣!", + "δ½ ε₯½οΌŒδΈ–η•Œγ€‚", + " x ", + ] { + let boundaries: Vec<_> = text + .grapheme_indices(true) + .map(|(i, _)| i) + .chain([text.len()]) + .collect(); + for width in 0..=20 { + for hard in [false, true] { + let rows = wrap_rich( + vec![RichSpan { + content: text.into(), + style: Style::default(), + link: None, + }], + width, + hard, + ); + let mut offset = 0; + for row in rows { + let rendered: String = + row.spans.iter().map(|s| s.content.as_ref()).collect(); + if !hard { + // Soft wrapping may consume spaces after the preceding row. + while !text[offset..].starts_with(&rendered) + && text[offset..].starts_with(' ') + { + offset += 1; + } + } + assert!(text[offset..].starts_with(&rendered)); + assert!(boundaries.contains(&offset)); + offset += rendered.len(); + assert!(boundaries.contains(&offset)); + assert!( + rendered.width() <= width.max(1) + || rendered.graphemes(true).count() == 1 + ); + } + if hard { + assert_eq!(offset, text.len()); + } + } + } + } + } + + #[test] + fn table_cell_retains_alignment_and_grapheme_width() { + let style = Style::new().bold(); + let rows = wrap_line(Line::from("πŸ‘©πŸ½β€πŸ’»πŸ‡©πŸ‡ͺ").style(style).right_aligned(), 2); + assert_eq!(rows.len(), 2); + for row in rows { + assert_eq!(row.width(), 2); + assert_eq!(row.style, style); + assert_eq!(row.alignment, Some(ratatui::layout::Alignment::Right)); + } + } +} diff --git a/src/tui/text_input.rs b/src/tui/text_input.rs index 91a200f..f203cd8 100644 --- a/src/tui/text_input.rs +++ b/src/tui/text_input.rs @@ -175,24 +175,24 @@ impl TextInput { /// stranding at the right edge as an overflowing query is edited down. pub(crate) fn visible_cursor_col(&self, width: u16) -> u16 { let col = (self.textarea.screen_cursor().col as u16) - .saturating_sub(self.textarea.horizontal_scroll_offset()); + .saturating_sub(self.textarea.scroll_offset().1); col.min(width.saturating_sub(1)) } /// The content column a click `col` cells into the field lands on. /// - /// `col` arrives relative to the field's rect, but `cursor_at_screen` takes an + /// `col` arrives relative to the field's rect, but `screen_to_data` takes an /// absolute content column β€” so the horizontal scroll has to be *added*, the /// inverse of what [`Self::visible_cursor_col`] subtracts on the way out. /// Without it every click on a query wider than the field lands on the wrong /// character. fn content_col(&self, col: u16) -> usize { - col.saturating_add(self.textarea.horizontal_scroll_offset()) as usize + col.saturating_add(self.textarea.scroll_offset().1) as usize } /// Jump the caret to a click `col` cells into the field. fn jump_to_col(&mut self, col: u16) { - let cursor = self.textarea.cursor_at_screen(0, self.content_col(col)); + let cursor = self.textarea.screen_to_data(0, self.content_col(col)); self.textarea .move_cursor(CursorMove::Jump(cursor.0 as u16, cursor.1 as u16)); } @@ -211,7 +211,7 @@ impl TextInput { /// the reversed-block caret sits on a selected char rather than highlighting /// the boundary cell after the word. pub(crate) fn select_word_at(&mut self, col: u16) { - let column = self.textarea.cursor_at_screen(0, self.content_col(col)).1; + let column = self.textarea.screen_to_data(0, self.content_col(col)).1; let Some((start, end)) = word_bounds(&self.textarea.lines()[0], column) else { self.jump_to_col(col); return; @@ -232,7 +232,7 @@ impl TextInput { /// case where you cannot see what you are selecting. pub(crate) fn drag_mouse_selection(&mut self, col: u16, overshoot: i16) { let target = (self.content_col(col) as isize + overshoot as isize).max(0) as usize; - let cursor = self.textarea.cursor_at_screen(0, target); + let cursor = self.textarea.screen_to_data(0, target); self.textarea .move_cursor(CursorMove::Jump(cursor.0 as u16, cursor.1 as u16)); } @@ -571,7 +571,7 @@ mod tests { terminal .draw(|frame| input.render_in(&theme, frame, rect, true, false)) .unwrap(); - let scroll = input.horizontal_scroll_offset(); + let scroll = input.scroll_offset().1; assert!(scroll > 0, "the field scrolled"); // Clicking the leftmost cell lands on the leftmost *visible* char. @@ -583,6 +583,35 @@ mod tests { assert_eq!(input.cursor().1, 0, "one alphanumeric run, selected whole"); } + #[test] + fn deleting_an_overflowing_field_restores_its_text_and_native_cursor() { + let theme = crate::tui::theme::Theme::terminal_default(); + let mut input = TextInput::from("abcdefghijklmnop"); + let rect = Rect::new(2, 1, 6, 1); + let backend = ratatui::backend::TestBackend::new(12, 3); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + terminal + .draw(|frame| input.render_in(&theme, frame, rect, true, false)) + .unwrap(); + assert!(input.scroll_offset().1 > 0); + for _ in 0..13 { + input.input(key(KeyCode::Backspace)); + terminal + .draw(|frame| input.render_in(&theme, frame, rect, true, false)) + .unwrap(); + } + assert_eq!(input.as_str(), "abc"); + assert_eq!(input.scroll_offset().1, 0); + assert_eq!(input.visible_cursor_col(rect.width), 3); + assert_eq!(terminal.backend().buffer()[(rect.x, rect.y)].symbol(), "a"); + assert_eq!( + terminal.get_cursor_position().unwrap(), + (rect.x + 3, rect.y).into() + ); + input.begin_mouse_selection(1); + assert_eq!(input.cursor().1, 1); + } + /// Dragging a selection past the field's edge has to keep going, or the one /// thing you cannot see is the text you are selecting. #[test] @@ -602,7 +631,7 @@ mod tests { // there is somewhere to the right to drag toward. input.set_cursor_byte(0); render(&mut input); - assert_eq!(input.horizontal_scroll_offset(), 0); + assert_eq!(input.scroll_offset().1, 0); // Select from the field's left edge, then drag off its right edge. input.begin_mouse_selection(0); @@ -618,9 +647,9 @@ mod tests { // …and the render that follows scrolls to keep the caret visible, so the // next drag at the same pointer position reaches further still. - let scrolled_from = input.horizontal_scroll_offset(); + let scrolled_from = input.scroll_offset().1; render(&mut input); - assert!(input.horizontal_scroll_offset() > scrolled_from, "followed"); + assert!(input.scroll_offset().1 > scrolled_from, "followed"); input.drag_mouse_selection(rect.width - 1, 3); assert!(input.cursor().1 > at_edge + 3);