Commit 4649ab8f18
4649ab8f18326a5147d1d1dd50ffcf65440eb416
parent: 82a657f7a0
Unsigned
Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2023-08-19 12:13 UTC
added embedded mpv instance, trying to implement #38
Layout: unified · split
Cargo.lock
+15 −1
| @@ -583,6 +583,19 @@ version = "0.2.147" |
| 583 | 583 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 584 | 584 | checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" |
| 585 | 585 | |
| 586 | [[package]] |
| 587 | name = "libmpv" |
| 588 | version = "2.0.1" |
| 589 | source = "git+https://github.com/sudipghimire533/libmpv-rs#18aa79a6b73e309360d3dcaeb434e862f9483350" |
| 590 | dependencies = [ |
| 591 | "libmpv-sys", |
| 592 | ] |
| 593 | |
| 594 | [[package]] |
| 595 | name = "libmpv-sys" |
| 596 | version = "3.1.0" |
| 597 | source = "git+https://github.com/sudipghimire533/libmpv-rs#18aa79a6b73e309360d3dcaeb434e862f9483350" |
| 598 | |
| 586 | 599 | [[package]] |
| 587 | 600 | name = "linux-raw-sys" |
| 588 | 601 | version = "0.3.8" |
| @@ -1520,7 +1533,7 @@ dependencies = [ |
| 1520 | 1533 | |
| 1521 | 1534 | [[package]] |
| 1522 | 1535 | name = "youtube-tui" |
| 1523 | | version = "0.7.5" |
| 1536 | version = "0.8.0" |
| 1524 | 1537 | dependencies = [ |
| 1525 | 1538 | "chrono", |
| 1526 | 1539 | "clipboard", |
| @@ -1528,6 +1541,7 @@ dependencies = [ |
| 1528 | 1541 | "dyn-clone", |
| 1529 | 1542 | "home", |
| 1530 | 1543 | "invidious", |
| 1544 | "libmpv", |
| 1531 | 1545 | "ratatui", |
| 1532 | 1546 | "serde", |
| 1533 | 1547 | "serde_json", |
Cargo.toml
+4 −2
| @@ -1,6 +1,6 @@ |
| 1 | 1 | [package] |
| 2 | 2 | name = "youtube-tui" |
| 3 | | version = "0.7.5" |
| 3 | version = "0.8.0" |
| 4 | 4 | authors = ["Siriusmart <siriusmart@tuta.io>"] |
| 5 | 5 | edition = "2021" |
| 6 | 6 | description = "An aesthetically pleasing YouTube TUI written in Rust" |
| @@ -16,10 +16,11 @@ exclude = ["/docs", "/LICENSE", "/threadcounter.sh", "/PKGBUILD"] |
| 16 | 16 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 17 | 17 | |
| 18 | 18 | [features] |
| 19 | | default = ["sixel", "halfblock", "clipboard"] |
| 19 | default = ["sixel", "halfblock", "clipboard", "mpv"] |
| 20 | 20 | sixel = ["viuer/sixel"] |
| 21 | 21 | halfblock = ["viuer"] |
| 22 | 22 | clipboard = ["dep:clipboard"] |
| 23 | mpv = ["dep:libmpv"] |
| 23 | 24 | |
| 24 | 25 | [dependencies] |
| 25 | 26 | serde = {version = "1.0", default-features = false, features = ["derive"]} |
| @@ -36,6 +37,7 @@ serde_json = {version = "1.0", default-features = false} |
| 36 | 37 | clipboard = {version = "0.5", optional = true} |
| 37 | 38 | urlencoding = "2.1" |
| 38 | 39 | ratatui = {version = "0.21", default-features = false, features = ["serde"]} |
| 40 | libmpv = { git = "https://github.com/sudipghimire533/libmpv-rs", optional = true } |
| 39 | 41 | |
| 40 | 42 | # tui-additions = {path = "../../Rust/Packages/tui-additions", default-features = false, features = ["framework", "widgets"]} |
| 41 | 43 | # invidious = {path = "../../Rust/Packages/invidious"} |
docs/src/changelogs.md
+6
| @@ -1,5 +1,11 @@ |
| 1 | 1 | # Changelogs |
| 2 | 2 | |
| 3 | ### v0.8.0 (latest unstable on Github) |
| 4 | |
| 5 | #### Added |
| 6 | |
| 7 | - Build in MPV player, controlled by command `mpv [libmpv command] [args...]` |
| 8 | |
| 3 | 9 | ## v0.7.4 (latest stable on crates.io) |
| 4 | 10 | |
| 5 | 11 | ### Added |
src/global/functions/run_command.rs
+23
| @@ -499,6 +499,29 @@ pub fn run_single_command( |
| 499 | 499 | let keymodifier = KeyModifiers::from_bits_truncate(modifier); |
| 500 | 500 | key_input(KeyEvent::new(keycode, keymodifier), framework, terminal) |
| 501 | 501 | } |
| 502 | // #[cfg(feature = "mpv")] |
| 503 | // ["queueplay", ..] => { |
| 504 | // if let Err(e) = framework.data.global.get::<Status>().unwrap().mpv.queue_play(command[1..].into_iter().map(|path| path.trim_matches('\'')).collect::<Vec<_>>().as_slice()) { |
| 505 | // *framework.data.global.get_mut::<Message>().unwrap() = Message::Error(format!("MPV error: {e}")) |
| 506 | // } |
| 507 | // |
| 508 | // } |
| 509 | #[cfg(feature = "mpv")] |
| 510 | // ["mpv", name, ..] => framework.data.global.get::<Status>().unwrap().mpv.command( |
| 511 | // name.to_string(), |
| 512 | // command[2..].into_iter().map(|s| s.to_string()).collect(), |
| 513 | // ), |
| 514 | ["mpv", name, ..] => framework |
| 515 | .data |
| 516 | .global |
| 517 | .get::<MpvWrapper>() |
| 518 | .unwrap() |
| 519 | .sender |
| 520 | .send(MpvAction::Command { |
| 521 | name: name.to_string(), |
| 522 | args: command[2..].iter().map(|s| s.to_string()).collect(), |
| 523 | }) |
| 524 | .unwrap(), |
| 502 | 525 | _ => { |
| 503 | 526 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 504 | 527 | Message::Error(format!("Unknown command: `{}`", command.join(" "))); |
src/global/structs/mod.rs
+4
| @@ -5,6 +5,8 @@ mod item; |
| 5 | 5 | mod keyaction; |
| 6 | 6 | mod library; |
| 7 | 7 | mod message; |
| 8 | #[cfg(feature = "mpv")] |
| 9 | mod mpv; |
| 8 | 10 | mod page; |
| 9 | 11 | mod state_env; |
| 10 | 12 | mod status; |
| @@ -18,6 +20,8 @@ pub use item::*; |
| 18 | 20 | pub use keyaction::*; |
| 19 | 21 | pub use library::*; |
| 20 | 22 | pub use message::*; |
| 23 | #[cfg(feature = "mpv")] |
| 24 | pub use mpv::*; |
| 21 | 25 | pub use page::*; |
| 22 | 26 | pub use state_env::*; |
| 23 | 27 | pub use status::*; |
src/global/structs/mpv.rs
added
+66
| @@ -0,0 +1,66 @@ |
| 1 | use std::{sync::mpsc, thread}; |
| 2 | |
| 3 | use libmpv::Mpv; |
| 4 | use typemap::Key; |
| 5 | |
| 6 | pub struct MpvWrapper { |
| 7 | pub sender: mpsc::Sender<MpvAction>, |
| 8 | pub copier: mpsc::Receiver<MpvResponse>, |
| 9 | } |
| 10 | |
| 11 | impl Clone for MpvWrapper { |
| 12 | fn clone(&self) -> Self { |
| 13 | panic!("no") |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | // impl Default for MpvWrapper { |
| 18 | // fn default() -> Self { |
| 19 | // Self::spawn() |
| 20 | // } |
| 21 | // } |
| 22 | |
| 23 | impl MpvWrapper { |
| 24 | pub fn spawn() -> Self { |
| 25 | let (sender, receiver) = mpsc::channel(); |
| 26 | let (responder, copier) = mpsc::channel(); |
| 27 | thread::spawn(move || { |
| 28 | let mpv = Mpv::new().unwrap(); |
| 29 | mpv.set_property("video", "no").unwrap(); |
| 30 | loop { |
| 31 | let action = match receiver.recv() { |
| 32 | Ok(res) => res, |
| 33 | Err(_) => continue, |
| 34 | }; |
| 35 | match action { |
| 36 | MpvAction::Command { name, args } => match mpv |
| 37 | .command(&name, &args.iter().map(|s| s.as_str()).collect::<Vec<_>>()) |
| 38 | { |
| 39 | Ok(_) => responder.send(MpvResponse::Copy).unwrap(), |
| 40 | Err(e) => responder.send(MpvResponse::Error(e.to_string())).unwrap(), |
| 41 | }, |
| 42 | MpvAction::RequestI64 { name } => responder |
| 43 | .send(MpvResponse::I64(mpv.get_property::<i64>(&name).ok())) |
| 44 | .unwrap(), |
| 45 | } |
| 46 | } |
| 47 | }); |
| 48 | Self { sender, copier } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug)] |
| 53 | pub enum MpvAction { |
| 54 | Command { name: String, args: Vec<String> }, |
| 55 | RequestI64 { name: String }, |
| 56 | } |
| 57 | |
| 58 | pub enum MpvResponse { |
| 59 | Copy, |
| 60 | I64(Option<i64>), |
| 61 | Error(String), |
| 62 | } |
| 63 | |
| 64 | impl Key for MpvWrapper { |
| 65 | type Value = Self; |
| 66 | } |
src/init.rs
+12 −1
| @@ -4,7 +4,11 @@ use crate::{ |
| 4 | 4 | }; |
| 5 | 5 | use home::home_dir; |
| 6 | 6 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 7 | | use std::{error::Error, fs, io::Stdout}; |
| 7 | use std::{ |
| 8 | error::Error, |
| 9 | fs::{self, OpenOptions}, |
| 10 | io::{Stdout, Write}, |
| 11 | }; |
| 8 | 12 | use tui_additions::framework::{Framework, FrameworkClean}; |
| 9 | 13 | |
| 10 | 14 | /// app to run before the app starts |
| @@ -49,11 +53,18 @@ pub fn init( |
| 49 | 53 | .insert::<Subscriptions>(Subscriptions::load()); |
| 50 | 54 | framework.data.global.insert::<Library>(Library::load()); |
| 51 | 55 | framework.data.global.insert::<Message>(Message::None); |
| 56 | |
| 52 | 57 | framework.data.global.insert::<Status>(Status { |
| 53 | 58 | provider: framework.data.global.get::<MainConfig>().unwrap().provider, |
| 54 | 59 | ..Status::default() |
| 55 | 60 | }); |
| 56 | 61 | |
| 62 | #[cfg(feature = "mpv")] |
| 63 | framework |
| 64 | .data |
| 65 | .global |
| 66 | .insert::<MpvWrapper>(MpvWrapper::spawn()); |
| 67 | |
| 57 | 68 | framework.data.state.insert::<Tasks>(Tasks::default()); |
| 58 | 69 | framework.data.state.insert::<Page>(Page::default()); |
| 59 | 70 | framework |
src/items/searchbar.rs
+1 −1
| @@ -148,7 +148,7 @@ impl FrameworkItem for SearchBar { |
| 148 | 148 | return Ok(()); |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | | let mut search = framework.data.state.get_mut::<Search>().unwrap(); |
| 151 | let search = framework.data.state.get_mut::<Search>().unwrap(); |
| 152 | 152 | search.query = self.text_field.content.clone(); |
| 153 | 153 | let search = search.clone(); |
| 154 | 154 | let tasks = framework.data.state.get_mut::<Tasks>().unwrap(); |
src/run.rs
+26 −1
| @@ -1,6 +1,11 @@ |
| 1 | 1 | use crossterm::event::{self, Event, MouseButton, MouseEventKind}; |
| 2 | 2 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 3 | | use std::{any::TypeId, error::Error, io::Stdout}; |
| 3 | use std::{ |
| 4 | any::TypeId, |
| 5 | error::Error, |
| 6 | io::Stdout, |
| 7 | time::{Duration, Instant}, |
| 8 | }; |
| 4 | 9 | use tui_additions::framework::Framework; |
| 5 | 10 | |
| 6 | 11 | use crate::{ |
| @@ -14,6 +19,8 @@ pub fn run( |
| 14 | 19 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 15 | 20 | framework: &mut Framework, |
| 16 | 21 | ) -> Result<(), Box<dyn Error>> { |
| 22 | let tick_rate = Duration::from_secs(1); |
| 23 | let mut last_tick = Instant::now(); |
| 17 | 24 | loop { |
| 18 | 25 | // repeat forever until all tasks are ran (and Tasks is cleared) |
| 19 | 26 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { |
| @@ -33,6 +40,24 @@ pub fn run( |
| 33 | 40 | if framework.data.global.get::<Status>().unwrap().exit { |
| 34 | 41 | break; |
| 35 | 42 | } |
| 43 | |
| 44 | if !event::poll( |
| 45 | tick_rate |
| 46 | .checked_sub(last_tick.elapsed()) |
| 47 | .unwrap_or_else(|| Duration::from_secs(0)), |
| 48 | )? { |
| 49 | // do tick changes |
| 50 | last_tick = Instant::now(); |
| 51 | // dbg!(framework |
| 52 | // .data |
| 53 | // .global |
| 54 | // .get::<Status>() |
| 55 | // .unwrap() |
| 56 | // .mpv |
| 57 | // .sender.send(MpvAction::RequestI64 { name: "audio-pts".to_string() })); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 36 | 61 | *framework.data.global.get_mut::<Message>().unwrap() = Message::None; |
| 37 | 62 | |
| 38 | 63 | match event::read()? { |