From 8671a0b62a06abf58c527f83b3ed6e9d9153b259 Mon Sep 17 00:00:00 2001 From: Nikon <47792796+INikonI@users.noreply.github.com> Date: Fri, 17 Jun 2022 13:56:17 +0000 Subject: [PATCH] Some changes --- src/main.rs | 68 +++++++++++++++---------------------------------- src/printing.rs | 49 +++++++++++++++++++++++------------ src/util.rs | 7 +++-- 3 files changed, 57 insertions(+), 67 deletions(-) diff --git a/src/main.rs b/src/main.rs index fee6e6c..897be01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,9 @@ mod printing; mod util; use crossterm::{ - cursor::{self, Hide}, event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers}, - execute, - style::{Color, Print, Stylize}, - terminal::{enable_raw_mode, SetSize, SetTitle}, + style::{Color, Stylize}, + terminal::enable_raw_mode, }; use discord_rich_presence::{DiscordIpc, DiscordIpcClient}; use model::{ @@ -16,10 +14,10 @@ use model::{ preset::Preset, }; use printing::{ - print_activity_status, print_binds_custom_presence, print_client_status, print_error, print_info, print_version, + print_activity_status, print_binds_custom_presence, print_client_status, print_error, + print_info, print_header, print_new_version_notify, }; use std::{ - io::stdout, thread::sleep, time::{Duration, SystemTime, UNIX_EPOCH}, }; @@ -27,28 +25,8 @@ use util::parse_yaml_file; fn main() { enable_raw_mode().unwrap(); - _ = execute!( - stdout(), - SetTitle("ndrpc"), - SetSize(80, 17), - Hide, - Print( - r" _ - | | - _ __ __| | _ __ _ __ ___ -| '_ \ / _` || '__|| '_ \ / __| -| | | || (_| || | | |_) || (__ -|_| |_| \__,_||_| | .__/ \___| - | | - |_|" - .with(Color::Blue) - .bold() - ), - cursor::MoveTo(37, 5), - Print("Copyright (c) 2022 INikonI".with(Color::Blue)) - ); - - print_version(); + print_header(); + print_new_version_notify(); let config: Config = match parse_yaml_file("config.yaml") { Ok(config) => config, @@ -63,7 +41,6 @@ fn main() { DiscordIpcClient::new(&config.app_id.to_string()).expect("Failed to create client"); print_client_status("Connecting...".with(Color::Yellow)); while drpc.connect().is_err() { - print_client_status("Failed to connect. Trying to reconnect...".with(Color::Yellow)); sleep(Duration::from_millis(100)); } print_client_status("Connected".with(Color::Green)); @@ -145,22 +122,19 @@ fn main() { return; } }; - let mut presets: Vec = { - let mut presets = Vec::new(); - for preset_name in preset_names { - let preset: Preset = - match parse_yaml_file(&format!("./presets/{}.yaml", preset_name)) { - Ok(preset) => preset, - Err(err) => { - print_error(&err.to_string()); - read().unwrap(); - return; - } - }; - presets.push(preset); - } - presets - }; + let mut presets: Vec = Vec::new(); + for preset_name in preset_names { + let preset: Preset = + match parse_yaml_file(&format!("./presets/{}.yaml", preset_name)) { + Ok(preset) => preset, + Err(err) => { + print_error(&err.to_string()); + read().unwrap(); + return; + } + }; + presets.push(preset); + } let mut update_fails: u8 = 0; let mut cycle = presets.iter().cycle(); @@ -171,11 +145,11 @@ fn main() { print_activity_status("Updated".with(Color::Green)); } else if update_fails > 1 { update_fails = 0; - print_activity_status("Reconnecting...".with(Color::Yellow)); + print_client_status("Reconnecting...".with(Color::Yellow)); while drpc.reconnect().is_err() { sleep(Duration::from_millis(100)); } - print_activity_status("Connected".with(Color::Green)); + print_client_status("Connected".with(Color::Green)); } else { update_fails += 1; print_activity_status("Update failed".with(Color::Red)); diff --git a/src/printing.rs b/src/printing.rs index b0e6352..dad134e 100644 --- a/src/printing.rs +++ b/src/printing.rs @@ -1,23 +1,49 @@ -use std::{io::stdout, time::Duration, thread::sleep}; +use std::{io::stdout, thread::sleep, time::Duration}; use crossterm::{ cursor, execute, style::{Color, Print, StyledContent, Stylize}, - terminal::{Clear, ClearType}, + terminal::{Clear, ClearType, SetSize, SetTitle}, }; use serde::Deserialize; pub enum PrintRow { Version = 3, + Copyright = 5, NewVersionNotify = 9, ClientStatus = 11, ActivityStatus = 12, Info = 14, Error = 15, - Binds = 17 + Binds = 17, } -pub fn print_version() { +pub fn print_header() { + _ = execute!( + stdout(), + SetTitle("ndrpc"), + SetSize(80, 17), + cursor::Hide, + Print( + r" _ + | | + _ __ __| | _ __ _ __ ___ +| '_ \ / _` || '__|| '_ \ / __| +| | | || (_| || | | |_) || (__ +|_| |_| \__,_||_| | .__/ \___| + | | + |_|" + .with(Color::Blue) + .bold() + ), + cursor::MoveTo(37, PrintRow::Copyright as u16), + Print("Copyright (c) 2022 INikonI".with(Color::Blue)), + cursor::MoveTo(37, PrintRow::Version as u16), + Print(format!("Version {}", env!("CARGO_PKG_VERSION")).with(Color::Blue)), + ); +} + +pub fn print_new_version_notify() { #[derive(Deserialize)] struct GithubRelease { pub tag_name: String, @@ -29,19 +55,10 @@ pub fn print_version() { .send() .unwrap(); - let current_version = env!("CARGO_PKG_VERSION"); - - let mut stdout = stdout(); - _ = execute!( - stdout, - cursor::MoveTo(37, PrintRow::Version as u16), - Print(format!("Version {}", current_version).with(Color::Blue)), - ); - let latest_version = &response.json::().unwrap().tag_name[1..]; - if latest_version != current_version { + if latest_version != env!("CARGO_PKG_VERSION") { _ = execute!( - stdout, + stdout(), cursor::MoveTo(0, PrintRow::NewVersionNotify as u16), Print( format!( @@ -116,4 +133,4 @@ pub fn print_error(text: &str) { cursor::MoveTo(0, PrintRow::Error as u16), Clear(ClearType::CurrentLine) ); -} \ No newline at end of file +} diff --git a/src/util.rs b/src/util.rs index 2d3ed00..384d7e4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,6 @@ -use std::fs::read_to_string; use anyhow::{Context, Result}; use serde::de::DeserializeOwned; +use std::fs::read_to_string; pub fn parse_yaml_file(path: &str) -> Result where @@ -8,11 +8,10 @@ where { let file_content = read_to_string(path) .with_context(|| format!("Failed to read file or file not found \"{}\"", path))?; - let t: T = serde_yaml::from_str(&file_content).with_context(|| { + serde_yaml::from_str(&file_content).with_context(|| format!( "Failed to parse file \"{}\"", path.split('/').last().unwrap() ) - })?; - Ok(t) + ) }