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 | source = "registry+https://github.com/rust-lang/crates.io-index" |
583 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 584 | checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" |
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 | [[package]] |
599 | [[package]] |
| 587 | name = "linux-raw-sys" |
600 | name = "linux-raw-sys" |
| 588 | version = "0.3.8" |
601 | version = "0.3.8" |
| @@ -1520,7 +1533,7 @@ dependencies = [ |
| 1520 | |
1533 | |
| 1521 | [[package]] |
1534 | [[package]] |
| 1522 | name = "youtube-tui" |
1535 | name = "youtube-tui" |
| 1523 | version = "0.7.5" |
1536 | version = "0.8.0" |
| 1524 | dependencies = [ |
1537 | dependencies = [ |
| 1525 | "chrono", |
1538 | "chrono", |
| 1526 | "clipboard", |
1539 | "clipboard", |
| @@ -1528,6 +1541,7 @@ dependencies = [ |
| 1528 | "dyn-clone", |
1541 | "dyn-clone", |
| 1529 | "home", |
1542 | "home", |
| 1530 | "invidious", |
1543 | "invidious", |
| |
1544 | "libmpv", |
| 1531 | "ratatui", |
1545 | "ratatui", |
| 1532 | "serde", |
1546 | "serde", |
| 1533 | "serde_json", |
1547 | "serde_json", |
Cargo.toml
+4 −2
| @@ -1,6 +1,6 @@ |
| 1 | [package] |
1 | [package] |
| 2 | name = "youtube-tui" |
2 | name = "youtube-tui" |
| 3 | version = "0.7.5" |
3 | version = "0.8.0" |
| 4 | authors = ["Siriusmart <siriusmart@tuta.io>"] |
4 | authors = ["Siriusmart <siriusmart@tuta.io>"] |
| 5 | edition = "2021" |
5 | edition = "2021" |
| 6 | description = "An aesthetically pleasing YouTube TUI written in Rust" |
6 | description = "An aesthetically pleasing YouTube TUI written in Rust" |
| @@ -16,10 +16,11 @@ exclude = ["/docs", "/LICENSE", "/threadcounter.sh", "/PKGBUILD"] |
| 16 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
16 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 17 | |
17 | |
| 18 | [features] |
18 | [features] |
| 19 | default = ["sixel", "halfblock", "clipboard"] |
19 | default = ["sixel", "halfblock", "clipboard", "mpv"] |
| 20 | sixel = ["viuer/sixel"] |
20 | sixel = ["viuer/sixel"] |
| 21 | halfblock = ["viuer"] |
21 | halfblock = ["viuer"] |
| 22 | clipboard = ["dep:clipboard"] |
22 | clipboard = ["dep:clipboard"] |
| |
23 | mpv = ["dep:libmpv"] |
| 23 | |
24 | |
| 24 | [dependencies] |
25 | [dependencies] |
| 25 | serde = {version = "1.0", default-features = false, features = ["derive"]} |
26 | serde = {version = "1.0", default-features = false, features = ["derive"]} |
| @@ -36,6 +37,7 @@ serde_json = {version = "1.0", default-features = false} |
| 36 | clipboard = {version = "0.5", optional = true} |
37 | clipboard = {version = "0.5", optional = true} |
| 37 | urlencoding = "2.1" |
38 | urlencoding = "2.1" |
| 38 | ratatui = {version = "0.21", default-features = false, features = ["serde"]} |
39 | ratatui = {version = "0.21", default-features = false, features = ["serde"]} |
| |
40 | libmpv = { git = "https://github.com/sudipghimire533/libmpv-rs", optional = true } |
| 39 | |
41 | |
| 40 | # tui-additions = {path = "../../Rust/Packages/tui-additions", default-features = false, features = ["framework", "widgets"]} |
42 | # tui-additions = {path = "../../Rust/Packages/tui-additions", default-features = false, features = ["framework", "widgets"]} |
| 41 | # invidious = {path = "../../Rust/Packages/invidious"} |
43 | # invidious = {path = "../../Rust/Packages/invidious"} |
docs/src/changelogs.md
+6
| @@ -1,5 +1,11 @@ |
| 1 | # Changelogs |
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 | ## v0.7.4 (latest stable on crates.io) |
9 | ## v0.7.4 (latest stable on crates.io) |
| 4 | |
10 | |
| 5 | ### Added |
11 | ### Added |
src/global/functions/run_command.rs
+23
| @@ -499,6 +499,29 @@ pub fn run_single_command( |
| 499 | let keymodifier = KeyModifiers::from_bits_truncate(modifier); |
499 | let keymodifier = KeyModifiers::from_bits_truncate(modifier); |
| 500 | key_input(KeyEvent::new(keycode, keymodifier), framework, terminal) |
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 | *framework.data.global.get_mut::<Message>().unwrap() = |
526 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 504 | Message::Error(format!("Unknown command: `{}`", command.join(" "))); |
527 | Message::Error(format!("Unknown command: `{}`", command.join(" "))); |
src/global/structs/mod.rs
+4
| @@ -5,6 +5,8 @@ mod item; |
| 5 | mod keyaction; |
5 | mod keyaction; |
| 6 | mod library; |
6 | mod library; |
| 7 | mod message; |
7 | mod message; |
| |
8 | #[cfg(feature = "mpv")] |
| |
9 | mod mpv; |
| 8 | mod page; |
10 | mod page; |
| 9 | mod state_env; |
11 | mod state_env; |
| 10 | mod status; |
12 | mod status; |
| @@ -18,6 +20,8 @@ pub use item::*; |
| 18 | pub use keyaction::*; |
20 | pub use keyaction::*; |
| 19 | pub use library::*; |
21 | pub use library::*; |
| 20 | pub use message::*; |
22 | pub use message::*; |
| |
23 | #[cfg(feature = "mpv")] |
| |
24 | pub use mpv::*; |
| 21 | pub use page::*; |
25 | pub use page::*; |
| 22 | pub use state_env::*; |
26 | pub use state_env::*; |
| 23 | pub use status::*; |
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 | use home::home_dir; |
5 | use home::home_dir; |
| 6 | use ratatui::{backend::CrosstermBackend, Terminal}; |
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 | use tui_additions::framework::{Framework, FrameworkClean}; |
12 | use tui_additions::framework::{Framework, FrameworkClean}; |
| 9 | |
13 | |
| 10 | /// app to run before the app starts |
14 | /// app to run before the app starts |
| @@ -49,11 +53,18 @@ pub fn init( |
| 49 | .insert::<Subscriptions>(Subscriptions::load()); |
53 | .insert::<Subscriptions>(Subscriptions::load()); |
| 50 | framework.data.global.insert::<Library>(Library::load()); |
54 | framework.data.global.insert::<Library>(Library::load()); |
| 51 | framework.data.global.insert::<Message>(Message::None); |
55 | framework.data.global.insert::<Message>(Message::None); |
| |
56 | |
| 52 | framework.data.global.insert::<Status>(Status { |
57 | framework.data.global.insert::<Status>(Status { |
| 53 | provider: framework.data.global.get::<MainConfig>().unwrap().provider, |
58 | provider: framework.data.global.get::<MainConfig>().unwrap().provider, |
| 54 | ..Status::default() |
59 | ..Status::default() |
| 55 | }); |
60 | }); |
| 56 | |
61 | |
| |
62 | #[cfg(feature = "mpv")] |
| |
63 | framework |
| |
64 | .data |
| |
65 | .global |
| |
66 | .insert::<MpvWrapper>(MpvWrapper::spawn()); |
| |
67 | |
| 57 | framework.data.state.insert::<Tasks>(Tasks::default()); |
68 | framework.data.state.insert::<Tasks>(Tasks::default()); |
| 58 | framework.data.state.insert::<Page>(Page::default()); |
69 | framework.data.state.insert::<Page>(Page::default()); |
| 59 | framework |
70 | framework |
src/items/searchbar.rs
+1 −1
| @@ -148,7 +148,7 @@ impl FrameworkItem for SearchBar { |
| 148 | return Ok(()); |
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 | search.query = self.text_field.content.clone(); |
152 | search.query = self.text_field.content.clone(); |
| 153 | let search = search.clone(); |
153 | let search = search.clone(); |
| 154 | let tasks = framework.data.state.get_mut::<Tasks>().unwrap(); |
154 | let tasks = framework.data.state.get_mut::<Tasks>().unwrap(); |
src/run.rs
+26 −1
| @@ -1,6 +1,11 @@ |
| 1 | use crossterm::event::{self, Event, MouseButton, MouseEventKind}; |
1 | use crossterm::event::{self, Event, MouseButton, MouseEventKind}; |
| 2 | use ratatui::{backend::CrosstermBackend, Terminal}; |
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 | use tui_additions::framework::Framework; |
9 | use tui_additions::framework::Framework; |
| 5 | |
10 | |
| 6 | use crate::{ |
11 | use crate::{ |
| @@ -14,6 +19,8 @@ pub fn run( |
| 14 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
19 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 15 | framework: &mut Framework, |
20 | framework: &mut Framework, |
| 16 | ) -> Result<(), Box<dyn Error>> { |
21 | ) -> Result<(), Box<dyn Error>> { |
| |
22 | let tick_rate = Duration::from_secs(1); |
| |
23 | let mut last_tick = Instant::now(); |
| 17 | loop { |
24 | loop { |
| 18 | // repeat forever until all tasks are ran (and Tasks is cleared) |
25 | // repeat forever until all tasks are ran (and Tasks is cleared) |
| 19 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { |
26 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { |
| @@ -33,6 +40,24 @@ pub fn run( |
| 33 | if framework.data.global.get::<Status>().unwrap().exit { |
40 | if framework.data.global.get::<Status>().unwrap().exit { |
| 34 | break; |
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 | *framework.data.global.get_mut::<Message>().unwrap() = Message::None; |
61 | *framework.data.global.get_mut::<Message>().unwrap() = Message::None; |
| 37 | |
62 | |
| 38 | match event::read()? { |
63 | match event::read()? { |