From 69ce498a051d0136cf6903907c1e592b9c75e7b5 Mon Sep 17 00:00:00 2001 From: talwat <83217276+talwat@users.noreply.github.com> Date: Wed, 2 Oct 2024 00:27:18 +0200 Subject: [PATCH] feat: add --alternate flag --- .github/workflows/build.yml | 2 +- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 6 +++++- src/play.rs | 9 ++++++--- src/player/ui.rs | 24 ++++++++++++++++++------ 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ae930bb..22bf93e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -58,7 +58,7 @@ jobs: - name: Install dependencies if: matrix.os == 'ubuntu-latest' - run: sudo apt install libasound2-dev + run: sudo apt install libasound2-dev libssl-dev - name: Build binary uses: houseabsolute/actions-rust-cross@v0 diff --git a/Cargo.lock b/Cargo.lock index ac7fcc9..b2a1010 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -982,7 +982,7 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lowfi" -version = "1.2.1" +version = "1.2.2" dependencies = [ "Inflector", "arc-swap", diff --git a/Cargo.toml b/Cargo.toml index 6812c41..232dbf7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lowfi" -version = "1.2.1" +version = "1.2.2" edition = "2021" description = "An extremely simple lofi player." license = "MIT" diff --git a/src/main.rs b/src/main.rs index 49f2290..4f3b5c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,10 @@ mod tracks; #[derive(Parser)] #[command(about)] struct Args { + /// Whether to use an alternate terminal screen. + #[clap(long, short)] + alternate: bool, + /// The command that was ran. /// This is [None] if no command was specified. #[command(subcommand)] @@ -42,6 +46,6 @@ async fn main() -> eyre::Result<()> { } => scrape::scrape(extension, include_full).await, } } else { - play::play().await + play::play(cli.alternate).await } } diff --git a/src/play.rs b/src/play.rs index 2e187e1..815093e 100644 --- a/src/play.rs +++ b/src/play.rs @@ -2,7 +2,7 @@ use std::{io::stderr, sync::Arc}; -use crossterm::cursor::SavePosition; +use crossterm::{cursor::SavePosition, terminal}; use tokio::{ sync::mpsc::{self}, task::{self}, @@ -13,20 +13,23 @@ use crate::player::{ui, Messages}; /// Initializes the audio server, and then safely stops /// it when the frontend quits. -pub async fn play() -> eyre::Result<()> { +pub async fn play(alternate: bool) -> eyre::Result<()> { // Save the position. This is important since later on we can revert to this position // and clear any potential error messages that may have showed up. // TODO: Figure how to set some sort of flag to hide error messages within rodio, // TODO: Instead of just ignoring & clearing them after. crossterm::execute!(stderr(), SavePosition)?; + // Enable raw mode early in theory to prevent uncontrolled text in the terminal from the user. + terminal::enable_raw_mode()?; + let (tx, rx) = mpsc::channel(8); let player = Arc::new(Player::new().await?); let audio = task::spawn(Player::play(Arc::clone(&player), tx.clone(), rx)); tx.send(Messages::Init).await?; - ui::start(Arc::clone(&player), tx.clone()).await?; + ui::start(Arc::clone(&player), tx.clone(), alternate).await?; audio.abort(); player.sink.stop(); diff --git a/src/player/ui.rs b/src/player/ui.rs index 638773c..59b5227 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -6,10 +6,10 @@ use crate::tracks::TrackInfo; use super::Player; use crossterm::{ - cursor::{Hide, MoveToColumn, MoveUp, RestorePosition, Show}, + cursor::{Hide, MoveTo, MoveToColumn, MoveUp, RestorePosition, Show}, event::{self, KeyCode, KeyModifiers}, style::{Print, Stylize}, - terminal::{self, Clear, ClearType}, + terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, }; use tokio::{ sync::mpsc::Sender, @@ -136,15 +136,24 @@ async fn interface(queue: Arc) -> eyre::Result<()> { } /// Initializes the UI, this will also start taking input from the user. -pub async fn start(queue: Arc, sender: Sender) -> eyre::Result<()> { +/// +/// `alternate` controls whether to use [EnterAlternateScreen] in order to hide +/// previous terminal history. +pub async fn start( + queue: Arc, + sender: Sender, + alternate: bool, +) -> eyre::Result<()> { crossterm::execute!( stderr(), RestorePosition, Clear(ClearType::FromCursorDown), Hide )?; - terminal::enable_raw_mode()?; - //crossterm::execute!(stderr(), EnterAlternateScreen, MoveTo(0, 0))?; + + if alternate { + crossterm::execute!(stderr(), EnterAlternateScreen, MoveTo(0, 0))?; + } task::spawn(interface(Arc::clone(&queue))); @@ -179,7 +188,10 @@ pub async fn start(queue: Arc, sender: Sender) -> eyre::Result } } - //crossterm::execute!(stderr(), LeaveAlternateScreen)?; + if alternate { + crossterm::execute!(stderr(), LeaveAlternateScreen)?; + } + crossterm::execute!(stderr(), Clear(ClearType::FromCursorDown), Show)?; terminal::disable_raw_mode()?;