// Copyright 2014-2025 Jesper Larsson
//
// This file is part of Klipspringer, <https://klipspringer.avadeaux.net/>
//
// Klipspringer is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// Klipspringer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Klipspringer. If
// not, see <https://www.gnu.org/licenses/>.

package net.avadeaux.klipspringer;

import net.avadeaux.klipspringer.codec.PcmFormat;

/** Runnable that continuously updates terminal display with current status. Terminates and updates
  * the display with a shutdown hook, and should therefore not be run in a daemon thread.
  */
public class TrackTerminalDisplay implements Runnable {
    private final PlayMonitor state;
    private String backup = "";
    private boolean quit = false;

    public TrackTerminalDisplay(PlayMonitor state) {
        this.state = state;
    }

    public synchronized void quit() {
        System.out.print(backup);
        backup = "";
        quit = true;
    }

    public void run() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    quit();
                }
            });
        PlayMonitor.Info info = state.getInfoObject();
        while (true) {
            state.getStatus(info, 1000);
            synchronized (this) {
                System.out.print(backup);
                if (quit) { break; }
                double ti = info.time();
                if (Double.isNaN(ti)) { backup = ""; continue; }
                System.out.println(info.trackName() == null ? "" : info.trackName());
                long s = (long) Math.floor(ti);
                if (s >= 3600) {
                    System.out.print(String.format("%d:%02d:%02d", s/3600, (s%3600)/60, s%60));
                } else {
                    System.out.print(String.format("%d:%02d", s/60, s%60));
                }
                PcmFormat fmt = info.format();
                if (fmt != null) {
                    int ch = info.monomixing() ? 1 : fmt.channels();
                    String q = info.quality();
                    System.out.print(" "+fmt.significantBips()+"-bit "
                                     +fmt.rate()+" Hz "
                                     + (ch == 2 ? "stereo" : (ch == 1 ? "mono" : ch+"-channel"))
                                     + (q.length() > 0 ? " ("+q+")" : ""));
                }
                System.out.print("\n");
                backup = "\u001b[A\u001b[K\u001b[A\u001b[K";
            }
        }
    }
}

Version: v4.3.2.2 (2026-05-16T17:03:34+02:00)
Raw file
Source code overview
Klipspringer home