From ed96c9b6bf9cfd4a481a8e438aaade2758785d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 24 Sep 2026 20:04:28 +0300 Subject: [PATCH] fix(song-detail): keep only the current line, no playback history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /tmp/song_detail_last accumulated every emitted line — 9454 lines in the live file. That log was an emergent side effect of the append workaround, not a feature, and the `cat` consumers rendered the whole blob. The file now holds exactly the current line: writes truncate in place (the inode never changes), nothing playing truncates it to 0 bytes, and startup carries over at most one line so a leftover append-era log is dropped. Consumers read the current line instead of following a stream: waybar custom/music moved from `tail -n 1 -F` to `cat` + interval 1, the same shape eww and hyprlock already use. tail -F needed the append stream to deliver equal-length rewrites and printed "file truncated" on every in-place rewrite; an interval read holds no offset state to go stale. No history, no persistence: everything stays in tmpfs and is rebuilt from playerctl each poll. --- .config/scripts/shared/song_detail_writer.sh | 36 ++++++++++++-------- .config/waybar/config-hyprland | 3 +- .config/waybar/config-niri | 3 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.config/scripts/shared/song_detail_writer.sh b/.config/scripts/shared/song_detail_writer.sh index 6dc99bd..169c817 100755 --- a/.config/scripts/shared/song_detail_writer.sh +++ b/.config/scripts/shared/song_detail_writer.sh @@ -1,12 +1,15 @@ #!/bin/bash # Single-source writer: poll playerctl, write the formatted song-detail line -# to /tmp/song_detail_last so every waybar bar can stream it via `tail -F` +# to /tmp/song_detail_last so every consumer (waybar, eww, hyprlock) reads it # without two scripts racing on shared /tmp state. # +# The file holds ONLY the current line: no history, no persistence. Every +# write truncates in place, so the inode stays put and readers that re-read +# the file each interval always see the live line. Whatever was in the file +# before — including a leftover in-place log from an older build — is dropped. +# # niri spawn-at-startup re-runs on compositor restart and used to stack -# copies; orphans then `mv`-replaced the output inode every second, which -# makes waybar's `tail -F` reopen and flicker. Newest instance takes over; -# the tailed file is rewritten in place so its inode stays put. +# copies; newest instance takes over (singleton below). OUTPUT_FILE="/tmp/song_detail_last" POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_" @@ -46,12 +49,15 @@ atomic_write() { printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target" } -# Append-only stream. tail -F is offset-based: an in-place rewrite whose new -# content is the SAME BYTE LENGTH as the old (icon flip, position tick) is -# invisible to it — the bar goes stale until some longer line happens by. -# Appending keeps the inode put and delivers every state change. +# Current line only. Truncate in place (same inode, same path) so a reader +# that re-reads the file every interval gets the new content and never sees +# a pile of old lines. Empty state = empty file. write_output() { - printf '%s\n' "$1" >> "$OUTPUT_FILE" + if [[ -z "$1" ]]; then + : > "$OUTPUT_FILE" + else + printf '%s\n' "$1" > "$OUTPUT_FILE" + fi } last_emit="__init__" @@ -81,12 +87,12 @@ pango_escape() { printf '%s' "$s" } -# File must exist so `tail -F` can attach; do not wipe a live line. -[[ -e "$OUTPUT_FILE" ]] || : > "$OUTPUT_FILE" - -# Append-mode log grows with every emitted line; keep only the newest once large. -if [[ -f "$OUTPUT_FILE" ]] && (( $(stat -c %s "$OUTPUT_FILE" 2>/dev/null || echo 0) > 1048576 )); then - tail -n 1 "$OUTPUT_FILE" > "$OUTPUT_FILE.trim" && mv -f "$OUTPUT_FILE.trim" "$OUTPUT_FILE" +# File must exist for readers. Carry over at most the current line — any +# accumulated log from an older append-mode build is dropped here. +if [[ -s "$OUTPUT_FILE" ]]; then + printf '%s\n' "$(tail -n 1 "$OUTPUT_FILE")" > "$OUTPUT_FILE" +else + : > "$OUTPUT_FILE" fi while true; do diff --git a/.config/waybar/config-hyprland b/.config/waybar/config-hyprland index 32a9164..289c113 100644 --- a/.config/waybar/config-hyprland +++ b/.config/waybar/config-hyprland @@ -79,7 +79,8 @@ "custom/music": { "format": "{}", - "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", + "exec": "cat /tmp/song_detail_last 2>/dev/null", + "interval": 1, "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", diff --git a/.config/waybar/config-niri b/.config/waybar/config-niri index c3f1279..e883c62 100644 --- a/.config/waybar/config-niri +++ b/.config/waybar/config-niri @@ -79,7 +79,8 @@ "custom/music": { "format": "{}", - "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", + "exec": "cat /tmp/song_detail_last 2>/dev/null", + "interval": 1, "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+",