Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
INikonI authored Jun 17, 2022
1 parent 9370ff3 commit 8671a0b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 67 deletions.
68 changes: 21 additions & 47 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,29 @@ 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::{
config::{Config, PresenceKind},
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},
};
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,
Expand All @@ -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));
Expand Down Expand Up @@ -145,22 +122,19 @@ fn main() {
return;
}
};
let mut presets: Vec<Preset> = {
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<Preset> = 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();
Expand All @@ -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));
Expand Down
49 changes: 33 additions & 16 deletions src/printing.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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::<GithubRelease>().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!(
Expand Down Expand Up @@ -116,4 +133,4 @@ pub fn print_error(text: &str) {
cursor::MoveTo(0, PrintRow::Error as u16),
Clear(ClearType::CurrentLine)
);
}
}
7 changes: 3 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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<T>(path: &str) -> Result<T>
where
T: DeserializeOwned,
{
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)
)
}

0 comments on commit 8671a0b

Please sign in to comment.