rl-hours-tracker 0.4.4

A rust binary used for calculating the hours played of Rocket League in the past two weeks.
Documentation
use std::io::{ErrorKind, Write};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{env, process, thread};

use colour::{blue, blue_ln, cyan, e_red_ln, green_ln, green_ln_bold};
use log::{error, warn};
use rl_hours_tracker::initialize_logging;
use rl_hours_tracker::winit_tray_icon::{UserEvent, initialize_tray_icon};
use rl_hours_tracker::{
    calculate_past_two::update_past_two, create_directory, run, run_self_update,
};
use winit::event_loop::EventLoop;

fn main() {
    let event_loop = EventLoop::<UserEvent>::with_user_event()
        .build()
        .unwrap_or_else(|e| {
            error!("error occurred creating event loop: {e}");
            panic!("could not create event loop for tray icon");
        });

    let proxy = event_loop.create_proxy();
    
    // Create booleans for sharing between multiple threads
    let currently_tracking = Arc::new(Mutex::new(AtomicBool::new(false)));
    let stop_tracker = Arc::new(Mutex::new(AtomicBool::new(false)));

    let main_ct = currently_tracking.clone();
    let main_st = stop_tracker.clone();

    std::thread::spawn(move || {
        blue!(
            "

   ___           __       __    __                         
  / _ \\___  ____/ /_____ / /_  / /  ___ ___ ____ ___ _____ 
 / , _/ _ \\/ __/  '_/ -_) __/ / /__/ -_) _ `/ _ `/ // / -_)
/_/|_|\\___/\\__/_/\\_\\\\__/\\__/ /____/\\__/\\_,_/\\_, /\\_,_/\\__/ 
    "
        );
        cyan!(
            "
   __ __                    ______             __          
  / // /__  __ _________   /_  __/______ _____/ /_____ ____
 / _  / _ \\/ // / __(_-<    / / / __/ _ `/ __/  '_/ -_) __/
/_//_/\\___/\\_,_/_/ /___/   /_/ /_/  \\_,_/\\__/_/\\_\\\\__/_/   
                                                           
"
        );

        std::io::stdout().flush().unwrap_or_else(|_| {
            blue_ln!(
                "
        
   ___           __       __    __                         
  / _ \\___  ____/ /_____ / /_  / /  ___ ___ ____ ___ _____ 
 / , _/ _ \\/ __/  '_/ -_) __/ / /__/ -_) _ `/ _ `/ // / -_)
/_/|_|\\___/\\__/_/\\_\\\\__/\\__/ /____/\\__/\\_,_/\\_, /\\_,_/\\__/ 

   __ __                    ______             __          
  / // /__  __ _________   /_  __/______ _____/ /_____ ____
 / _  / _ \\/ // / __(_-<    / / / __/ _ `/ __/  '_/ -_) __/
/_//_/\\___/\\_,_/_/ /___/   /_/ /_/  \\_,_/\\__/_/\\_\\\\__/_/  
        "
            )
        });

        // Initialize logging for the program
        initialize_logging().unwrap_or_else(|e| {
            e_red_ln!("an error occurred when initializing logging: {e}");
            thread::sleep(Duration::from_secs(2));
            e_red_ln!("this program will end in 3 seconds");
            thread::sleep(Duration::from_secs(3));
            process::exit(1);
        });

        // Checks if the program is being run from the AppData directory.
        // This does not run the self update if using through rust binary.
        if let Ok(path) = env::current_dir() {
            let dir = path.to_str().unwrap_or_default();

            if dir.contains("AppData") {
                run_self_update().unwrap_or_else(|e| error!("error running self update: {e}"));
            }
        }

        // Create the directories for the program
        let folders_result = create_directory();

        // Handles the successful result from the 'create_directory' function or panics if any errors occurred
        if !folders_result.is_empty() {
            for folder in folders_result {
                folder.unwrap_or_else(|e| {
                    if e.kind() != ErrorKind::AlreadyExists {
                        error!("There was an issue when creating folders: {e}");
                        process::exit(1);
                    }
                })
            }
        } else {
            green_ln!("All directories created successfully!");
        }

        // Updates the hours in the past two weeks if it returns true
        if update_past_two().unwrap_or_else(|e| {
            warn!("past two could not be updated: {e}");
            false
        }) {
            green_ln_bold!("Past Two Updated!\n");
        }

        run(proxy, stop_tracker, currently_tracking);
    });

    // Initialize the tray icon
    initialize_tray_icon(event_loop, main_st, main_ct);
}