Commit 183ac39ccb

183ac39ccbc258b98e50a430819b2a83c23bc328

parent: ddf8315021

Unsigned

Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2023-08-23 20:56 UTC

mpv player message progress bar

Layout: unified · split

Cargo.lock +1
@@ -1546,6 +1546,7 @@ dependencies = [
15461546 "serde_yaml",
15471547 "tui-additions",
15481548 "typemap",
1549 "unicode-segmentation",
15491550 "urlencoding",
15501551 "viuer",
15511552]
Cargo.toml +1
@@ -38,6 +38,7 @@ clipboard = {version = "0.5", optional = true}
3838urlencoding = "2.1"
3939ratatui = {version = "0.21", default-features = false, features = ["serde"]}
4040libmpv = { git = "https://github.com/sudipghimire533/libmpv-rs", optional = true }
41unicode-segmentation = "1"
4142
4243tui-additions = {path = "../../Rust/Packages/tui-additions", default-features = false, features = ["framework", "widgets"]}
4344# invidious = {path = "../../Rust/Packages/invidious"}
src/config/commandbindings.rs +25 −13
@@ -175,6 +175,30 @@ fn global_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
175175 KeyCodeSerde::Char('y'),
176176 HashMap::from([(0, String::from("cp ${url}"))]),
177177 ),
178 (
179 KeyCodeSerde::Char(' '),
180 HashMap::from([(0, String::from("tpause ;; echo mpv Player paused toggled"))]),
181 ),
182 (
183 KeyCodeSerde::KeyVariants(super::serde::KeyVariantsSerde::Left),
184 HashMap::from([
185 (
186 2,
187 String::from("mpv seek -5 ;; echo mpv Reversed 5 seconds"),
188 ),
189 (3, String::from("back ;; echo mpv Unskipped")),
190 ]),
191 ),
192 (
193 KeyCodeSerde::KeyVariants(super::serde::KeyVariantsSerde::Right),
194 HashMap::from([
195 (
196 2,
197 String::from("mpv seek 5 ;; echo mpv Fast forwarded 5 seconds"),
198 ),
199 (3, String::from("next ;; echo mpv Skipped")),
200 ]),
201 ),
178202 ])
179203}
180204
@@ -183,7 +207,6 @@ fn search_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
183207 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
184208 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
185209 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
186 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
187210 ])
188211}
189212
@@ -196,7 +219,6 @@ fn channel_playlists_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
196219 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
197220 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
198221 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
199 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
200222 ])
201223}
202224
@@ -205,7 +227,6 @@ fn channel_videos_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
205227 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
206228 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
207229 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
208 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
209230 ])
210231}
211232
@@ -214,7 +235,6 @@ fn playlist_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
214235 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
215236 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
216237 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
217 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
218238 ])
219239}
220240
@@ -223,7 +243,6 @@ fn popular_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
223243 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
224244 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
225245 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
226 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
227246 ])
228247}
229248
@@ -232,15 +251,11 @@ fn trending_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
232251 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
233252 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
234253 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
235 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
236254 ])
237255}
238256
239257fn video_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
240 HashMap::from([(
241 KeyCodeSerde::Char(' '),
242 HashMap::from([(0, String::from("parrun mpv '${embed-url}'"))]),
243 )])
258 HashMap::new()
244259}
245260
246261fn watchhistory_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
@@ -248,7 +263,6 @@ fn watchhistory_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
248263 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
249264 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
250265 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
251 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
252266 ])
253267}
254268
@@ -267,7 +281,6 @@ fn feed_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
267281 KeyCodeSerde::Char('p'),
268282 HashMap::from([(2, String::from("parrun mpv '${hover-video-url}'"))]),
269283 ),
270 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
271284 ])
272285}
273286
@@ -276,6 +289,5 @@ fn library_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
276289 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
277290 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
278291 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
279 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
280292 ])
281293}
src/config/commands.rs +9 −9
@@ -100,11 +100,11 @@ fn video_default() -> Vec<HashMap<String, String>> {
100100 )]),
101101 HashMap::from([(
102102 String::from("Play audio"),
103 String::from("mpv sprop loop no ;; mpv loadfile '${embed-url}'"),
103 String::from("mpv stop ;; resume ;; mpv sprop loop no ;; mpv loadfile '${embed-url}'"),
104104 )]),
105105 HashMap::from([(
106106 String::from("Play audio (loop)"),
107 String::from("mpv sprop loop inf ;; mpv loadfile '${embed-url}'"),
107 String::from("mpv stop ;; resume ;; mpv sprop loop inf ;; mpv loadfile '${embed-url}'"),
108108 )]),
109109 HashMap::from([(
110110 String::from("View channel"),
@@ -149,11 +149,11 @@ fn saved_video_default() -> Vec<HashMap<String, String>> {
149149 )]),
150150 HashMap::from([(
151151 String::from("[Offline] Play saved file (audio)"),
152 String::from("mpv sprop loop no ;; mpv loadfile '${offline-path}'"),
152 String::from("mpv stop ;; resume ;; mpv sprop loop no ;; mpv loadfile '${offline-path}'"),
153153 )]),
154154 HashMap::from([(
155155 String::from("[Offline] Play saved file (audio loop)"),
156 String::from("mpv sprop loop inf ;; mpv loadfile '${offline-path}'"),
156 String::from("mpv stop ;; resume ;; mpv sprop loop inf ;; mpv loadfile '${offline-path}'"),
157157 )]),
158158 HashMap::from([(
159159 String::from("View channel"),
@@ -199,11 +199,11 @@ fn playlist_default() -> Vec<HashMap<String, String>> {
199199 )]),
200200 HashMap::from([(
201201 String::from("Play all (audio)"),
202 String::from("mpv stop ;; ${mpv-queuelist} ;; mpv playlist-play-index 0"),
202 String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv playlist-play-index 0"),
203203 )]),
204204 HashMap::from([(
205205 String::from("Shuffle play all (audio loop)"),
206 String::from("mpv stop ;; ${mpv-queuelist} ;; mpv playlist-shuffle ;; mpv playlist-play-index 0"),
206 String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv playlist-shuffle ;; mpv playlist-play-index 0"),
207207 )]),
208208 HashMap::from([(
209209 String::from("View channel"),
@@ -245,15 +245,15 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
245245 )]),
246246 HashMap::from([(
247247 String::from("[Offline] Play all (videos)"),
248 String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --force-window'"),
248 String::from("parrun ${video-player} ${save-path}*${id}*/* --force-window"),
249249 )]),
250250 HashMap::from([(
251251 String::from("[Offline] Play all (audio)"),
252 String::from("mpv stop ;; ${offline-queuelist} ;; mpv playlist-play-index 0"),
252 String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv playlist-play-index 0"),
253253 )]),
254254 HashMap::from([(
255255 String::from("[Offline] Shuffle play all (audio loop)"),
256 String::from("mpv stop ;; ${offline-queuelist} ;; mpv playlist-shuffle ;; mpv playlist-play-index 0"),
256 String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv playlist-shuffle ;; mpv playlist-play-index 0"),
257257 )]),
258258 HashMap::from([(
259259 String::from("View channel"),
src/config/commands_remap.rs +20
@@ -42,6 +42,26 @@ impl Default for CommandsRemapConfig {
4242 ("x".to_string(), "quit".to_string()),
4343 ("exit".to_string(), "quit".to_string()),
4444 ("sub".to_string(), "sync".to_string()),
45 (
46 "tpause".to_string(),
47 "mpv tprop pause ;; echo success Toggled player pause".to_string(),
48 ),
49 (
50 "pause".to_string(),
51 "mpv sprop pause yes ;; echo mpv Player paused".to_string(),
52 ),
53 (
54 "resume".to_string(),
55 "mpv sprop pause no ;; echo mpv Player resumed".to_string(),
56 ),
57 (
58 "back".to_string(),
59 "mpv playlist-previous ;; echo mpv Skipped".to_string(),
60 ),
61 (
62 "next".to_string(),
63 "mpv playlist-next ;; echo mpv Skipped".to_string(),
64 ),
4565 ]))
4666 }
4767}
src/global/functions/run_command.rs +43 −11
@@ -459,7 +459,7 @@ pub fn run_single_command(
459459 key_input(KeyEvent::new(keycode, keymodifier), framework, terminal)
460460 }
461461 #[cfg(feature = "mpv")]
462 ["mpv", "property", property] | ["mpv", "prop", property] => {
462 ["mpv", "prop", property] => {
463463 let res = framework
464464 .data
465465 .global
@@ -468,22 +468,41 @@ pub fn run_single_command(
468468 .property(property.to_string());
469469
470470 *framework.data.global.get_mut::<Message>().unwrap() = match res {
471 Some(value) => Message::Message(format!("Value: {value}")),
471 Some(value) => Message::Message(format!("Value: `{value}`")),
472472 None => Message::Error("No such property".to_string()),
473473 };
474474 }
475475 #[cfg(feature = "mpv")]
476 ["mpv", "set_property", name, ..] | ["mpv", "sprop", name, ..] => {
476 ["mpv", "tprop", property] => {
477 let mpv = framework.data.global.get::<MpvWrapper>().unwrap();
478 let res = mpv.property(property.to_string());
479
480 let toset = match res {
481 Some(value) if value.as_str() == "yes" || value.as_str() == "true" => "no",
482 _ => "yes",
483 };
484
485 let res = mpv.set_property(property.to_string(), toset.to_string());
486 *framework.data.global.get_mut::<Message>().unwrap() = match res {
487 MpvResponse::Copy => Message::Mpv(format!("Set `{property}` to `{toset}`")),
488 MpvResponse::Error(e) => Message::Error(format!("MPV error: {e}")),
489 _ => unreachable!(),
490 };
491 }
492 #[cfg(feature = "mpv")]
493 ["mpv", "sprop", name, ..] => {
494 let value = command[3..].join(" ");
477495 let res = framework
478496 .data
479497 .global
480498 .get::<MpvWrapper>()
481499 .unwrap()
482 .set_property(name.to_string(), command[3..].join(" "));
500 .set_property(name.to_string(), value.clone());
483501
484502 *framework.data.global.get_mut::<Message>().unwrap() = match res {
485 MpvResponse::Copy => Message::Mpv("Value set".to_string()),
486 MpvResponse::Error(e) => Message::Error(format!("MPV error: {e}")),
503 // MpvResponse::Copy => Message::Mpv(format!("Set `{name}` to `{value}`")),
504 MpvResponse::Copy => return,
505 MpvResponse::Error(e) => Message::Mpv(format!("MPV error: {e}")),
487506 _ => unreachable!(),
488507 };
489508 }
@@ -495,14 +514,21 @@ pub fn run_single_command(
495514 );
496515
497516 *framework.data.global.get_mut::<Message>().unwrap() = match res {
498 MpvResponse::Copy => Message::Mpv("MPV player OK.".to_string()),
499 MpvResponse::Error(e) => Message::Error(format!("MPV error: {e}")),
517 // MpvResponse::Copy => Message::Mpv("MPV player OK.".to_string()),
518 MpvResponse::Copy => return,
519 MpvResponse::Error(e) => Message::Mpv(format!("MPV error: {e}")),
500520 _ => unreachable!(),
501521 };
502522 }
503 ["echo", ..] => {
504 *framework.data.global.get_mut::<Message>().unwrap() =
505 Message::Message(command[1..].join(" "))
523 ["echo", r#type, ..] => {
524 *framework.data.global.get_mut::<Message>().unwrap() = match *r#type {
525 "message" => Message::Message(command[2..].join(" ")),
526 "mpv" => Message::Mpv(command[2..].join(" ")),
527 "success" => Message::Success(command[2..].join(" ")),
528 "error" => Message::Error(command[2..].join(" ")),
529 "none" => Message::None,
530 _ => Message::Error(format!("Unknown type `{}`", r#type)),
531 }
506532 }
507533 _ => {
508534 if let Some(cmd) = framework
@@ -601,6 +627,12 @@ fn help_msg(cmdefines: &CommandsRemapConfig) -> String {
601627 \x1b[33munsub [id or url]\x1b[0m Remove channel from subscription
602628 \x1b[33msyncall\x1b[0m Sync all subscriptions
603629
630\x1b[91mMPV:\x1b[0m
631 \x1b[33mmpv prop [label]\x1b[0m Gets mpv property
632 \x1b[33mmpv sprop [label] [value]\x1b[0m Set mpv property
633 \x1b[33mmpv tprop [label] [value]\x1b[0m Toggle a yes/no property
634 \x1b[33mmpv [command]\x1b[0m Runs a libmpv command
635
604636\x1b[91mCUSTOM COMMANDS:\x1b[0m
605637\x1b[37mdefined in cmdefine.yml\x1b[30m
606638{}
src/items/messagebar.rs +56 −3
@@ -1,4 +1,7 @@
1use crate::{config::*, global::structs::*};
1use crate::{
2 config::*,
3 global::{functions::secs_display_string, structs::*},
4};
25use ratatui::{
36 layout::Rect,
47 style::Style,
@@ -25,20 +28,70 @@ impl FrameworkItem for MessageBar {
2528
2629 let mpv = framework.data.global.get::<MpvWrapper>().unwrap();
2730 if Self::is_mpv_render(framework) {
31 let mut label = mpv.property("media-title".to_string()).unwrap();
32 if let Some((name, ext)) = label.rsplit_once('.') {
33 if ext.len() < 5
34 && !name.len() > 13
35 && &name[name.len() - 1..name.len()] == "]"
36 && name[name.len() - 12..name.len() - 1]
37 .chars()
38 .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
39 && &name[name.len() - 13..name.len() - 12] == "["
40 {
41 label = name[0..name.len() - 13].to_string();
42 }
43 }
2844 let duration = mpv
2945 .property("duration".to_string())
3046 .unwrap_or_default()
3147 .parse::<f64>()
3248 .unwrap() as u64;
49 let duration_s = secs_display_string(duration as u32);
3350 let playerhead = mpv
3451 .property("time-pos".to_string())
3552 .unwrap_or_default()
3653 .parse::<f64>()
3754 .unwrap() as u64;
38 let percentage = playerhead * 100 / duration;
55 let mut playerhead_s = secs_display_string(playerhead as u32);
56 if playerhead_s.len() != duration_s.len() {
57 playerhead_s = format!(
58 "{}{playerhead_s}",
59 " ".repeat(duration_s.len() - playerhead_s.len())
60 );
61 }
62 let percentage = (playerhead * 100 / duration).to_string();
63
64 let right_chunk = format!(
65 "{playerhead_s}/{duration_s} {}[{percentage}%]",
66 " ".repeat(3 - percentage.len())
67 );
68 let left_chunk = format!("[Now Playing]: {label}");
69 let length = area.width as usize - 2;
70 let total_len = right_chunk.len() + left_chunk.len();
3971
4072 *framework.data.global.get_mut::<Message>().unwrap() =
41 Message::Mpv(format!("{playerhead}/{duration} [{percentage}%]"));
73 Message::Mpv(if length > total_len + 6 {
74 let mut seeker_len = length - total_len - 4;
75 let seeker_pad = if seeker_len > 10 { seeker_len / 10 } else { 0 };
76 seeker_len -= seeker_pad * 2;
77 let seeker_pos = seeker_len * playerhead as usize / duration as usize;
78 format!(
79 "{left_chunk} {}├{}-{}┤{} {right_chunk}",
80 " ".repeat(seeker_pad),
81 "─".repeat(seeker_pos),
82 "─".repeat(seeker_len - seeker_pos - 1),
83 " ".repeat(seeker_pad)
84 )
85 } else if length > total_len + 3 {
86 format!(
87 "{left_chunk}{}{right_chunk}",
88 " ".repeat(length - total_len)
89 )
90 } else if length > 19 {
91 left_chunk
92 } else {
93 String::from("Not enough width")
94 });
4295 }
4396
4497 let message = framework.data.global.get::<Message>().unwrap();
src/items/singleitem.rs +1 −1
@@ -347,7 +347,7 @@ impl SinglePlaylistItem {
347347 Ok(entries) => entries
348348 .map(|entry| {
349349 format!(
350 "mpv loadfile '{}'",
350 "mpv loadfile '{}' append",
351351 match entry {
352352 Ok(entry) =>
353353 entry.path().as_os_str().to_str().unwrap().to_string(),