fix: don't initialize environment if ui is disabled

This commit is contained in:
Tal 2025-12-29 10:52:32 +01:00
parent 6b65b7d952
commit cd002ef9ab
6 changed files with 30 additions and 8 deletions

View File

@ -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. 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_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.

View File

@ -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. /// Returns the application data directory used for persistency.
/// ///
/// The function returns the platform-specific user data directory with /// The function returns the platform-specific user data directory with
@ -121,7 +125,7 @@ async fn main() -> eyre::Result<()> {
} }
let stream = audio::stream()?; 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()) let (mut player, mut tasks) = Player::init(args, stream.mixer())
.await .await
.inspect_err(|_| environment.cleanup(false).unwrap())?; .inspect_err(|_| environment.cleanup(false).unwrap())?;

View File

@ -4,7 +4,7 @@ use crate::player::Current;
use tokio::{sync::broadcast, time::Instant}; use tokio::{sync::broadcast, time::Instant};
pub mod environment; pub mod environment;
pub mod task; pub mod init;
pub use environment::Environment; pub use environment::Environment;
pub mod input; pub mod input;
pub mod interface; pub mod interface;

View File

@ -15,16 +15,29 @@ pub struct Environment {
/// Whether the terminal is in an alternate screen or not. /// Whether the terminal is in an alternate screen or not.
alternate: bool, alternate: bool,
/// Whether the UI is actually enabled at all.
/// This will effectively make the environment just do nothing.
enabled: bool,
} }
impl Environment { impl Environment {
/// This prepares the terminal, returning an [Environment] helpful /// This prepares the terminal, returning an [Environment] helpful
/// for cleaning up afterwards. /// for cleaning up afterwards.
pub fn ready(alternate: bool) -> super::Result<Self> { pub fn ready(args: &crate::Args) -> super::Result<Self> {
let enabled = !crate::env("LOWFI_DISABLE_UI");
if !enabled {
return Ok(Environment {
enhancement: false,
alternate: args.alternate,
enabled,
});
}
let mut lock = stdout().lock(); let mut lock = stdout().lock();
crossterm::execute!(lock, Hide)?; crossterm::execute!(lock, Hide)?;
if alternate { if args.alternate {
crossterm::execute!(lock, EnterAlternateScreen, MoveTo(0, 0))?; crossterm::execute!(lock, EnterAlternateScreen, MoveTo(0, 0))?;
} }
@ -39,8 +52,9 @@ impl Environment {
} }
let environment = Self { let environment = Self {
enabled,
enhancement, enhancement,
alternate, alternate: args.alternate,
}; };
panic::set_hook(Box::new(move |info| { panic::set_hook(Box::new(move |info| {
@ -54,6 +68,10 @@ impl Environment {
/// Uses the information collected from initialization to safely close down /// Uses the information collected from initialization to safely close down
/// the terminal & restore it to it's previous state. /// the terminal & restore it to it's previous state.
pub fn cleanup(&self, elegant: bool) -> super::Result<()> { pub fn cleanup(&self, elegant: bool) -> super::Result<()> {
if !self.enabled {
return Ok(());
};
let mut lock = stdout().lock(); let mut lock = stdout().lock();
if self.alternate { if self.alternate {

View File

@ -2,7 +2,7 @@ use crate::{
ui::{self, State}, ui::{self, State},
Args, Args,
}; };
use std::{env, io::stdout, time::Duration}; use std::{io::stdout, time::Duration};
pub mod clock; pub mod clock;
pub mod components; pub mod components;
@ -59,7 +59,7 @@ impl TryFrom<&Args> for Params {
let delta = 1.0 / f32::from(args.fps); let delta = 1.0 / f32::from(args.fps);
let delta = Duration::from_secs_f32(delta); 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") { if disabled && !cfg!(feature = "mpris") {
return Err(ui::Error::RejectedDisable); return Err(ui::Error::RejectedDisable);
} }