diff --git a/src/args.rs b/src/args.rs index d83d9ec..b5c0b49 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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, diff --git a/src/format/fixed.rs b/src/format/fixed.rs index ac487a7..6dc8344 100644 --- a/src/format/fixed.rs +++ b/src/format/fixed.rs @@ -105,6 +105,8 @@ pub struct FixedFormatter { jieba: Option, #[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, } @@ -118,6 +120,7 @@ impl FixedFormatter { break_with_sentence: bool, #[cfg(feature = "jieba")] break_chinese_words: bool, #[cfg(feature = "jieba")] jieba_dict: Option, + no_remove_space_at_line_start: bool, typ: Option, ) -> Result { #[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) -> 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); diff --git a/src/format/mod.rs b/src/format/mod.rs index 74cd5ac..ce80cfa 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -17,6 +17,7 @@ pub fn fmt_message(mes: &mut Vec, 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, 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() { diff --git a/src/main.rs b/src/main.rs index ab48699..16af5a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, }, diff --git a/src/scripts/bgi/script.rs b/src/scripts/bgi/script.rs index 387a063..2e21138 100644 --- a/src/scripts/bgi/script.rs +++ b/src/scripts/bgi/script.rs @@ -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, } } } diff --git a/src/scripts/circus/script.rs b/src/scripts/circus/script.rs index f6af5e9..7b0a0ed 100644 --- a/src/scripts/circus/script.rs +++ b/src/scripts/circus/script.rs @@ -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, } } diff --git a/src/types.rs b/src/types.rs index d6fa202..e56233b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -878,6 +878,8 @@ pub enum FormatOptions { #[cfg(feature = "jieba")] /// Path to custom jieba dictionary jieba_dict: Option, + /// Do not remove space at the start of the line + no_remove_space_at_line_start: bool, }, /// Do not wrap line None,