From 177c4893e0df8833e90e2847546067a0aa949af7 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Thu, 21 Nov 2019 22:45:09 +0100 Subject: [PATCH] add proxy configuration value + document possible configuration options fixes #109 --- Cargo.lock | 1 + Cargo.toml | 1 + README.md | 7 ++++++- src/config.rs | 3 ++- src/main.rs | 9 +++++++-- src/spotify.rs | 29 +++++++++++++++++++++-------- src/ui/contextmenu.rs | 5 ++++- 7 files changed, 42 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c69ec7..589bc05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1544,6 +1544,7 @@ dependencies = [ "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "webbrowser 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index be293cd..5706ea2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ dbus = { version = "0.6.4", optional = true } rand = "0.6.5" webbrowser = "0.5" clipboard = { version = "0.5", optional = true } +url = "1.7" [dependencies.librespot] version = "0.1.0" diff --git a/README.md b/README.md index 7040a1a..7264300 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,12 @@ the search view. ## Configuration -Configuration is saved to `~/.config/ncspot/config.toml`. +Configuration is saved to `~/.config/ncspot/config.toml`. Possible configuration +values are: + +* `proxy`: Set an HTTP proxy +* `use_nerdfont`: Turn nerdfont glyphs on/off +* `theme`: Set a custom color palette (see below) ### Theming diff --git a/src/config.rs b/src/config.rs index 4318853..2726b85 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,11 +8,12 @@ use directories::ProjectDirs; pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b"; -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Clone, Serialize, Deserialize, Debug, Default)] pub struct Config { pub keybindings: Option>, pub theme: Option, pub use_nerdfont: Option, + pub proxy: Option, } #[derive(Serialize, Deserialize, Debug, Default, Clone)] diff --git a/src/main.rs b/src/main.rs index 321f405..d105a85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ extern crate chrono; extern crate fern; extern crate rand; +extern crate url; use std::fs; use std::path::PathBuf; @@ -161,7 +162,7 @@ fn main() { let mut credentials = get_credentials(false); - while !spotify::Spotify::test_credentials(credentials.clone()) { + while !spotify::Spotify::test_credentials(cfg.clone(), credentials.clone()) { credentials = get_credentials(true); } @@ -172,7 +173,11 @@ fn main() { let event_manager = EventManager::new(cursive.cb_sink().clone()); - let spotify = Arc::new(spotify::Spotify::new(event_manager.clone(), credentials)); + let spotify = Arc::new(spotify::Spotify::new( + cfg.clone(), + event_manager.clone(), + credentials, + )); let queue = Arc::new(queue::Queue::new(spotify.clone())); diff --git a/src/spotify.rs b/src/spotify.rs index 529cc9a..25aa7ec 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -1,3 +1,4 @@ +use config::Config; use librespot::core::authentication::Credentials; use librespot::core::cache::Cache; use librespot::core::config::SessionConfig; @@ -33,6 +34,7 @@ use futures::Future; use futures::Stream; use tokio_core::reactor::Core; use tokio_timer; +use url::Url; use std::sync::RwLock; use std::thread; @@ -197,7 +199,7 @@ impl futures::Future for Worker { } impl Spotify { - pub fn new(events: EventManager, credentials: Credentials) -> Spotify { + pub fn new(cfg: Config, events: EventManager, credentials: Credentials) -> Spotify { let player_config = PlayerConfig { bitrate: Bitrate::Bitrate320, normalisation: false, @@ -208,7 +210,9 @@ impl Spotify { let (tx, rx) = mpsc::unbounded(); { let events = events.clone(); - thread::spawn(move || Self::worker(events, rx, player_config, credentials, user_tx)); + thread::spawn(move || { + Self::worker(cfg, events, rx, player_config, credentials, user_tx) + }); } let spotify = Spotify { @@ -226,10 +230,18 @@ impl Spotify { spotify } - pub fn test_credentials(credentials: Credentials) -> bool { - let th = thread::spawn(|| { + pub fn session_config(cfg: &Config) -> SessionConfig { + let mut session_config = SessionConfig::default(); + if let Some(ref proxy) = cfg.proxy { + session_config.proxy = Url::parse(&proxy).ok(); + } + session_config + } + + pub fn test_credentials(cfg: Config, credentials: Credentials) -> bool { + let th = thread::spawn(move || { let mut core = Core::new().unwrap(); - let config = SessionConfig::default(); + let config = Self::session_config(&cfg); let handle = core.handle(); core.run(Session::connect(config, credentials, None, handle)) @@ -238,8 +250,8 @@ impl Spotify { th.join().is_ok() } - fn create_session(core: &mut Core, credentials: Credentials) -> Session { - let session_config = SessionConfig::default(); + fn create_session(cfg: &Config, core: &mut Core, credentials: Credentials) -> Session { + let session_config = Self::session_config(cfg); let cache = Cache::new(config::cache_path("librespot"), true); let handle = core.handle(); debug!("opening spotify session"); @@ -279,6 +291,7 @@ impl Spotify { } fn worker( + cfg: Config, events: EventManager, commands: mpsc::UnboundedReceiver, player_config: PlayerConfig, @@ -287,7 +300,7 @@ impl Spotify { ) { let mut core = Core::new().unwrap(); - let session = Self::create_session(&mut core, credentials); + let session = Self::create_session(&cfg, &mut core, credentials); user_tx .send(session.username()) .expect("could not pass username back to Spotify::new"); diff --git a/src/ui/contextmenu.rs b/src/ui/contextmenu.rs index 26bff6a..d146c8d 100644 --- a/src/ui/contextmenu.rs +++ b/src/ui/contextmenu.rs @@ -57,7 +57,10 @@ impl ContextMenu { content.add_item("Share", ContextMenuAction::ShareUrl(url)); } if let Some(t) = item.track() { - content.add_item("Add to playlist", ContextMenuAction::AddToPlaylist(Box::new(t))) + content.add_item( + "Add to playlist", + ContextMenuAction::AddToPlaylist(Box::new(t)), + ) } // open detail view of artist/album