From 6c0204e6965bf13c3d883ea5fa65d52415acd530 Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 22 Feb 2024 11:39:22 +0800 Subject: refactor: change header of messages saved to markdown (#317) --- src/config/input.rs | 20 ++++++++++++++++++++ src/config/mod.rs | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/config/input.rs b/src/config/input.rs index c3e0de3..6fa1b1c 100644 --- a/src/config/input.rs +++ b/src/config/input.rs @@ -12,6 +12,7 @@ use std::{ io::Read, path::{Path, PathBuf}, }; +use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; const IMAGE_EXTS: [&str; 5] = ["png", "jpeg", "jpg", "webp", "gif"]; @@ -78,6 +79,25 @@ impl Input { self.data_urls.clone() } + pub fn summary(&self) -> String { + let text = &self.text; + if text.width_cjk() > 70 { + let mut sum_width = 0; + let mut chars = vec![]; + for c in text.chars() { + sum_width += c.width_cjk().unwrap_or(1); + if sum_width > 67 { + chars.extend(['.', '.', '.']); + break; + } + chars.push(c); + } + chars.into_iter().collect() + } else { + text.clone() + } + } + pub fn render(&self) -> String { if self.medias.is_empty() { return self.text.clone(); diff --git a/src/config/mod.rs b/src/config/mod.rs index 71e47da..df74716 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -230,14 +230,15 @@ impl Config { return Ok(()); } let timestamp = now(); + let summary = input.summary(); let input_markdown = input.render(); let output = match self.role.as_ref() { None => { - format!("# CHAT:[{timestamp}]\n{input_markdown}\n--------\n{output}\n--------\n\n",) + format!("# CHAT: {summary} [{timestamp}]\n{input_markdown}\n--------\n{output}\n--------\n\n",) } Some(v) => { format!( - "# CHAT:[{timestamp}] ({})\n{input_markdown}\n--------\n{output}\n--------\n\n", + "# CHAT: {summary} [{timestamp}] ({})\n{input_markdown}\n--------\n{output}\n--------\n\n", v.name, ) } -- cgit v1.2.3