diff --git a/docs/ENVIRONMENT_VARS.md b/docs/ENVIRONMENT_VARS.md index 868d11e..3d27569 100644 --- a/docs/ENVIRONMENT_VARS.md +++ b/docs/ENVIRONMENT_VARS.md @@ -6,4 +6,4 @@ lowfi has some more specific options, usually as a result of minor feature reque If you have some behavior you'd like to change, which is quite specific, then see if one of these options suits you. * `LOWFI_FIXED_MPRIS_NAME` - Limits the number of lowfi instances to one, but ensures the player name is always `lowfi`. -* `LOWFI_DISABLE_UI` - Disables the UI. +* `LOWFI_DISABLE_UI` - Disables the UI. This requires MPRIS, so that you can still actually control lowfi. diff --git a/src/main.rs b/src/main.rs index 97d1405..67feff6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,6 +89,10 @@ enum Commands { }, } +pub fn env(name: &str) -> bool { + std::env::var(name).is_ok_and(|x| x == "1") +} + /// Returns the application data directory used for persistency. /// /// The function returns the platform-specific user data directory with @@ -121,7 +125,7 @@ async fn main() -> eyre::Result<()> { } let stream = audio::stream()?; - let environment = ui::Environment::ready(args.alternate)?; + let environment = ui::Environment::ready(&args)?; let (mut player, mut tasks) = Player::init(args, stream.mixer()) .await .inspect_err(|_| environment.cleanup(false).unwrap())?; diff --git a/src/ui.rs b/src/ui.rs index 1c045cb..3c17369 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -4,7 +4,7 @@ use crate::player::Current; use tokio::{sync::broadcast, time::Instant}; pub mod environment; -pub mod task; +pub mod init; pub use environment::Environment; pub mod input; pub mod interface; diff --git a/src/ui/environment.rs b/src/ui/environment.rs index a03bafe..a0add50 100644 --- a/src/ui/environment.rs +++ b/src/ui/environment.rs @@ -15,16 +15,29 @@ pub struct Environment { /// Whether the terminal is in an alternate screen or not. alternate: bool, + + /// Whether the UI is actually enabled at all. + /// This will effectively make the environment just do nothing. + enabled: bool, } impl Environment { /// This prepares the terminal, returning an [Environment] helpful /// for cleaning up afterwards. - pub fn ready(alternate: bool) -> super::Result { + pub fn ready(args: &crate::Args) -> super::Result { + let enabled = !crate::env("LOWFI_DISABLE_UI"); + if !enabled { + return Ok(Environment { + enhancement: false, + alternate: args.alternate, + enabled, + }); + } + let mut lock = stdout().lock(); crossterm::execute!(lock, Hide)?; - if alternate { + if args.alternate { crossterm::execute!(lock, EnterAlternateScreen, MoveTo(0, 0))?; } @@ -39,8 +52,9 @@ impl Environment { } let environment = Self { + enabled, enhancement, - alternate, + alternate: args.alternate, }; panic::set_hook(Box::new(move |info| { @@ -54,6 +68,10 @@ impl Environment { /// Uses the information collected from initialization to safely close down /// the terminal & restore it to it's previous state. pub fn cleanup(&self, elegant: bool) -> super::Result<()> { + if !self.enabled { + return Ok(()); + }; + let mut lock = stdout().lock(); if self.alternate { diff --git a/src/ui/task.rs b/src/ui/init.rs similarity index 100% rename from src/ui/task.rs rename to src/ui/init.rs diff --git a/src/ui/interface.rs b/src/ui/interface.rs index b82113a..306a725 100644 --- a/src/ui/interface.rs +++ b/src/ui/interface.rs @@ -2,7 +2,7 @@ use crate::{ ui::{self, State}, Args, }; -use std::{env, io::stdout, time::Duration}; +use std::{io::stdout, time::Duration}; pub mod clock; pub mod components; @@ -59,7 +59,7 @@ impl TryFrom<&Args> for Params { let delta = 1.0 / f32::from(args.fps); let delta = Duration::from_secs_f32(delta); - let disabled = env::var("LOWFI_DISABLE_UI").is_ok_and(|x| x == "1"); + let disabled = crate::env("LOWFI_DISABLE_UI"); if disabled && !cfg!(feature = "mpris") { return Err(ui::Error::RejectedDisable); }