mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-14 00:44:26 +08:00
Add format support
This commit is contained in:
54
src/format/fixed.rs
Normal file
54
src/format/fixed.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
pub struct FixedFormatter {
|
||||
length: usize,
|
||||
keep_original: bool,
|
||||
}
|
||||
|
||||
impl FixedFormatter {
|
||||
pub fn new(length: usize, keep_original: bool) -> Self {
|
||||
FixedFormatter {
|
||||
length,
|
||||
keep_original,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format(&self, message: &str) -> String {
|
||||
let mut result = String::new();
|
||||
let vec: Vec<_> = UnicodeSegmentation::graphemes(message, true).collect();
|
||||
let mut current_length = 0;
|
||||
for grapheme in vec {
|
||||
if grapheme == "\n" {
|
||||
if self.keep_original {
|
||||
result.push('\n');
|
||||
current_length = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if current_length >= self.length {
|
||||
result.push('\n');
|
||||
current_length = 0;
|
||||
}
|
||||
result.push_str(grapheme);
|
||||
current_length += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
let formatter = FixedFormatter::new(10, false);
|
||||
let message = "This is a test message.\nThis is another line.";
|
||||
let formatted_message = formatter.format(message);
|
||||
assert_eq!(
|
||||
formatted_message,
|
||||
"This is a \ntest messa\nge.This is\n another l\nine."
|
||||
);
|
||||
assert_eq!(formatter.format("● This is a test."), "● This is \na test.");
|
||||
let fommater2 = FixedFormatter::new(10, true);
|
||||
assert_eq!(
|
||||
fommater2.format("● Th\nis is a test."),
|
||||
"● Th\nis is a te\nst."
|
||||
);
|
||||
}
|
||||
18
src/format/mod.rs
Normal file
18
src/format/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
mod fixed;
|
||||
|
||||
use crate::types::*;
|
||||
|
||||
pub fn fmt_message(mes: &mut Vec<Message>, opt: FormatOptions) {
|
||||
match opt {
|
||||
FormatOptions::Fixed {
|
||||
length,
|
||||
keep_original,
|
||||
} => {
|
||||
let formatter = fixed::FixedFormatter::new(length, keep_original);
|
||||
for message in mes.iter_mut() {
|
||||
message.message = formatter.format(&message.message);
|
||||
}
|
||||
}
|
||||
FormatOptions::None => {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user