From 1c75a7cedbf7baa5531a287b1d1279140777ef76 Mon Sep 17 00:00:00 2001
From: h7x4 <h7x4@nani.wtf>
Date: Sat, 4 May 2024 00:06:43 +0200
Subject: [PATCH] fix examples and documentation

---
 Cargo.toml               | 11 +++--
 README.md                | 46 +++++++--------------
 examples/fetch_state.rs  |  6 ++-
 examples/media_player.rs | 87 +++++++++++++++++++---------------------
 4 files changed, 68 insertions(+), 82 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 74b22ad..99e9fae 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,12 +1,15 @@
 [package]
 name = "mpvipc"
 version = "1.3.0"
-authors = ["Jonas Frei <freijon@pm.me>"]
+authors = [
+  "Jonas Frei <freijon@pm.me>",
+  "h7x4 <h7x4@nani.wtf>"
+]
 description = "A small library which provides bindings to control existing mpv instances through sockets."
 license = "GPL-3.0"
-homepage = "https://gitlab.com/mpv-ipc/mpvipc"
-repository = "https://gitlab.com/mpv-ipc/mpvipc"
-documentation = "https://docs.rs/mpvipc/"
+homepage = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
+repository = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
+documentation = "https://pvv.ntnu.no/~oysteikt/gitea/mpvipc/master/docs/mpvipc/"
 edition = "2021"
 rust-version = "1.75"
 
diff --git a/README.md b/README.md
index 97c98cb..89d797f 100644
--- a/README.md
+++ b/README.md
@@ -5,46 +5,30 @@
 
 A small library which provides bindings to control existing mpv instances through sockets.
 
-To make use of this library, please make sure mpv is started with the following option:
-`
-$ mpv --input-ipc-server=/tmp/mpv.sock --idle ...
-`
-
 ## Dependencies
 
 - `mpv`
