Keep only the current line, no playback history
Browse files at this commited96c9b6bf9cfd4a481a8e438aaade2758785d34Parent: ef264af- fix
- keep only the current line, no playback history
3 changed files +25 -17
- .config/scripts/shared/song_detail_writer.sh +21 -15
- .config/waybar/config-hyprland +2 -1
- .config/waybar/config-niri +2 -1
.config/scripts/shared/song_detail_writer.shmodified+21 -15
| @@ -1,12 +1,15 @@ | |||
| 1 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | # Single-source writer: poll playerctl, write the formatted song-detail line | 2 | # Single-source writer: poll playerctl, write the formatted song-detail line |
| 3 | -# to /tmp/song_detail_last so every waybar bar can stream it via `tail -F` | 3 | +# to /tmp/song_detail_last so every consumer (waybar, eww, hyprlock) reads it |
| 4 | # without two scripts racing on shared /tmp state. | 4 | # without two scripts racing on shared /tmp state. |
| 5 | # | 5 | # |
| 6 | +# The file holds ONLY the current line: no history, no persistence. Every | ||
| 7 | +# write truncates in place, so the inode stays put and readers that re-read | ||
| 8 | +# the file each interval always see the live line. Whatever was in the file | ||
| 9 | +# before — including a leftover in-place log from an older build — is dropped. | ||
| 10 | +# | ||
| 6 | # niri spawn-at-startup re-runs on compositor restart and used to stack | 11 | # niri spawn-at-startup re-runs on compositor restart and used to stack |
| 7 | -# copies; orphans then `mv`-replaced the output inode every second, which | 12 | +# copies; newest instance takes over (singleton below). |
| 8 | -# makes waybar's `tail -F` reopen and flicker. Newest instance takes over; | ||
| 9 | -# the tailed file is rewritten in place so its inode stays put. | ||
| 10 | 13 | ||
| 11 | OUTPUT_FILE="/tmp/song_detail_last" | 14 | OUTPUT_FILE="/tmp/song_detail_last" |
| 12 | POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_" | 15 | POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_" |
| @@ -46,12 +49,15 @@ atomic_write() { | |||
| 46 | printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target" | 49 | printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target" |
| 47 | } | 50 | } |
| 48 | 51 | ||
| 49 | -# Append-only stream. tail -F is offset-based: an in-place rewrite whose new | 52 | +# Current line only. Truncate in place (same inode, same path) so a reader |
| 50 | -# content is the SAME BYTE LENGTH as the old (icon flip, position tick) is | 53 | +# that re-reads the file every interval gets the new content and never sees |
| 51 | -# invisible to it — the bar goes stale until some longer line happens by. | 54 | +# a pile of old lines. Empty state = empty file. |
| 52 | -# Appending keeps the inode put and delivers every state change. | ||
| 53 | write_output() { | 55 | write_output() { |
| 54 | - printf '%s\n' "$1" >> "$OUTPUT_FILE" | 56 | + if [[ -z "$1" ]]; then |
| 57 | + : > "$OUTPUT_FILE" | ||
| 58 | + else | ||
| 59 | + printf '%s\n' "$1" > "$OUTPUT_FILE" | ||
| 60 | + fi | ||
| 55 | } | 61 | } |
| 56 | 62 | ||
| 57 | last_emit="__init__" | 63 | last_emit="__init__" |
| @@ -81,12 +87,12 @@ pango_escape() { | |||
| 81 | printf '%s' "$s" | 87 | printf '%s' "$s" |
| 82 | } | 88 | } |
| 83 | 89 | ||
| 84 | -# File must exist so `tail -F` can attach; do not wipe a live line. | 90 | +# File must exist for readers. Carry over at most the current line — any |
| 85 | -[[ -e "$OUTPUT_FILE" ]] || : > "$OUTPUT_FILE" | 91 | +# accumulated log from an older append-mode build is dropped here. |
| 86 | - | 92 | +if [[ -s "$OUTPUT_FILE" ]]; then |
| 87 | -# Append-mode log grows with every emitted line; keep only the newest once large. | 93 | + printf '%s\n' "$(tail -n 1 "$OUTPUT_FILE")" > "$OUTPUT_FILE" |
| 88 | -if [[ -f "$OUTPUT_FILE" ]] && (( $(stat -c %s "$OUTPUT_FILE" 2>/dev/null || echo 0) > 1048576 )); then | 94 | +else |
| 89 | - tail -n 1 "$OUTPUT_FILE" > "$OUTPUT_FILE.trim" && mv -f "$OUTPUT_FILE.trim" "$OUTPUT_FILE" | 95 | + : > "$OUTPUT_FILE" |
| 90 | fi | 96 | fi |
| 91 | 97 | ||
| 92 | while true; do | 98 | while true; do |
.config/waybar/config-hyprlandmodified+2 -1
| @@ -79,7 +79,8 @@ | |||
| 79 | 79 | ||
| 80 | "custom/music": { | 80 | "custom/music": { |
| 81 | "format": "{}", | 81 | "format": "{}", |
| 82 | - "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", | 82 | + "exec": "cat /tmp/song_detail_last 2>/dev/null", |
| 83 | + "interval": 1, | ||
| 83 | "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", | 84 | "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", |
| 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", | 85 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", | 86 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |
.config/waybar/config-nirimodified+2 -1
| @@ -79,7 +79,8 @@ | |||
| 79 | 79 | ||
| 80 | "custom/music": { | 80 | "custom/music": { |
| 81 | "format": "{}", | 81 | "format": "{}", |
| 82 | - "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", | 82 | + "exec": "cat /tmp/song_detail_last 2>/dev/null", |
| 83 | + "interval": 1, | ||
| 83 | "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", | 84 | "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", |
| 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", | 85 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", | 86 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |