Run rustfmt on the entire crate
This commit is contained in:
parent
2911b9bb49
commit
b0a62f25eb
|
@ -1,5 +1,5 @@
|
||||||
use env_logger;
|
use env_logger;
|
||||||
use mpvipc::{Mpv, Error as MpvError};
|
use mpvipc::{Error as MpvError, Mpv};
|
||||||
|
|
||||||
fn main() -> Result<(), MpvError> {
|
fn main() -> Result<(), MpvError> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
use env_logger;
|
use env_logger;
|
||||||
use mpvipc::{
|
use mpvipc::{Error, Event, Mpv, MpvDataType, Property};
|
||||||
Error,
|
|
||||||
Event,
|
|
||||||
Mpv,
|
|
||||||
MpvDataType,
|
|
||||||
Property,
|
|
||||||
};
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
fn seconds_to_hms(total: f64) -> String {
|
fn seconds_to_hms(total: f64) -> String {
|
||||||
|
@ -32,39 +26,47 @@ fn main() -> Result<(), Error> {
|
||||||
loop {
|
loop {
|
||||||
let event = mpv.event_listen()?;
|
let event = mpv.event_listen()?;
|
||||||
match event {
|
match event {
|
||||||
Event::PropertyChange(property) => {
|
Event::PropertyChange(property) => match property {
|
||||||
match property {
|
Property::Path(Some(value)) => println!("\nPlaying: {}[K", value),
|
||||||
Property::Path(Some(value)) => println!("\nPlaying: {}[K", value),
|
Property::Path(None) => (),
|
||||||
Property::Path(None) => (),
|
Property::Pause(value) => pause = value,
|
||||||
Property::Pause(value) => pause = value,
|
Property::PlaybackTime(Some(value)) => playback_time = value,
|
||||||
Property::PlaybackTime(Some(value)) => playback_time = value,
|
Property::PlaybackTime(None) => playback_time = std::f64::NAN,
|
||||||
Property::PlaybackTime(None) => playback_time = std::f64::NAN,
|
Property::Duration(Some(value)) => duration = value,
|
||||||
Property::Duration(Some(value)) => duration = value,
|
Property::Duration(None) => duration = std::f64::NAN,
|
||||||
Property::Duration(None) => duration = std::f64::NAN,
|
Property::Metadata(Some(value)) => {
|
||||||
Property::Metadata(Some(value)) => {
|
println!("File tags:[K");
|
||||||
println!("File tags:[K");
|
if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
|
||||||
if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
|
println!(" Artist: {}[K", value);
|
||||||
println!(" Artist: {}[K", value);
|
}
|
||||||
}
|
if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
|
||||||
if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
|
println!(" Album: {}[K", value);
|
||||||
println!(" Album: {}[K", value);
|
}
|
||||||
}
|
if let Some(MpvDataType::String(value)) = value.get("TITLE") {
|
||||||
if let Some(MpvDataType::String(value)) = value.get("TITLE") {
|
println!(" Title: {}[K", value);
|
||||||
println!(" Title: {}[K", value);
|
}
|
||||||
}
|
if let Some(MpvDataType::String(value)) = value.get("TRACK") {
|
||||||
if let Some(MpvDataType::String(value)) = value.get("TRACK") {
|
println!(" Track: {}[K", value);
|
||||||
println!(" Track: {}[K", value);
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
Property::Metadata(None) => (),
|
|
||||||
Property::Unknown { name: _, id: _, data: _ } => (),
|
|
||||||
}
|
}
|
||||||
|
Property::Metadata(None) => (),
|
||||||
|
Property::Unknown {
|
||||||
|
name: _,
|
||||||
|
id: _,
|
||||||
|
data: _,
|
||||||
|
} => (),
|
||||||
},
|
},
|
||||||
Event::Shutdown => return Ok(()),
|
Event::Shutdown => return Ok(()),
|
||||||
Event::Unimplemented => panic!("Unimplemented event"),
|
Event::Unimplemented => panic!("Unimplemented event"),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
print!("{}{} / {} ({:.0}%)[K\r", if pause { "(Paused) " } else { "" }, seconds_to_hms(playback_time), seconds_to_hms(duration), 100. * playback_time / duration);
|
print!(
|
||||||
|
"{}{} / {} ({:.0}%)[K\r",
|
||||||
|
if pause { "(Paused) " } else { "" },
|
||||||
|
seconds_to_hms(playback_time),
|
||||||
|
seconds_to_hms(duration),
|
||||||
|
100. * playback_time / duration
|
||||||
|
);
|
||||||
io::stdout().flush().unwrap();
|
io::stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
src/ipc.rs
13
src/ipc.rs
|
@ -1,10 +1,10 @@
|
||||||
|
use super::*;
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
use serde_json::{self, Value};
|
use serde_json::{self, Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::BufReader;
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
use std::io::BufReader;
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PlaylistEntry {
|
pub struct PlaylistEntry {
|
||||||
|
@ -260,8 +260,7 @@ pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<(
|
||||||
pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Result<(), Error> {
|
pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Result<(), Error> {
|
||||||
let ipc_string = format!(
|
let ipc_string = format!(
|
||||||
"{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n",
|
"{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n",
|
||||||
id,
|
id, property
|
||||||
property
|
|
||||||
);
|
);
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
||||||
Ok(feedback) => {
|
Ok(feedback) => {
|
||||||
|
@ -307,7 +306,11 @@ fn try_convert_property(name: &str, id: isize, data: MpvDataType) -> Event {
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
warn!("Property {} not implemented", name);
|
warn!("Property {} not implemented", name);
|
||||||
Property::Unknown { name: name.to_string(), id, data }
|
Property::Unknown {
|
||||||
|
name: name.to_string(),
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Event::PropertyChange(property)
|
Event::PropertyChange(property)
|
||||||
|
|
170
src/lib.rs
170
src/lib.rs
|
@ -3,8 +3,8 @@ pub mod ipc;
|
||||||
use ipc::*;
|
use ipc::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
use std::io::{BufReader, Read};
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use std::io::{Read, BufReader};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
|
@ -132,9 +132,7 @@ impl Drop for Mpv {
|
||||||
|
|
||||||
impl fmt::Debug for Mpv {
|
impl fmt::Debug for Mpv {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt.debug_tuple("Mpv")
|
fmt.debug_tuple("Mpv").field(&self.name).finish()
|
||||||
.field(&self.name)
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,9 +183,7 @@ impl Display for ErrorCode {
|
||||||
f.write_str("The received value is not of type \'std::f64\'")
|
f.write_str("The received value is not of type \'std::f64\'")
|
||||||
}
|
}
|
||||||
ErrorCode::ValueDoesNotContainHashMap => {
|
ErrorCode::ValueDoesNotContainHashMap => {
|
||||||
f.write_str(
|
f.write_str("The received value is not of type \'std::collections::HashMap\'")
|
||||||
"The received value is not of type \'std::collections::HashMap\'",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
ErrorCode::ValueDoesNotContainPlaylist => {
|
ErrorCode::ValueDoesNotContainPlaylist => {
|
||||||
f.write_str("The received value is not of type \'mpvipc::Playlist\'")
|
f.write_str("The received value is not of type \'mpvipc::Playlist\'")
|
||||||
|
@ -290,9 +286,9 @@ impl Mpv {
|
||||||
|
|
||||||
pub fn disconnect(&self) {
|
pub fn disconnect(&self) {
|
||||||
let mut stream = &self.stream;
|
let mut stream = &self.stream;
|
||||||
stream.shutdown(std::net::Shutdown::Both).expect(
|
stream
|
||||||
"socket disconnect",
|
.shutdown(std::net::Shutdown::Both)
|
||||||
);
|
.expect("socket disconnect");
|
||||||
let mut buffer = [0; 32];
|
let mut buffer = [0; 32];
|
||||||
for _ in 0..stream.bytes().count() {
|
for _ in 0..stream.bytes().count() {
|
||||||
stream.read(&mut buffer[..]).unwrap();
|
stream.read(&mut buffer[..]).unwrap();
|
||||||
|
@ -449,33 +445,25 @@ impl Mpv {
|
||||||
option: PlaylistAddOptions,
|
option: PlaylistAddOptions,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match file_type {
|
match file_type {
|
||||||
PlaylistAddTypeOptions::File => {
|
PlaylistAddTypeOptions::File => match option {
|
||||||
match option {
|
PlaylistAddOptions::Replace => {
|
||||||
PlaylistAddOptions::Replace => {
|
run_mpv_command(self, "loadfile", &[file, "replace"])
|
||||||
run_mpv_command(self, "loadfile", &[file, "replace"])
|
|
||||||
}
|
|
||||||
PlaylistAddOptions::Append => {
|
|
||||||
run_mpv_command(self, "loadfile", &[file, "append"])
|
|
||||||
}
|
|
||||||
PlaylistAddOptions::AppendPlay => {
|
|
||||||
run_mpv_command(self, "loadfile", &[file, "append-play"])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
PlaylistAddOptions::Append => run_mpv_command(self, "loadfile", &[file, "append"]),
|
||||||
|
PlaylistAddOptions::AppendPlay => {
|
||||||
|
run_mpv_command(self, "loadfile", &[file, "append-play"])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
PlaylistAddTypeOptions::Playlist => {
|
PlaylistAddTypeOptions::Playlist => match option {
|
||||||
match option {
|
PlaylistAddOptions::Replace => {
|
||||||
PlaylistAddOptions::Replace => {
|
run_mpv_command(self, "loadlist", &[file, "replace"])
|
||||||
run_mpv_command(self, "loadlist", &[file, "replace"])
|
|
||||||
}
|
|
||||||
PlaylistAddOptions::Append |
|
|
||||||
PlaylistAddOptions::AppendPlay => {
|
|
||||||
run_mpv_command(self, "loadlist", &[file, "append"])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
PlaylistAddOptions::Append | PlaylistAddOptions::AppendPlay => {
|
||||||
|
run_mpv_command(self, "loadlist", &[file, "append"])
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn playlist_clear(&self) -> Result<(), Error> {
|
pub fn playlist_clear(&self) -> Result<(), Error> {
|
||||||
|
@ -492,13 +480,11 @@ impl Mpv {
|
||||||
|
|
||||||
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
|
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
|
||||||
match get_mpv_property::<usize>(self, "playlist-pos") {
|
match get_mpv_property::<usize>(self, "playlist-pos") {
|
||||||
Ok(current_id) => {
|
Ok(current_id) => run_mpv_command(
|
||||||
run_mpv_command(
|
self,
|
||||||
self,
|
"playlist-move",
|
||||||
"playlist-move",
|
&[&id.to_string(), &(current_id + 1).to_string()],
|
||||||
&[&id.to_string(), &(current_id + 1).to_string()],
|
),
|
||||||
)
|
|
||||||
}
|
|
||||||
Err(msg) => Err(msg),
|
Err(msg) => Err(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -533,21 +519,17 @@ impl Mpv {
|
||||||
match option {
|
match option {
|
||||||
Switch::On => enabled = true,
|
Switch::On => enabled = true,
|
||||||
Switch::Off => {}
|
Switch::Off => {}
|
||||||
Switch::Toggle => {
|
Switch::Toggle => match get_mpv_property_string(self, "loop-file") {
|
||||||
match get_mpv_property_string(self, "loop-file") {
|
Ok(value) => match value.as_ref() {
|
||||||
Ok(value) => {
|
"false" => {
|
||||||
match value.as_ref() {
|
enabled = true;
|
||||||
"false" => {
|
|
||||||
enabled = true;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
enabled = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(msg) => return Err(msg),
|
_ => {
|
||||||
}
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Err(msg) => return Err(msg),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "loop-file", enabled)
|
set_mpv_property(self, "loop-file", enabled)
|
||||||
}
|
}
|
||||||
|
@ -557,21 +539,17 @@ impl Mpv {
|
||||||
match option {
|
match option {
|
||||||
Switch::On => enabled = true,
|
Switch::On => enabled = true,
|
||||||
Switch::Off => {}
|
Switch::Off => {}
|
||||||
Switch::Toggle => {
|
Switch::Toggle => match get_mpv_property_string(self, "loop-playlist") {
|
||||||
match get_mpv_property_string(self, "loop-playlist") {
|
Ok(value) => match value.as_ref() {
|
||||||
Ok(value) => {
|
"false" => {
|
||||||
match value.as_ref() {
|
enabled = true;
|
||||||
"false" => {
|
|
||||||
enabled = true;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
enabled = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(msg) => return Err(msg),
|
_ => {
|
||||||
}
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Err(msg) => return Err(msg),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "loop-playlist", enabled)
|
set_mpv_property(self, "loop-playlist", enabled)
|
||||||
}
|
}
|
||||||
|
@ -581,14 +559,12 @@ impl Mpv {
|
||||||
match option {
|
match option {
|
||||||
Switch::On => enabled = true,
|
Switch::On => enabled = true,
|
||||||
Switch::Off => {}
|
Switch::Off => {}
|
||||||
Switch::Toggle => {
|
Switch::Toggle => match get_mpv_property::<bool>(self, "mute") {
|
||||||
match get_mpv_property::<bool>(self, "mute") {
|
Ok(value) => {
|
||||||
Ok(value) => {
|
enabled = !value;
|
||||||
enabled = !value;
|
|
||||||
}
|
|
||||||
Err(msg) => return Err(msg),
|
|
||||||
}
|
}
|
||||||
}
|
Err(msg) => return Err(msg),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "mute", enabled)
|
set_mpv_property(self, "mute", enabled)
|
||||||
}
|
}
|
||||||
|
@ -627,38 +603,34 @@ impl Mpv {
|
||||||
|
|
||||||
pub fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), Error> {
|
pub fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), Error> {
|
||||||
match get_mpv_property::<f64>(self, "speed") {
|
match get_mpv_property::<f64>(self, "speed") {
|
||||||
Ok(speed) => {
|
Ok(speed) => match option {
|
||||||
match option {
|
NumberChangeOptions::Increase => {
|
||||||
NumberChangeOptions::Increase => {
|
set_mpv_property(self, "speed", speed + input_speed)
|
||||||
set_mpv_property(self, "speed", speed + input_speed)
|
|
||||||
}
|
|
||||||
|
|
||||||
NumberChangeOptions::Decrease => {
|
|
||||||
set_mpv_property(self, "speed", speed - input_speed)
|
|
||||||
}
|
|
||||||
|
|
||||||
NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
NumberChangeOptions::Decrease => {
|
||||||
|
set_mpv_property(self, "speed", speed - input_speed)
|
||||||
|
}
|
||||||
|
|
||||||
|
NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed),
|
||||||
|
},
|
||||||
Err(msg) => Err(msg),
|
Err(msg) => Err(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), Error> {
|
pub fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), Error> {
|
||||||
match get_mpv_property::<f64>(self, "volume") {
|
match get_mpv_property::<f64>(self, "volume") {
|
||||||
Ok(volume) => {
|
Ok(volume) => match option {
|
||||||
match option {
|
NumberChangeOptions::Increase => {
|
||||||
NumberChangeOptions::Increase => {
|
set_mpv_property(self, "volume", volume + input_volume)
|
||||||
set_mpv_property(self, "volume", volume + input_volume)
|
|
||||||
}
|
|
||||||
|
|
||||||
NumberChangeOptions::Decrease => {
|
|
||||||
set_mpv_property(self, "volume", volume - input_volume)
|
|
||||||
}
|
|
||||||
|
|
||||||
NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
NumberChangeOptions::Decrease => {
|
||||||
|
set_mpv_property(self, "volume", volume - input_volume)
|
||||||
|
}
|
||||||
|
|
||||||
|
NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume),
|
||||||
|
},
|
||||||
Err(msg) => Err(msg),
|
Err(msg) => Err(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue