From 28b395cfd770785641c49f98cc9491210271db46 Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 25 Oct 2015 10:12:35 -0400 Subject: Use notify-rust for notifications --- src/main.rs | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ddf4feb..d904f6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ -use std::process::Command; use std::fs::File; use std::io::prelude::*; +extern crate notify_rust; extern crate toml; extern crate websocket; extern crate rustc_serialize; -use rustc_serialize::{Decodable}; -use rustc_serialize::json::{Json, Decoder}; +use notify_rust::Notification; mod message; use message::{Message, Push}; @@ -16,11 +15,18 @@ fn main() { use websocket::client::request::Url; use websocket::Client; - let mut cfg_file = File::open("config.toml").unwrap(); + let mut cfg_file = File::open("config.toml").expect("Could not find config.toml."); let mut s = String::new(); - cfg_file.read_to_string(&mut s); + cfg_file.read_to_string(&mut s).expect("Could not read config.toml"); let cfg = toml::Parser::new(s.as_ref()).parse().unwrap(); - let token = cfg.get("pushbullet").unwrap().as_table().unwrap().get("token").expect("Could not find 'token' in config.toml").as_str().unwrap(); + let cfg_pb = cfg.get("pushbullet") + .expect("Could not find [pushbullet] section in config.") + .as_table() + .expect("Config should contain a [pushbullet] section."); + let token = cfg_pb.get("token") + .expect("[pushbullet] section should contain 'token'") + .as_str() + .expect("'token' should be a string"); let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token); let url = Url::parse(wss_url.as_ref()).unwrap(); @@ -44,28 +50,23 @@ fn main() { return; } websocket::Message::Text(message) => { - let json = Json::from_str(message.as_ref()).unwrap(); - let mut decoder = Decoder::new(json); - let msg = Decodable::decode(&mut decoder).unwrap(); - println!("Got {:?}", msg); - match msg { - Message::Nop => {} - Message::Push(Push::Mirror { - notification_id, - title, - body, - application_name, - .. - }) => { - let _ = Command::new("notify-send") - .arg(title) - .arg(body) - .arg("-t") - .arg("8000") - .status().unwrap(); - } - Message::Push(Push::Dismissal { notification_id }) => { - + let msg = Message::parse(message.as_ref()); + if let Some(msg) = msg { + match msg { + Message::Push(Push::Mirror { + title, + body, + application_name, + .. + }) => { + let title = format!("{}: {}", application_name, title); + Notification::new() + .body(body.as_ref()) + .summary(title.as_ref()) + .show() + .unwrap(); + } + _ => {} } } } -- cgit v1.2.3