Remove async-std and squash some bugs
This commit is contained in:
@@ -58,7 +58,7 @@ impl Config {
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn get_config_directory() -> PathBuf {
|
||||
pub fn get_config_directory() -> PathBuf {
|
||||
ProjectDirs::from("moe", "paoda", "domasi")
|
||||
.unwrap()
|
||||
.config_dir()
|
||||
|
||||
93
src/main.rs
93
src/main.rs
@@ -1,17 +1,14 @@
|
||||
use async_std::task;
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use crossbeam::channel::{unbounded, Receiver, Sender};
|
||||
use crossterm::{
|
||||
cursor,
|
||||
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
|
||||
terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType},
|
||||
QueueableCommand,
|
||||
terminal::{disable_raw_mode, enable_raw_mode},
|
||||
};
|
||||
use domasi::pomodoro::{Alert, Clock, Status};
|
||||
use domasi::{Config, Pomodoro};
|
||||
use std::fs;
|
||||
use std::io::{stdout, Write};
|
||||
use std::io::{self, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
@@ -54,7 +51,7 @@ fn main() {
|
||||
}
|
||||
|
||||
pub fn create_config() {
|
||||
let config_dir = Config::get_data_directory();
|
||||
let config_dir = Config::get_config_directory();
|
||||
let data_dir = Config::get_data_directory().join("alert");
|
||||
|
||||
if !config_dir.exists() {
|
||||
@@ -88,17 +85,20 @@ pub fn create_config() {
|
||||
|
||||
pub fn start(args: &ArgMatches) {
|
||||
let mut pomodoro = Pomodoro::new();
|
||||
|
||||
let (tx, rx): (Sender<Status>, Receiver<Status>) = channel();
|
||||
let (tx, rx): (Sender<Status>, Receiver<Status>) = unbounded();
|
||||
|
||||
// UI Thread
|
||||
thread::spawn(move || loop {
|
||||
if let Ok(status) = rx.recv() {
|
||||
loop {
|
||||
let (remain, text) = Clock::get_formatted_string(status);
|
||||
print_overwrite(&text);
|
||||
|
||||
// Make this check better pls
|
||||
let out = io::stdout();
|
||||
let mut handle = out.lock();
|
||||
handle.write_all(text.as_bytes()).unwrap();
|
||||
handle.flush().unwrap();
|
||||
|
||||
// TODO: Make sure this isn't an issue.
|
||||
if remain < 1 {
|
||||
break;
|
||||
}
|
||||
@@ -111,48 +111,45 @@ pub fn start(args: &ArgMatches) {
|
||||
// User Input Thread
|
||||
thread::spawn(|| setup_user_input().unwrap());
|
||||
|
||||
// Async Pomodoro
|
||||
task::block_on(async {
|
||||
let config = {
|
||||
match Config::load() {
|
||||
Some(cfg) => cfg,
|
||||
None => Default::default(),
|
||||
}
|
||||
};
|
||||
let config = {
|
||||
match Config::load() {
|
||||
Some(cfg) => cfg,
|
||||
None => Default::default(),
|
||||
}
|
||||
};
|
||||
|
||||
let maybe_audio: Option<PathBuf>;
|
||||
let maybe_audio: Option<PathBuf>;
|
||||
|
||||
match args.value_of("alert") {
|
||||
Some(path) => maybe_audio = Some(Path::new(path).to_path_buf()),
|
||||
None => {
|
||||
match &config.sound_file {
|
||||
Some(path) => maybe_audio = Some(path.clone()),
|
||||
None => {
|
||||
// Look in the default locations
|
||||
// check for .mp3, .wav, .ogg, .flac
|
||||
let data_dir = Config::get_data_directory().join("alert");
|
||||
let data_dir_str = data_dir.to_string_lossy();
|
||||
match args.value_of("alert") {
|
||||
Some(path) => maybe_audio = Some(Path::new(path).to_path_buf()),
|
||||
None => {
|
||||
match &config.sound_file {
|
||||
Some(path) => maybe_audio = Some(path.clone()),
|
||||
None => {
|
||||
// Look in the default locations
|
||||
// check for .mp3, .wav, .ogg, .flac
|
||||
let data_dir = Config::get_data_directory().join("alert");
|
||||
let data_dir_str = data_dir.to_string_lossy();
|
||||
|
||||
let items = fs::read_dir(&data_dir).unwrap_or_else(|_err| {
|
||||
panic!("Unable to read the contents of {}", data_dir_str);
|
||||
});
|
||||
let items = fs::read_dir(&data_dir).unwrap_or_else(|_err| {
|
||||
panic!("Unable to read the contents of {}", data_dir_str);
|
||||
});
|
||||
|
||||
maybe_audio = get_audio_file(items);
|
||||
}
|
||||
maybe_audio = get_audio_file(items);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match maybe_audio {
|
||||
Some(audio_path) => {
|
||||
let default_device = rodio::default_output_device().unwrap();
|
||||
let alert = Alert::new(&audio_path, &default_device);
|
||||
match maybe_audio {
|
||||
Some(audio_path) => {
|
||||
let default_device = rodio::default_output_device().unwrap();
|
||||
let alert = Alert::new(&audio_path, &default_device);
|
||||
|
||||
pomodoro.start(config, tx, Some(&alert)).await;
|
||||
}
|
||||
None => pomodoro.start(config, tx, None).await,
|
||||
pomodoro.start(config, tx, Some(&alert));
|
||||
}
|
||||
});
|
||||
None => pomodoro.start(config, tx, None),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_audio_file(items: std::fs::ReadDir) -> Option<PathBuf> {
|
||||
@@ -185,16 +182,6 @@ fn setup_user_input() -> crossterm::Result<()> {
|
||||
disable_raw_mode()
|
||||
}
|
||||
|
||||
fn print_overwrite(text: &str) {
|
||||
let mut stdout = stdout();
|
||||
|
||||
stdout.queue(Clear(ClearType::CurrentLine)).unwrap();
|
||||
stdout.queue(cursor::SavePosition).unwrap();
|
||||
stdout.write_all(text.as_bytes()).unwrap();
|
||||
stdout.queue(cursor::RestorePosition).unwrap();
|
||||
stdout.flush().unwrap();
|
||||
}
|
||||
|
||||
fn get_user_input() -> crossterm::Result<()> {
|
||||
loop {
|
||||
if poll(Clock::get_polling_interval())? {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use super::Config;
|
||||
use crossbeam::channel::Sender;
|
||||
use rodio::{Decoder, Device, Source};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
// #[derive()]
|
||||
pub struct Alert<'a> {
|
||||
path: &'a Path,
|
||||
device: &'a Device,
|
||||
@@ -68,12 +67,7 @@ impl Default for Pomodoro {
|
||||
}
|
||||
|
||||
impl<'a> Pomodoro {
|
||||
pub async fn start(
|
||||
&mut self,
|
||||
config: Config,
|
||||
tx: Sender<Status>,
|
||||
maybe_alert: Option<&Alert<'a>>,
|
||||
) {
|
||||
pub fn start(&mut self, config: Config, tx: Sender<Status>, maybe_alert: Option<&Alert<'a>>) {
|
||||
loop {
|
||||
self.next();
|
||||
|
||||
@@ -86,15 +80,15 @@ impl<'a> Pomodoro {
|
||||
match self.state {
|
||||
State::Work => {
|
||||
Self::send_to_clock(&tx, self.state, config.work_time);
|
||||
Self::wait(config.work_time).await;
|
||||
Self::wait(config.work_time);
|
||||
}
|
||||
State::ShortBreak => {
|
||||
Self::send_to_clock(&tx, self.state, config.short_break);
|
||||
Self::wait(config.short_break).await;
|
||||
Self::wait(config.short_break);
|
||||
}
|
||||
State::LongBreak => {
|
||||
Self::send_to_clock(&tx, self.state, config.long_break);
|
||||
Self::wait(config.long_break).await;
|
||||
Self::wait(config.long_break);
|
||||
}
|
||||
State::Inactive => {
|
||||
println!("Pomodoro Cycle is complete");
|
||||
@@ -130,8 +124,8 @@ impl Pomodoro {
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait(duration: Duration) {
|
||||
async_std::task::sleep(duration).await;
|
||||
fn wait(duration: Duration) {
|
||||
std::thread::sleep(duration);
|
||||
}
|
||||
|
||||
fn send_to_clock(tx: &Sender<Status>, state: State, length: Duration) {
|
||||
@@ -196,7 +190,7 @@ impl Clock {
|
||||
let minutes = (seconds - (hours * 3600)) / 60;
|
||||
let seconds = seconds - (hours * 3600) - (minutes * 60);
|
||||
|
||||
let mut clock = String::new();
|
||||
let mut clock = "\r".to_string();
|
||||
|
||||
clock.push_str(&format!("{}", status.state));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user