-- `cargo` (makedep)
-
-## Install
-
-- [Cargo](https://crates.io/crates/mpvipc)
-
-You can use this package with cargo.
+- `cargo` (make dependency)
+- `cargo-nextest` (test depencency)
+- `grcov` (test depencency)
 
 ## Example
 
 Make sure mpv is started with the following option:
-`
+
+```bash
 $ mpv --input-ipc-server=/tmp/mpv.sock --idle
-`
-
-Here is a small code example which connects to the socket /tmp/mpv.sock and toggles playback.
-
-```rust
-extern crate mpvipc;
-
-use mpvipc::*;
-use std::sync::mpsc::channel;
-
-fn main() {
-    let mpv = Mpv::connect("/tmp/mpv.sock").unwrap();
-    let paused: bool = mpv.get_property("pause").unwrap();
-    mpv.set_property("pause", !paused).expect("Error pausing");
-}
 ```
 
-For a more extensive example and proof of concept, see project [mpvc](https://gitlab.com/mpv-ipc/mpvc).
+Here is a small code example which connects to the socket `/tmp/mpv.sock` and toggles playback.
 
-## Bugs / Ideas
+```rust
+use mpvipc::*;
 
-Check out the [Issue Tracker](https://gitlab.com/mpv-ipc/mpvipc/issues)
+#[tokio::main]
+async fn main() -> Result<(), MpvError> {
+    let mpv = Mpv::connect("/tmp/mpv.sock").await?;
+    let paused: bool = mpv.get_property("pause").await?;
+    mpv.set_property("pause", !paused).expect("Error pausing");
+}
+```
\ No newline at end of file
diff --git a/examples/fetch_state.rs b/examples/fetch_state.rs
index a4fbcb1..7a47029 100644
--- a/examples/fetch_state.rs
+++ b/examples/fetch_state.rs
@@ -1,15 +1,19 @@
-use mpvipc::{MpvError, Mpv, MpvExt};
+use mpvipc::{Mpv, MpvError, MpvExt};
 
 #[tokio::main]
 async fn main() -> Result<(), MpvError> {
     env_logger::init();
 
     let mpv = Mpv::connect("/tmp/mpv.sock").await?;
+
     let meta = mpv.get_metadata().await?;
     println!("metadata: {:?}", meta);
+
     let playlist = mpv.get_playlist().await?;
     println!("playlist: {:?}", playlist);
+
     let playback_time: f64 = mpv.get_property("playback-time").await?;
     println!("playback-time: {}", playback_time);
+
     Ok(())
 }
diff --git a/examples/media_player.rs b/examples/media_player.rs
index 9def21a..2f394de 100644
--- a/examples/media_player.rs
+++ b/examples/media_player.rs
@@ -1,4 +1,5 @@
-use mpvipc::{MpvError, Mpv, MpvExt};
+use futures::StreamExt;
+use mpvipc::{parse_event_property, Event, Mpv, MpvDataType, MpvError, MpvExt, Property};
 
 fn seconds_to_hms(total: f64) -> String {
     let total = total as u64;
@@ -14,55 +15,49 @@ async fn main() -> Result<(), MpvError> {
     env_logger::init();
 
     let mpv = Mpv::connect("/tmp/mpv.sock").await?;
-    let pause = false;
-    let playback_time = std::f64::NAN;
-    let duration = std::f64::NAN;
+
     mpv.observe_property(1, "path").await?;
     mpv.observe_property(2, "pause").await?;
     mpv.observe_property(3, "playback-time").await?;
     mpv.observe_property(4, "duration").await?;
     mpv.observe_property(5, "metadata").await?;
-    loop {
-        // TODO:
-        //     let event = mpv.event_listen()?;
-        //     match event {
-        //         Event::PropertyChange { id: _, property } => match property {
-        //             Property::Path(Some(value)) => println!("\nPlaying: {}", value),
-        //             Property::Path(None) => (),
-        //             Property::Pause(value) => pause = value,
-        //             Property::PlaybackTime(Some(value)) => playback_time = value,
-        //             Property::PlaybackTime(None) => playback_time = std::f64::NAN,
-        //             Property::Duration(Some(value)) => duration = value,
-        //             Property::Duration(None) => duration = std::f64::NAN,
-        //             Property::Metadata(Some(value)) => {
-        //                 println!("File tags:");
-        //                 if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
-        //                     println!(" Artist: {}", value);
-        //                 }
-        //                 if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
-        //                     println!(" Album: {}", value);
-        //                 }
-        //                 if let Some(MpvDataType::String(value)) = value.get("TITLE") {
-        //                     println!(" Title: {}", value);
-        //                 }
-        //                 if let Some(MpvDataType::String(value)) = value.get("TRACK") {
-        //                     println!(" Track: {}", value);
-        //                 }
-        //             }
-        //             Property::Metadata(None) => (),
-        //             Property::Unknown { name: _, data: _ } => (),
-        //         },
-        //         Event::Shutdown => return Ok(()),
-        //         Event::Unimplemented => panic!("Unimplemented event"),
-        //         _ => (),
-        //     }
-        //     print!(
-        //         "{}{} / {} ({:.0}%)\r",
-        //         if pause { "(Paused) " } else { "" },
-        //         seconds_to_hms(playback_time),
-        //         seconds_to_hms(duration),
-        //         100. * playback_time / duration
-        //     );
-        //     io::stdout().flush().unwrap();
+
+    let mut events = mpv.get_event_stream().await;
+    while let Some(Ok(event)) = events.next().await {
+        match event {
+            mpvipc::Event::PropertyChange { .. } => match parse_event_property(event)? {
+                (1, Property::Path(Some(value))) => println!("\nPlaying: {}", value),
+                (2, Property::Pause(value)) => {
+                    println!("Pause: {}", value);
+                }
+                (3, Property::PlaybackTime(Some(value))) => {
+                    println!("Playback time: {}", seconds_to_hms(value));
+                }
+                (4, Property::Duration(Some(value))) => {
+                    println!("Duration: {}", seconds_to_hms(value));
+                }
+                (5, Property::Metadata(Some(value))) => {
+                    println!("File tags:");
+                    if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
+                        println!(" Artist: {}", value);
+                    }
+                    if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
+                        println!(" Album: {}", value);
+                    }
+                    if let Some(MpvDataType::String(value)) = value.get("TITLE") {
+                        println!(" Title: {}", value);
+                    }
+                    if let Some(MpvDataType::String(value)) = value.get("TRACK") {
+                        println!(" Track: {}", value);
+                    }
+                }
+                _ => (),
+            },
+            Event::Shutdown => return Ok(()),
+            Event::Unimplemented(_) => panic!("Unimplemented event"),
+            _ => (),
+        }
     }
+
+    Ok(())
 }