Skip to content

Commit

Permalink
Merge pull request #2 from Gobbel2000/improvements
Browse files Browse the repository at this point in the history
Send reply to browser and suppress output from mpv
  • Loading branch information
ryze312 authored Apr 9, 2023
2 parents 36fb89b + 3c9db73 commit d4a6334
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/browser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Deserialize;
use std::io;
use std::io::BufReader;
use std::io::Read;
use std::io::{Read, Write};

use crate::error::FF2MpvError;

Expand All @@ -10,6 +10,11 @@ pub struct FF2MpvMessage {
pub url: String,
}

pub fn send_reply() -> Result<(), io::Error> {
// "ok" formatted as a JSON string
send_message("\"ok\"")
}

pub fn get_mpv_message() -> Result<FF2MpvMessage, FF2MpvError> {
let message = read_message()?;
let ff2mpv_message = serde_json::from_str(&message)?;
Expand All @@ -28,3 +33,13 @@ fn read_message() -> Result<String, io::Error> {
reader.read_to_string(&mut string)?;
Ok(string)
}

fn send_message(message: &str) -> Result<(), io::Error> {
let length = (message.len() as u32).to_ne_bytes();
let message = message.as_bytes();

let mut stdout = io::stdout();
stdout.write_all(&length)?;
stdout.write_all(message)?;
Ok(())
}
3 changes: 3 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ impl Command {
let config = Config::build();
let ff2mpv_message = browser::get_mpv_message()?;
Command::launch_mpv(config.player_command, config.player_args, &ff2mpv_message.url)?;
browser::send_reply()?;

Ok(())
}

fn launch_mpv(command: String, args: Vec<String>, url: &str) -> Result<(), io::Error> {
process::Command::new(command)
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.args(args)
.arg(url)
.spawn()?;
Expand Down

0 comments on commit d4a6334

Please sign in to comment.