updates every 250 milliseconds

This commit is contained in:
Cadey Ratio 2020-04-07 08:03:07 -04:00
parent 6a58a1daac
commit 62829525d4
1 changed files with 43 additions and 16 deletions

View File

@ -1,12 +1,14 @@
mod xsetroot; mod xsetroot;
use chrono::prelude::*; use chrono::prelude::*;
use std::fmt; use std::{
use std::io::prelude::*; fmt,
use std::io::BufReader; io::{prelude::*, BufReader},
use std::sync::{Arc, Mutex}; net::Shutdown,
use std::thread; os::unix::net::{UnixListener, UnixStream},
use std::os::unix::net::{UnixStream, UnixListener}; sync::{Arc, Mutex},
thread, time,
};
use xsetroot::XSetRoot; use xsetroot::XSetRoot;
type MTState = Arc<Mutex<State>>; type MTState = Arc<Mutex<State>>;
@ -18,25 +20,46 @@ fn handle_client(stream: UnixStream, st: MTState) {
let _ = write!(&stream, "OK"); let _ = write!(&stream, "OK");
let mut data = st.lock().unwrap(); let mut data = st.lock().unwrap();
data.msg = msg; data.msg = msg.trim().to_string();
data.show(); stream.shutdown(Shutdown::Both).expect("socket to close");
} }
fn main() -> std::io::Result::<()> { const UPDATE_FREQUENCY: u64 = 250;
let mut st: State = State::init();
st.msg = "coi rodo .ui.".to_string(); fn update_every_so_often(st: MTState) {
loop {
let so_often = time::Duration::from_millis(UPDATE_FREQUENCY);
thread::sleep(so_often);
{
let data = st.lock().unwrap();
data.show();
}
}
}
fn main() -> std::io::Result<()> {
let st: State = State::init();
st.show(); st.show();
let mtst = Arc::new(Mutex::new(st)); let mtst = Arc::new(Mutex::new(st));
// start update thread
{
let mtst = mtst.clone();
thread::spawn(move || update_every_so_often(mtst));
}
let _ = std::fs::remove_file("/home/cadey/tmp/cabytcini.sock")?;
let listener = UnixListener::bind("/home/cadey/tmp/cabytcini.sock")?; let listener = UnixListener::bind("/home/cadey/tmp/cabytcini.sock")?;
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(stream) => { Ok(stream) => {
thread::spawn(|| handle_client(stream, mtst.clone())); let mtst = mtst.clone();
}, thread::spawn(move || handle_client(stream, mtst));
}
Err(err) => { Err(err) => {
break; break;
}, }
} }
} }
@ -61,8 +84,12 @@ impl State {
} }
fn show(&self) { fn show(&self) {
let now = Local::now(); let now = Local::now().format("%H:%M M%m %-d %a");
let xsr = XSetRoot::init(); let xsr = XSetRoot::init();
xsr.render(format!("{} | {}", self, now.format("%H:%M M%m %-d %a"))); if self.msg == "" {
xsr.render(format!("{}", now))
} else {
xsr.render(format!("{} | {}", self.msg, now));
}
} }
} }