diff options
| author | salmonik <raisersoft@proton.me> | 2026-06-07 21:56:40 +0300 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-06-07 21:56:40 +0300 |
| commit | 3aea522b0969e0d41cb3d0eec737563518dfe861 (patch) | |
| tree | 04d28e1fed7613419f3bc96dd9fcdc844f91ca12 /src/daemon.rs | |
| parent | Add lyrics support (diff) | |
| download | rsplayer-3aea522b0969e0d41cb3d0eec737563518dfe861.tar.gz rsplayer-3aea522b0969e0d41cb3d0eec737563518dfe861.zip | |
change lrc lyrics display.
Diffstat (limited to '')
| -rw-r--r-- | src/daemon.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/daemon.rs b/src/daemon.rs index 0590f71..a0de698 100644 --- a/src/daemon.rs +++ b/src/daemon.rs | |||
| @@ -523,14 +523,21 @@ fn handle_lyrics_stream(stream: &mut UnixStream, audio: &Arc<AudioEngine>) { | |||
| 523 | return; | 523 | return; |
| 524 | } | 524 | } |
| 525 | 525 | ||
| 526 | let mut last_idx = None; | ||
| 527 | let mut prev_line_count = 0; | ||
| 526 | loop { | 528 | loop { |
| 527 | let pos = audio.position(); | 529 | let pos = audio.position(); |
| 528 | let current_idx = lyrics.iter().rposition(|l| l.time <= pos).unwrap_or(0); | 530 | let current_idx = lyrics.iter().rposition(|l| l.time <= pos).unwrap_or(0); |
| 531 | if last_idx == Some(current_idx) { | ||
| 532 | thread::sleep(Duration::from_millis(100)); | ||
| 533 | continue; | ||
| 534 | } | ||
| 535 | last_idx = Some(current_idx); | ||
| 536 | |||
| 529 | let start = current_idx.saturating_sub(1); | 537 | let start = current_idx.saturating_sub(1); |
| 530 | let end = (current_idx + 3).min(lyrics.len()); | 538 | let end = (current_idx + 3).min(lyrics.len()); |
| 531 | 539 | ||
| 532 | let mut output = String::new(); | 540 | let mut output = String::new(); |
| 533 | output.push_str("\x1b[2J\x1b[H"); | ||
| 534 | for (idx, l) in lyrics.iter().enumerate().take(end).skip(start) { | 541 | for (idx, l) in lyrics.iter().enumerate().take(end).skip(start) { |
| 535 | if idx == current_idx { | 542 | if idx == current_idx { |
| 536 | output.push_str(&format!(" > {}\n", l.text)); | 543 | output.push_str(&format!(" > {}\n", l.text)); |
| @@ -538,13 +545,22 @@ fn handle_lyrics_stream(stream: &mut UnixStream, audio: &Arc<AudioEngine>) { | |||
| 538 | output.push_str(&format!(" {}\n", l.text)); | 545 | output.push_str(&format!(" {}\n", l.text)); |
| 539 | } | 546 | } |
| 540 | } | 547 | } |
| 541 | if stream.write_all(output.as_bytes()).is_err() { | 548 | let line_count = output.lines().count(); |
| 549 | let mut frame = String::new(); | ||
| 550 | if prev_line_count > 0 { | ||
| 551 | frame.push_str(&format!("\x1b[{}A\x1b[1B", prev_line_count)); | ||
| 552 | } else { | ||
| 553 | frame.push_str(&format!("\x1b[2K")); | ||
| 554 | } | ||
| 555 | frame.push_str(&output); | ||
| 556 | if stream.write_all(frame.as_bytes()).is_err() { | ||
| 542 | break; | 557 | break; |
| 543 | } | 558 | } |
| 544 | if stream.flush().is_err() { | 559 | if stream.flush().is_err() { |
| 545 | break; | 560 | break; |
| 546 | } | 561 | } |
| 547 | thread::sleep(Duration::from_millis(200)); | 562 | prev_line_count = line_count; |
| 563 | thread::sleep(Duration::from_millis(100)); | ||
| 548 | } | 564 | } |
| 549 | } | 565 | } |
| 550 | 566 | ||
