Encode ANSI escape codes correctly on windows

This commit is contained in:
oSumAtrIX 2021-10-17 06:27:41 +02:00
parent 6a1b288fea
commit 4fa97e95f6
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4

View file

@ -1,17 +1,10 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
mod converter;
mod downloader;
mod error;
mod settings;
mod spotify;
mod tag;
use async_std::{task, task::block_on}; use async_std::{task, task::block_on};
use colored::Colorize; use colored::{Colorize, control::set_virtual_terminal};
use downloader::{DownloadState, Downloader}; use downloader::{DownloadState, Downloader};
use settings::Settings; use configuration::Configuration;
use spotify::Spotify; use spotify::Spotify;
use std::{ use std::{
env, env,
@ -20,12 +13,27 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
mod converter;
mod downloader;
mod error;
mod configuration;
mod spotify;
mod tag;
fn main() { fn main() {
block_on(start()); block_on(start());
} }
async fn start() { async fn start() {
let settings = match Settings::load().await { #[cfg(windows)] {
//backwards compatibility.
match set_virtual_terminal(true) {
Ok(_) => {},
Err(_) => {}
};
}
let settings = match Configuration::load("settings.json".to_string()).await {
Ok(settings) => { Ok(settings) => {
println!( println!(
"{} {}.", "{} {}.",
@ -34,26 +42,26 @@ async fn start() {
); );
settings settings
} }
Err(_e) => { Err(e) => {
println!( println!(
"{} {}...", "{} {}...",
"Settings could not be loaded, because of the following error:".red(), "Settings could not be loaded, because of the following error:".red(),
_e e
); );
let default_settings = let default_settings =
Settings::new("username", "password", "client_id", "secret").unwrap(); Configuration::new("username", "password", "client_id", "secret").unwrap();
match default_settings.save().await { match default_settings.save("settings.json".to_string()).await {
Ok(_) => { Ok(_) => {
println!( println!(
"{}", "{}",
"..but default settings have been created successfully. Edit them and run the program again.".green() "..but default settings have been created successfully. Edit them and run the program again.".green()
); );
} }
Err(_e) => { Err(e) => {
println!( println!(
"{} {}", "{} {}",
"..and default settings could not be written:".red(), "..and default settings could not be written:".red(),
_e e
); );
} }
}; };
@ -75,11 +83,11 @@ async fn start() {
println!("{}", "Login succeeded.".green()); println!("{}", "Login succeeded.".green());
spotify spotify
} }
Err(_e) => { Err(e) => {
println!( println!(
"{} {}", "{} {}",
"Login failed, possibly due to invalid credentials or settings:".red(), "Login failed, possibly due to invalid credentials or settings:".red(),
_e e
); );
return; return;
} }
@ -89,14 +97,14 @@ async fn start() {
match downloader.add_uri(&args[1]).await { match downloader.add_uri(&args[1]).await {
Ok(_) => {} Ok(_) => {}
Err(_e) => { Err(e) => {
error!("{} {}", "Adding url failed:".red(), _e) error!("{} {}", "Adding url failed:".red(), e)
} }
} }
let refresh = Duration::from_secs(settings.refresh_ui_seconds); let refresh = Duration::from_secs(settings.refresh_ui_seconds);
let now = Instant::now(); let now = Instant::now();
let mut time_elapsed: u64; let mut timeelapsed: u64;
'outer: loop { 'outer: loop {
print!("{esc}[2J{esc}[1;1H", esc = 27 as char); print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
@ -136,15 +144,15 @@ async fn start() {
println!("{:<19}| {}", progress, download.title); println!("{:<19}| {}", progress, download.title);
} }
time_elapsed = now.elapsed().as_secs(); timeelapsed = now.elapsed().as_secs();
if exit_flag == 1 { if exit_flag == 1 {
break 'outer; break 'outer;
} }
println!("\nElapsed second(s): {}", time_elapsed); println!("\nElapsed second(s): {}", timeelapsed);
task::sleep(refresh).await task::sleep(refresh).await
} }
println!("Finished download(s) in {} second(s).", time_elapsed); println!("Finished download(s) in {} second(s).", timeelapsed);
} else { } else {
println!( println!(
"Usage:\n{} (track_url | album_url | playlist_url | artist_url )", "Usage:\n{} (track_url | album_url | playlist_url | artist_url )",