Skip to content

Commit

Permalink
feat(input): new input system with a timeout feature and more
Browse files Browse the repository at this point in the history
as well as several small code refactors and little improvements
  • Loading branch information
Vernoxvernax committed Feb 10, 2024
1 parent 5f50e10 commit 19f550c
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 209 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ config = { version = "0.14.0", features = ["yaml"] }
tokio = { version = "1.35.1", features = ["full"] }
chrono = { version = "0.4.33", features = ["alloc", "clock"] }
libmpv-sirno = "2.0.2-fork.1"
getch = "0.3.1"
crossterm = "0.27.0"
serde = "1.0.196"
serde_json = "1.0.113"
serde_derive = "1.0.196"
http = "1.0.0"
colored = "2.1.0"
urlencoding = "2.1.3"
discord-presence = "1.0.0"
discord-presence = { git = "https://github.com/jewlexx/discord-presence/" }
toml = "0.8.9"
regex = "1.10.3"
rpassword = "7.3.1"
Expand Down
2 changes: 2 additions & 0 deletions RECENT_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* Removed getch crate and rewrote it's functionality using the awesome crossterm crate with additional timeout features.
* Fixed issue where episodes would be skipped if they would already be marked as played.
* Some code refactoring.
* Bump dependencies.
47 changes: 28 additions & 19 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// This part of puddler parses and writes the emby and jellyfin config files
use std::fs;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::{
fs,
path::Path,
io::{
stdout,
stdin,
prelude::*
}
};
use colored::Colorize;
use regex::Regex;

use crate::APPNAME;
use crate::mediaserver_information::ConfigFileRaw;
use crate::mediaserver_information::ConfigFile;
use crate::mediaserver_information::ConfigFileUser;
use crate::mediaserver_information::getch;
use crate::is_numeric;
use crate::{
APPNAME,
is_numeric,
mediaserver_information::{
ConfigFileRaw,
ConfigFile,
ConfigFileUser,
getch
}
};


pub fn choose_config(server_kind: char, autologin: bool) -> Option<String> {
Expand Down Expand Up @@ -46,7 +55,7 @@ pub fn choose_config(server_kind: char, autologin: bool) -> Option<String> {
}
};
if autologin {
return Some(file_list.get(0).unwrap().to_string())
return Some(file_list.first().unwrap().to_string())
};
let copy = file_list.clone();
println!("Please choose which configuration file you want to use.");
Expand All @@ -56,9 +65,9 @@ pub fn choose_config(server_kind: char, autologin: bool) -> Option<String> {
print!(": ");
let index: usize;
loop {
io::stdout().flush().expect("Failed to flush stdout");
stdout().flush().expect("Failed to flush stdout");
let mut index_raw: String = String::new();
io::stdin().read_line( &mut index_raw).unwrap();
stdin().read_line( &mut index_raw).unwrap();
index_raw.trim().parse::<String>().unwrap();
if ! is_numeric(&index_raw) {
print!("Invalid input, please try again.\n: ")
Expand All @@ -74,7 +83,7 @@ pub fn choose_config(server_kind: char, autologin: bool) -> Option<String> {


pub fn read_config(config_path_string: &String, autologin: bool) -> Result<(ConfigFile, ConfigFileRaw), (Option<ConfigFileRaw>, &str)> {
let file = std::fs::read_to_string(config_path_string).unwrap();
let file = fs::read_to_string(config_path_string).unwrap();
let local_config_file: Result<ConfigFileRaw, serde_json::Error> = serde_json::from_str::<ConfigFileRaw>(&file);
match local_config_file {
Ok(a) => {
Expand Down Expand Up @@ -106,7 +115,7 @@ pub fn read_config(config_path_string: &String, autologin: bool) -> Result<(Conf
} else {
print!("Do you want to use this config?\n {} ({}): {}\n Username: {}\n (Y)es / (N)o", server_name.green(), media_server_name, a.ipaddress, a.user.first().unwrap().username);
let input = getch("YyNn");
io::stdout().flush().expect("Failed to flush stdout");
stdout().flush().expect("Failed to flush stdout");
if "yY".contains(input) {
user = a.user.first().unwrap();
Ok((ConfigFile {
Expand All @@ -131,18 +140,18 @@ pub fn read_config(config_path_string: &String, autologin: bool) -> Result<(Conf
println!(" [{}] {}", &index, thing.username);
}
print!(": ");
io::stdout().flush().expect("Failed to flush stdout");
stdout().flush().expect("Failed to flush stdout");
let index: usize;
loop {
let mut index_raw: String = String::new();
io::stdin().read_line( &mut index_raw).unwrap();
stdin().read_line( &mut index_raw).unwrap();
index_raw.trim().parse::<String>().unwrap();
if ! is_numeric(&index_raw) {
if index_raw.trim() == "Add" {
return Err((Some(a), "add user"))
} else {
print!("Invalid input, please try again.\n: ");
io::stdout().flush().expect("Failed to flush stdout");
stdout().flush().expect("Failed to flush stdout");
}
} else {
index = index_raw.trim().parse().unwrap();
Expand Down Expand Up @@ -203,7 +212,7 @@ pub fn write_config(config_path_string: String, config_file: &ConfigFile, other_
user: [config_file_user].to_vec()
}
};
let result = std::fs::write(config_path_string, serde_json::to_string_pretty(&config_file_raw).unwrap());
let result = fs::write(config_path_string, serde_json::to_string_pretty(&config_file_raw).unwrap());
match result {
Ok(()) => println!("Saved to config file ..."),
Err(_e) => panic!("write access??")
Expand Down
3 changes: 1 addition & 2 deletions src/discord.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{thread, sync::{Arc, Mutex}};

use discord_presence::Client;

use crate::mediaserver_information;
use discord_presence::Client;
use mediaserver_information::HeadDict;


Expand Down
Loading

0 comments on commit 19f550c

Please sign in to comment.