fix: shift of auto-wrap points in both code and JSON previewers (#3739)

This commit is contained in:
三咲雅 misaki masa 2026-03-05 20:16:03 +08:00 committed by GitHub
parent 9648d8a43b
commit be91b4111c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 163 additions and 85 deletions

View file

@ -1,4 +1,4 @@
use std::{borrow::Cow, path::PathBuf};
use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
@ -9,9 +9,6 @@ use yazi_shared::{SStr, timestamp_us};
use super::PreviewWrap;
use crate::normalize_path;
#[rustfmt::skip]
const TABS: &[&str] = &["", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "];
#[derive(Debug, Deserialize, DeserializeOver2, Serialize)]
pub struct Preview {
pub wrap: PreviewWrap,
@ -34,10 +31,15 @@ impl Preview {
self.cache_dir.join(format!("{prefix}-{}", timestamp_us()))
}
pub fn indent(&self) -> SStr { Self::indent_with(self.tab_size as usize) }
pub fn indent(&self) -> SStr {
#[rustfmt::skip]
const TABS: &[&str] = &["", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "];
pub fn indent_with(n: usize) -> SStr {
if let Some(s) = TABS.get(n) { Cow::Borrowed(s) } else { Cow::Owned(" ".repeat(n)) }
if let Some(&s) = TABS.get(self.tab_size as usize) {
s.into()
} else {
" ".repeat(self.tab_size as usize).into()
}
}
}

View file

@ -6,3 +6,12 @@ pub enum PreviewWrap {
No,
Yes,
}
impl From<PreviewWrap> for Option<ratatui::widgets::Wrap> {
fn from(wrap: PreviewWrap) -> Self {
match wrap {
PreviewWrap::No => None,
PreviewWrap::Yes => Some(ratatui::widgets::Wrap { trim: false }),
}
}
}