Allow fixed formatter to keep space at line start

This commit is contained in:
2026-01-24 12:07:57 +08:00
parent c6b5bcef60
commit b8e004bff6
7 changed files with 39 additions and 1 deletions

View File

@@ -695,6 +695,9 @@ pub struct ImportArgs {
#[arg(long, action = ArgAction::SetTrue)]
/// Whether to disable break Chinese words at the end of the line.
pub patched_no_break_chinese_words: bool,
#[arg(long, action = ArgAction::SetTrue)]
/// Do not remove space at the start of the line
pub patched_no_remove_space_at_line_start: bool,
#[arg(long)]
/// Name table file
pub name_csv: Option<String>,

View File

@@ -105,6 +105,8 @@ pub struct FixedFormatter {
jieba: Option<Jieba>,
#[cfg(not(feature = "jieba"))]
jieba: Option<()>,
/// Do not remove space at the start of the line
no_remove_space_at_line_start: bool,
#[allow(unused)]
typ: Option<ScriptType>,
}
@@ -118,6 +120,7 @@ impl FixedFormatter {
break_with_sentence: bool,
#[cfg(feature = "jieba")] break_chinese_words: bool,
#[cfg(feature = "jieba")] jieba_dict: Option<String>,
no_remove_space_at_line_start: bool,
typ: Option<ScriptType>,
) -> Result<Self> {
#[cfg(feature = "jieba")]
@@ -142,6 +145,7 @@ impl FixedFormatter {
jieba,
#[cfg(not(feature = "jieba"))]
jieba: None,
no_remove_space_at_line_start,
typ,
})
}
@@ -156,6 +160,7 @@ impl FixedFormatter {
break_with_sentence: false,
jieba: None,
typ: None,
no_remove_space_at_line_start: false,
}
}
@@ -202,6 +207,12 @@ impl FixedFormatter {
self
}
#[cfg(test)]
fn no_remove_space_at_line_start(mut self, no_remove: bool) -> Self {
self.no_remove_space_at_line_start = no_remove;
self
}
#[cfg(test)]
#[allow(dead_code)]
fn typ(mut self, typ: Option<ScriptType>) -> Self {
@@ -566,7 +577,10 @@ impl FixedFormatter {
}
}
if (current_length == 0 || pre_is_lf) && SPACE_STR_LIST.contains(&grapheme) {
if !self.no_remove_space_at_line_start
&& (current_length == 0 || pre_is_lf)
&& SPACE_STR_LIST.contains(&grapheme)
{
i += 1;
continue;
}
@@ -729,6 +743,15 @@ fn test_format() {
"(This) 「is\n\u{3000}a test."
);
let formatter3b = FixedFormatter::builder(10)
.break_words(false)
.no_remove_space_at_line_start(true);
assert_eq!(
formatter3b.format("(This) 「is a test."),
"(This) 「is\n a test."
);
let formatter4 = FixedFormatter::builder(10)
.break_words(false)
.break_with_sentence(true);

View File

@@ -17,6 +17,7 @@ pub fn fmt_message(mes: &mut Vec<Message>, opt: FormatOptions, typ: ScriptType)
break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict,
no_remove_space_at_line_start,
} => {
let formatter = fixed::FixedFormatter::new(
length,
@@ -28,6 +29,7 @@ pub fn fmt_message(mes: &mut Vec<Message>, opt: FormatOptions, typ: ScriptType)
break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict,
no_remove_space_at_line_start,
Some(typ),
)?;
for message in mes.iter_mut() {

View File

@@ -1580,6 +1580,8 @@ pub fn import_script(
break_chinese_words: !imp_cfg.patched_no_break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict: arg.jieba_dict.clone(),
no_remove_space_at_line_start: imp_cfg
.patched_no_remove_space_at_line_start,
},
types::FormatType::None => types::FormatOptions::None,
},
@@ -2051,6 +2053,8 @@ pub fn import_script(
break_chinese_words: !imp_cfg.patched_no_break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict: arg.jieba_dict.clone(),
no_remove_space_at_line_start: imp_cfg
.patched_no_remove_space_at_line_start,
},
types::FormatType::None => types::FormatOptions::None,
},
@@ -2237,6 +2241,7 @@ pub fn import_script(
break_chinese_words: !imp_cfg.patched_no_break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict: arg.jieba_dict.clone(),
no_remove_space_at_line_start: imp_cfg.patched_no_remove_space_at_line_start,
},
types::FormatType::None => types::FormatOptions::None,
},
@@ -2433,6 +2438,7 @@ pub fn import_script(
break_chinese_words: !imp_cfg.patched_no_break_chinese_words,
#[cfg(feature = "jieba")]
jieba_dict: arg.jieba_dict.clone(),
no_remove_space_at_line_start: imp_cfg.patched_no_remove_space_at_line_start,
},
types::FormatType::None => types::FormatOptions::None,
},

View File

@@ -188,6 +188,7 @@ impl Script for BGIScript {
break_chinese_words: true,
#[cfg(feature = "jieba")]
jieba_dict: None,
no_remove_space_at_line_start: false,
}
}
}

View File

@@ -224,6 +224,7 @@ impl Script for CircusMesScript {
break_chinese_words: true,
#[cfg(feature = "jieba")]
jieba_dict: None,
no_remove_space_at_line_start: false,
}
}

View File

@@ -878,6 +878,8 @@ pub enum FormatOptions {
#[cfg(feature = "jieba")]
/// Path to custom jieba dictionary
jieba_dict: Option<String>,
/// Do not remove space at the start of the line
no_remove_space_at_line_start: bool,
},
/// Do not wrap line
None,