Commit 77ac8db6fa

77ac8db6fa5a8a4fad3431b55f2c0b4018168bb0

parent: 4649ab8f18

Unsigned

Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2023-08-19 14:22 UTC

mpv commands (with additional prop and sprop) #38

Layout: unified · split

src/global/functions/run_command.rs +41 −22
@@ -499,29 +499,48 @@ 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")] 502 #[cfg(feature = "mpv")]
510 // ["mpv", name, ..] => framework.data.global.get::<Status>().unwrap().mpv.command( 503 ["mpv", "property", property] | ["mpv", "prop", property] => {
511 // name.to_string(), 504 let res = framework
512 // command[2..].into_iter().map(|s| s.to_string()).collect(), 505 .data
513 // ), 506 .global
514 ["mpv", name, ..] => framework 507 .get::<MpvWrapper>()
515 .data 508 .unwrap()
516 .global 509 .property(property.to_string());
517 .get::<MpvWrapper>() 510
518 .unwrap() 511 *framework.data.global.get_mut::<Message>().unwrap() = match res {
519 .sender 512 Some(value) => Message::Message(format!("Value: {value}")),
520 .send(MpvAction::Command { 513 None => Message::Error("No such property".to_string()),
521 name: name.to_string(), 514 };
522 args: command[2..].iter().map(|s| s.to_string()).collect(), 515 }
523 }) 516 #[cfg(feature = "mpv")]
524 .unwrap(), 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 *framework.data.global.get_mut::<Message>().unwrap() = 545 *framework.data.global.get_mut::<Message>().unwrap() =
527 Message::Error(format!("Unknown command: `{}`", command.join(" "))); 546 Message::Error(format!("Unknown command: `{}`", command.join(" ")));
src/global/structs/message.rs +4 −1
@@ -7,6 +7,7 @@ pub enum Message {
7 Message(String), 7 Message(String),
8 Error(String), 8 Error(String),
9 Success(String), 9 Success(String),
10 Mpv(String),
10 None, 11 None,
11} 12}
12 13
@@ -18,7 +19,9 @@ impl Message {
18 // to_string is used by the message bar to convert Message to string 19 // to_string is used by the message bar to convert Message to string
19 pub fn to_string(&self, default: &str) -> String { 20 pub fn to_string(&self, default: &str) -> String {
20 match self { 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 Self::None => default.to_string(), 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
6pub struct MpvWrapper { 6pub struct MpvWrapper {
7 pub sender: mpsc::Sender<MpvAction>, 7 pub sender: mpsc::Sender<MpvAction>,
8 pub copier: mpsc::Receiver<MpvResponse>, 8 // pub copier: mpsc::Receiver<MpvResponse>,
9} 9}
10 10
11impl Clone for MpvWrapper { 11impl Clone for MpvWrapper {
@@ -23,7 +23,7 @@ impl Clone for MpvWrapper {
23impl MpvWrapper { 23impl MpvWrapper {
24 pub fn spawn() -> Self { 24 pub fn spawn() -> Self {
25 let (sender, receiver) = mpsc::channel(); 25 let (sender, receiver) = mpsc::channel();
26 let (responder, copier) = mpsc::channel(); 26 // let (responder, copier) = mpsc::channel();
27 thread::spawn(move || { 27 thread::spawn(move || {
28 let mpv = Mpv::new().unwrap(); 28 let mpv = Mpv::new().unwrap();
29 mpv.set_property("video", "no").unwrap(); 29 mpv.set_property("video", "no").unwrap();
@@ -33,31 +33,101 @@ impl MpvWrapper {
33 Err(_) => continue, 33 Err(_) => continue,
34 }; 34 };
35 match action { 35 match action {
36 MpvAction::Command { name, args } => match mpv 36 MpvAction::Command {
37 name,
38 args,
39 responder,
40 } => match mpv
37 .command(&name, &args.iter().map(|s| s.as_str()).collect::<Vec<_>>()) 41 .command(&name, &args.iter().map(|s| s.as_str()).collect::<Vec<_>>())
38 { 42 {
39 Ok(_) => responder.send(MpvResponse::Copy).unwrap(), 43 Ok(_) => responder.send(MpvResponse::Copy).unwrap(),
40 Err(e) => responder.send(MpvResponse::Error(e.to_string())).unwrap(), 44 Err(e) => responder.send(MpvResponse::Error(e.to_string())).unwrap(),
41 }, 45 },
42 MpvAction::RequestI64 { name } => responder 46 MpvAction::GetProperty { name, responder } => responder
43 .send(MpvResponse::I64(mpv.get_property::<i64>(&name).ok())) 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 .unwrap(), 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#[derive(Debug)] 110#[derive(Debug)]
53pub enum MpvAction { 111pub enum MpvAction {
54 Command { name: String, args: Vec<String> }, 112 Command {
55 RequestI64 { name: String }, 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
58pub enum MpvResponse { 128pub enum MpvResponse {
59 Copy, 129 Copy,
60 I64(Option<i64>), 130 Property(Option<String>),
61 Error(String), 131 Error(String),
62} 132}
63 133
src/init.rs +2 −2
@@ -6,8 +6,8 @@ use home::home_dir;
6use ratatui::{backend::CrosstermBackend, Terminal}; 6use ratatui::{backend::CrosstermBackend, Terminal};
7use std::{ 7use std::{
8 error::Error, 8 error::Error,
9 fs::{self, OpenOptions}, 9 fs::{self},
10 io::{Stdout, Write}, 10 io::Stdout,
11}; 11};
12use tui_additions::framework::{Framework, FrameworkClean}; 12use tui_additions::framework::{Framework, FrameworkClean};
13 13
src/items/messagebar.rs +1 −1
@@ -45,7 +45,7 @@ impl FrameworkItem for MessageBar {
45 Message::None => appearance.colors.outline, 45 Message::None => appearance.colors.outline,
46 Message::Success(_) => appearance.colors.message_success_outline, 46 Message::Success(_) => appearance.colors.message_success_outline,
47 Message::Error(_) => appearance.colors.message_error_outline, 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