fmt + 改进 msg display

This commit is contained in:
shenjack 2025-02-11 23:36:27 +08:00
parent 76a3628d2d
commit ede6640aa9
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 60 additions and 43 deletions

View File

@ -88,12 +88,25 @@ impl<'de> Deserialize<'de> for Message {
impl Display for Message { impl Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !self.content.is_empty() { if !self.content.is_empty() && !self.content.trim().is_empty() {
write!(f, "{}|{}|{}|{}", self.msg_id(), self.sender_id, self.sender_name, self.content) write!(f, "{}|{}|{}|{}", self.msg_id(), self.sender_id, self.sender_name, self.content)
} else if !self.files.is_empty() { } else if !self.files.is_empty() {
write!(f, "{}|{}|{}|{:?}", self.msg_id(), self.sender_id, self.sender_name, self.files[0].name) write!(
f,
"{}|{}|{}|{:?}",
self.msg_id(),
self.sender_id,
self.sender_name,
self.files[0].name
)
} else { } else {
write!(f, "{}|{}|{}|empty content & empty files", self.msg_id(), self.sender_id, self.sender_name) write!(
f,
"{}|{}|{}|empty content & empty files",
self.msg_id(),
self.sender_id,
self.sender_name
)
} }
} }
} }
@ -120,14 +133,32 @@ impl MessageTrait for NewMessage {
impl Display for NewMessage { impl Display for NewMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( if !self.msg.content.trim().is_empty() {
f, write!(
"{}|{}|{}|{}|{}", f,
self.msg_id(), "{}|{}|{}|{}|{}",
self.room_id, self.msg.msg_id,
self.msg.sender_id, self.room_id,
self.msg.sender_name, self.msg.sender_id,
self.msg.content self.msg.sender_name,
) self.msg.content
)
} else if !self.msg.files.is_empty() {
write!(
f,
"{}|{}|{}|{}|{:?}",
self.msg.msg_id,
self.room_id,
self.msg.sender_id,
self.msg.sender_name,
self.msg.files[0].name
)
} else {
write!(
f,
"{}|{}|{}|{}|empty content & empty files",
self.msg.msg_id, self.room_id, self.msg.sender_id, self.msg.sender_name
)
}
} }
} }

View File

@ -54,9 +54,7 @@ pub fn help_msg() -> String {
static STARTUP_TIME: OnceLock<SystemTime> = OnceLock::new(); static STARTUP_TIME: OnceLock<SystemTime> = OnceLock::new();
pub fn start_up_time() -> SystemTime { pub fn start_up_time() -> SystemTime { *STARTUP_TIME.get().expect("WTF, why did you panic?") }
*STARTUP_TIME.get().expect("WTF, why did you panic?")
}
/// 获得当前客户端的 id /// 获得当前客户端的 id
/// 防止串号 /// 防止串号
@ -149,7 +147,7 @@ fn main() -> anyhow::Result<()> {
.build() .build()
.unwrap() .unwrap()
.block_on(inner_main()); .block_on(inner_main());
event!(Level::INFO, "shenbot-rs v{} exiting", VERSION); event!(Level::INFO, "shenbot-rs v{} exiting", VERSION);
Ok(()) Ok(())
} }

View File

@ -55,9 +55,7 @@ impl PyTasks {
self.ica_new_message.len() + self.ica_delete_message.len() + self.tailchat_new_message.len() self.ica_new_message.len() + self.ica_delete_message.len() + self.tailchat_new_message.len()
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool { self.len() == 0 }
self.len() == 0
}
pub fn cancel_all(&mut self) { pub fn cancel_all(&mut self) {
for handle in self.ica_new_message.drain(..) { for handle in self.ica_new_message.drain(..) {
@ -72,11 +70,13 @@ impl PyTasks {
} }
} }
pub static PY_TASKS: LazyLock<Mutex<PyTasks>> = LazyLock::new(|| Mutex::new(PyTasks { pub static PY_TASKS: LazyLock<Mutex<PyTasks>> = LazyLock::new(|| {
ica_new_message: Vec::new(), Mutex::new(PyTasks {
ica_delete_message: Vec::new(), ica_new_message: Vec::new(),
tailchat_new_message: Vec::new(), ica_delete_message: Vec::new(),
})); tailchat_new_message: Vec::new(),
})
});
pub fn get_func<'py>( pub fn get_func<'py>(
py_module: &Bound<'py, PyAny>, py_module: &Bound<'py, PyAny>,

View File

@ -39,18 +39,12 @@ impl PyStatus {
let _ = unsafe { PyPluginStatus.get_or_init(|| status) }; let _ = unsafe { PyPluginStatus.get_or_init(|| status) };
} }
pub fn get() -> &'static PyStatus { pub fn get() -> &'static PyStatus { unsafe { PyPluginStatus.get().unwrap() } }
unsafe { PyPluginStatus.get().unwrap() }
}
pub fn get_mut() -> &'static mut PyStatus { pub fn get_mut() -> &'static mut PyStatus { unsafe { PyPluginStatus.get_mut().unwrap() } }
unsafe { PyPluginStatus.get_mut().unwrap() }
}
/// 添加一个插件 /// 添加一个插件
pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) { pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) { self.files.insert(path, plugin); }
self.files.insert(path, plugin);
}
/// 重新加载一个插件 /// 重新加载一个插件
pub fn reload_plugin(&mut self, plugin_name: &str) -> bool { pub fn reload_plugin(&mut self, plugin_name: &str) -> bool {
@ -70,9 +64,7 @@ impl PyStatus {
} }
/// 删除一个插件 /// 删除一个插件
pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> { pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> { self.files.remove(path) }
self.files.remove(path)
}
pub fn get_status(&self, pluging_id: &str) -> Option<bool> { pub fn get_status(&self, pluging_id: &str) -> Option<bool> {
self.files.iter().find_map(|(_, plugin)| { self.files.iter().find_map(|(_, plugin)| {
@ -195,9 +187,7 @@ impl PyPlugin {
} }
} }
pub fn get_id(&self) -> String { pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) }
plugin_path_as_id(&self.file_path)
}
} }
impl Display for PyPlugin { impl Display for PyPlugin {
@ -398,9 +388,7 @@ pub fn load_py_plugins(path: &PathBuf) {
); );
} }
pub fn get_change_time(path: &Path) -> Option<SystemTime> { pub fn get_change_time(path: &Path) -> Option<SystemTime> { path.metadata().ok()?.modified().ok() }
path.metadata().ok()?.modified().ok()
}
pub fn py_module_from_code(content: &str, path: &Path) -> PyResult<Py<PyAny>> { pub fn py_module_from_code(content: &str, path: &Path) -> PyResult<Py<PyAny>> {
Python::with_gil(|py| -> PyResult<Py<PyAny>> { Python::with_gil(|py| -> PyResult<Py<PyAny>> {
@ -525,7 +513,7 @@ pub async fn post_py() -> anyhow::Result<()> {
async fn stop_tasks() { async fn stop_tasks() {
if call::PY_TASKS.lock().await.is_empty() { if call::PY_TASKS.lock().await.is_empty() {
return ; return;
} }
let waiter = tokio::spawn(async { let waiter = tokio::spawn(async {
call::PY_TASKS.lock().await.join_all().await; call::PY_TASKS.lock().await.join_all().await;