Don't crash when no messages
This commit is contained in:
parent
cc657d75f6
commit
855df73ef0
26
src/main.rs
26
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::<Message>();
|
||||
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(
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<Utc>;
|
||||
fn get_message(&self) -> String;
|
||||
|
@ -82,7 +88,12 @@ pub fn parse_datetime(i: &str) -> IResult<&str, DateTime<Utc>> {
|
|||
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::<Message>();
|
||||
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<Item>> {
|
||||
let mut items = vec![];
|
||||
loop {
|
||||
|
|
Loading…
Reference in New Issue
Block a user