Commit 77ac8db6fa
Unsigned
Layout: unified · split
src/global/functions/run_command.rs +41 −22
| @@ -499,29 +499,48 @@ 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 | 502 | #[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(), | |
| 503 | ["mpv", "property", property] | ["mpv", "prop", property] => { | |
| 504 | let res = framework | |
| 505 | .data | |
| 506 | .global | |
| 507 | .get::<MpvWrapper>() | |
| 508 | .unwrap() | |
| 509 | .property(property.to_string()); | |
| 510 | ||
| 511 | *framework.data.global.get_mut::<Message>().unwrap() = match res { | |
| 512 | Some(value) => Message::Message(format!("Value: {value}")), | |
| 513 | None => Message::Error("No such property".to_string()), | |
| 514 | }; | |
| 515 | } | |
| 516 | #[cfg(feature = "mpv")] | |
| 517 | ["mpv", "set_property", name, value] | ["mpv", "sprop", name, value] => { | |
| 518 | let res = framework | |
| 519 | .data | |
| 520 | .global | |
| 521 | .get::<MpvWrapper>() | |
| 522 | .unwrap() | |
| 523 | .set_property(name.to_string(), value.to_string()); | |
| 524 | ||
| 525 | *framework.data.global.get_mut::<Message>().unwrap() = match res { | |
| 526 | MpvResponse::Copy => Message::Mpv("Value set".to_string()), | |
| 527 | MpvResponse::Error(e) => Message::Error(format!("MPV error: {e}")), | |
| 528 | _ => unreachable!(), | |
| 529 | }; | |
| 530 | } | |
| 531 | #[cfg(feature = "mpv")] | |
| 532 | ["mpv", name, ..] => { | |
| 533 | let res = framework.data.global.get::<MpvWrapper>().unwrap().command( | |
| 534 | name.to_string(), | |
| 535 | command[2..].iter().map(|s| s.to_string()).collect(), | |
| 536 | ); | |
| 537 | ||
| 538 | *framework.data.global.get_mut::<Message>().unwrap() = match res { | |
| 539 | MpvResponse::Copy => Message::Mpv("MPV player OK.".to_string()), | |
| 540 | MpvResponse::Error(e) => Message::Error(format!("MPV error: {e}")), | |
| 541 | _ => unreachable!(), | |
| 542 | }; | |
| 543 | } | |
| 525 | 544 | _ => { |
| 526 | 545 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 527 | 546 | Message::Error(format!("Unknown command: `{}`", command.join(" "))); |
src/global/structs/message.rs +4 −1
| @@ -7,6 +7,7 @@ pub enum Message { | ||
| 7 | 7 | Message(String), |
| 8 | 8 | Error(String), |
| 9 | 9 | Success(String), |
| 10 | Mpv(String), | |
| 10 | 11 | None, |
| 11 | 12 | } |
| 12 | 13 | |
| @@ -18,7 +19,9 @@ impl Message { | ||
| 18 | 19 | // to_string is used by the message bar to convert Message to string |
| 19 | 20 | pub fn to_string(&self, default: &str) -> String { |
| 20 | 21 | match self { |
| 21 | Self::Message(msg) | Self::Error(msg) | Self::Success(msg) => msg.to_string(), | |
| 22 | Self::Message(msg) | Self::Error(msg) | Self::Success(msg) | Self::Mpv(msg) => { | |
| 23 | msg.to_string() | |
| 24 | } | |
| 22 | 25 | Self::None => default.to_string(), |
| 23 | 26 | } |
| 24 | 27 | } |
src/global/structs/mpv.rs +79 −9
| @@ -5,7 +5,7 @@ use typemap::Key; | ||
| 5 | 5 | |
| 6 | 6 | pub struct MpvWrapper { |
| 7 | 7 | pub sender: mpsc::Sender<MpvAction>, |
| 8 | pub copier: mpsc::Receiver<MpvResponse>, | |
| 8 | // pub copier: mpsc::Receiver<MpvResponse>, | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | impl Clone for MpvWrapper { |
| @@ -23,7 +23,7 @@ impl Clone for MpvWrapper { | ||
| 23 | 23 | impl MpvWrapper { |
| 24 | 24 | pub fn spawn() -> Self { |
| 25 | 25 | let (sender, receiver) = mpsc::channel(); |
| 26 | let (responder, copier) = mpsc::channel(); | |
| 26 | // let (responder, copier) = mpsc::channel(); | |
| 27 | 27 | thread::spawn(move || { |
| 28 | 28 | let mpv = Mpv::new().unwrap(); |
| 29 | 29 | mpv.set_property("video", "no").unwrap(); |
| @@ -33,31 +33,101 @@ impl MpvWrapper { | ||
| 33 | 33 | Err(_) => continue, |
| 34 | 34 | }; |
| 35 | 35 | match action { |
| 36 | MpvAction::Command { name, args } => match mpv | |
| 36 | MpvAction::Command { | |
| 37 | name, | |
| 38 | args, | |
| 39 | responder, | |
| 40 | } => match mpv | |
| 37 | 41 | .command(&name, &args.iter().map(|s| s.as_str()).collect::<Vec<_>>()) |
| 38 | 42 | { |
| 39 | 43 | Ok(_) => responder.send(MpvResponse::Copy).unwrap(), |
| 40 | 44 | Err(e) => responder.send(MpvResponse::Error(e.to_string())).unwrap(), |
| 41 | 45 | }, |
| 42 | MpvAction::RequestI64 { name } => responder | |
| 43 | .send(MpvResponse::I64(mpv.get_property::<i64>(&name).ok())) | |
| 46 | MpvAction::GetProperty { name, responder } => responder | |
| 47 | .send(MpvResponse::Property( | |
| 48 | mpv.get_property::<String>(&name).ok(), | |
| 49 | )) | |
| 50 | .unwrap(), | |
| 51 | MpvAction::SetProperty { | |
| 52 | name, | |
| 53 | value, | |
| 54 | responder, | |
| 55 | } => responder | |
| 56 | .send(match mpv.set_property(&name, value) { | |
| 57 | Ok(_) => MpvResponse::Copy, | |
| 58 | Err(e) => MpvResponse::Error(e.to_string()), | |
| 59 | }) | |
| 44 | 60 | .unwrap(), |
| 45 | 61 | } |
| 46 | 62 | } |
| 47 | 63 | }); |
| 48 | Self { sender, copier } | |
| 64 | Self { sender } | |
| 65 | } | |
| 66 | ||
| 67 | pub fn command(&self, name: String, args: Vec<String>) -> MpvResponse { | |
| 68 | let (tx, rx) = mpsc::channel(); | |
| 69 | self.sender | |
| 70 | .send(MpvAction::Command { | |
| 71 | name, | |
| 72 | args, | |
| 73 | responder: tx, | |
| 74 | }) | |
| 75 | .unwrap(); | |
| 76 | ||
| 77 | rx.recv().unwrap() | |
| 78 | } | |
| 79 | ||
| 80 | pub fn property(&self, prop: String) -> Option<String> { | |
| 81 | let (tx, rx) = mpsc::channel(); | |
| 82 | self.sender | |
| 83 | .send(MpvAction::GetProperty { | |
| 84 | name: prop, | |
| 85 | responder: tx, | |
| 86 | }) | |
| 87 | .unwrap(); | |
| 88 | ||
| 89 | if let MpvResponse::Property(p) = rx.recv().unwrap() { | |
| 90 | p | |
| 91 | } else { | |
| 92 | unreachable!() | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | pub fn set_property(&self, name: String, value: String) -> MpvResponse { | |
| 97 | let (tx, rx) = mpsc::channel(); | |
| 98 | self.sender | |
| 99 | .send(MpvAction::SetProperty { | |
| 100 | name, | |
| 101 | value, | |
| 102 | responder: tx, | |
| 103 | }) | |
| 104 | .unwrap(); | |
| 105 | ||
| 106 | rx.recv().unwrap() | |
| 49 | 107 | } |
| 50 | 108 | } |
| 51 | 109 | |
| 52 | 110 | #[derive(Debug)] |
| 53 | 111 | pub enum MpvAction { |
| 54 | Command { name: String, args: Vec<String> }, | |
| 55 | RequestI64 { name: String }, | |
| 112 | Command { | |
| 113 | name: String, | |
| 114 | args: Vec<String>, | |
| 115 | responder: mpsc::Sender<MpvResponse>, | |
| 116 | }, | |
| 117 | GetProperty { | |
| 118 | name: String, | |
| 119 | responder: mpsc::Sender<MpvResponse>, | |
| 120 | }, | |
| 121 | SetProperty { | |
| 122 | name: String, | |
| 123 | value: String, | |
| 124 | responder: mpsc::Sender<MpvResponse>, | |
| 125 | }, | |
| 56 | 126 | } |
| 57 | 127 | |
| 58 | 128 | pub enum MpvResponse { |
| 59 | 129 | Copy, |
| 60 | I64(Option<i64>), | |
| 130 | Property(Option<String>), | |
| 61 | 131 | Error(String), |
| 62 | 132 | } |
| 63 | 133 | |
src/init.rs +2 −2
| @@ -6,8 +6,8 @@ use home::home_dir; | ||
| 6 | 6 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 7 | 7 | use std::{ |
| 8 | 8 | error::Error, |
| 9 | fs::{self, OpenOptions}, | |
| 10 | io::{Stdout, Write}, | |
| 9 | fs::{self}, | |
| 10 | io::Stdout, | |
| 11 | 11 | }; |
| 12 | 12 | use tui_additions::framework::{Framework, FrameworkClean}; |
| 13 | 13 | |
src/items/messagebar.rs +1 −1
| @@ -45,7 +45,7 @@ impl FrameworkItem for MessageBar { | ||
| 45 | 45 | Message::None => appearance.colors.outline, |
| 46 | 46 | Message::Success(_) => appearance.colors.message_success_outline, |
| 47 | 47 | Message::Error(_) => appearance.colors.message_error_outline, |
| 48 | Message::Message(_) => appearance.colors.message_outline, | |
| 48 | Message::Message(_) | Message::Mpv(_) => appearance.colors.message_outline, | |
| 49 | 49 | } |
| 50 | 50 | })); |
| 51 | 51 | |