From 855df73ef07c6dccb66b156c9d5eacf0400c6c17 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Mon, 3 Jun 2024 16:42:42 +0200 Subject: [PATCH] Don't crash when no messages --- src/main.rs | 26 +------------------------- src/poezio_logs.rs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7fd8c27..7b5812d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,30 +30,6 @@ fn get_own_nick() -> String { std::env::var("USER").unwrap() } -fn load_logs(jid: &str) -> gio::ListStore { - let logs = { - let mut file = std::fs::File::open(jid).unwrap(); - let mut string = String::new(); - use std::io::Read; - file.read_to_string(&mut string).unwrap(); - string - }; - let (_, logs) = poezio_logs::parse_logs(&logs).unwrap(); - let store = gio::ListStore::new::(); - for item in logs { - if let poezio_logs::Item::Message(message) = item { - use poezio_logs::LogItem; - let message = Message::new( - message.get_time(), - message.get_nick(), - &message.get_message(), - ); - store.append(&message); - } - } - store -} - fn xep_0392(input: &str) -> String { use sha1::Digest; let sha1 = sha1::Sha1::digest(input); @@ -157,7 +133,7 @@ fn main() { let item = tabs_selection.selected_item().unwrap(); let tab: &Tab = item.downcast_ref().unwrap(); println!("Switching to {}", tab.jid()); - let store = load_logs(&tab.jid()); + let store = poezio_logs::load_logs(&tab.jid()); let selection = win2.selection(); selection.set_model(Some(&store)); win2.messages().scroll_to( diff --git a/src/poezio_logs.rs b/src/poezio_logs.rs index f7e8269..5ab7d8d 100644 --- a/src/poezio_logs.rs +++ b/src/poezio_logs.rs @@ -14,6 +14,7 @@ // along with this program. If not, see . use chrono::{DateTime, TimeZone, Utc}; +use gtk::gio; use nom; use nom::{ bytes::complete::{tag, take, take_until}, @@ -22,8 +23,13 @@ use nom::{ sequence::tuple, IResult, }; + +use std::fs::read_to_string; +use std::path::PathBuf; use std::str::FromStr; +use crate::Message; + pub trait LogItem { fn get_time(&self) -> &DateTime; fn get_message(&self) -> String; @@ -82,7 +88,12 @@ pub fn parse_datetime(i: &str) -> IResult<&str, DateTime> { tag("Z"), ))(i)?; // TODO: handle DST and other time transitions - Ok((i, Utc.with_ymd_and_hms(year, month, day, hour, minute, second).single().unwrap())) + Ok(( + i, + Utc.with_ymd_and_hms(year, month, day, hour, minute, second) + .single() + .unwrap(), + )) } pub fn parse_log_info(i: &str) -> IResult<&str, LogInfo> { @@ -143,6 +154,30 @@ pub enum Item<'a> { Info(LogInfo<'a>), } +pub fn load_logs(jid: &str) -> gio::ListStore { + let xdg_data = std::env::var("XDG_DATA_HOME").unwrap_or(format!( + "{}/.local/share", + std::env::var("HOME").expect("NO $HOME?!") + )); + let xdg_data = PathBuf::from(xdg_data); + let entry = xdg_data.join("poezio/logs").join(jid); + + let logs = read_to_string(entry).unwrap_or("".to_string()); + let (_, logs) = parse_logs(&logs).unwrap(); + let store = gio::ListStore::new::(); + for item in logs { + if let Item::Message(message) = item { + let message = Message::new( + message.get_time(), + message.get_nick(), + &message.get_message(), + ); + store.append(&message); + } + } + store +} + pub fn parse_logs(mut logs: &str) -> IResult<&str, Vec> { let mut items = vec![]; loop {