Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.
Rust downloadmanagerguihacktoberfesthomebrewlightweightlinuxlinux-distributionlocal-firstmacosmacos-packagemedia-toolboxopen-sourcerustsveltetauritrendingtrending-repositorieswindowswindows-desktopytdlp
<script lang="ts">
import { onMount } from "svelte";
import { invoke } from "@tauri-apps/api/core";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { t } from "$lib/i18n";
import { showToast } from "$lib/stores/toast-store.svelte";
type Props = { id: number; status?: string };
let { id, status = "downloading" }: Props = $props();
let expanded = $state(false);
let lines = $state<string[]>([]);
let loading = $state(false);
let preEl: HTMLPreElement | null = $state(null);
let atBottom = $state(true);
let refreshTimer: ReturnType<typeof setInterval> | null = null;
// Última linha do yt-dlp, mostrada inline enquanto o log está fechado: é a
// resposta mais barata para "o que ele está fazendo agora?" sem abrir 200
// linhas (padrão do Seal, `currentLine`).
let lastLine = $state("");
let showLast = $derived(!expanded && !!lastLine && (status === "downloading" || status === "paused" || status === "error"));
function isNoise(line: string): boolean {
const l = line.trim();
return !l || l.startsWith("download:") || l.startsWith("[download]") && /\d+(\.\d+)?%/.test(l);
}
async function refresh() {
try {
const next = await invoke<string[]>("get_download_log", { downloadId: id });
lines = next;
for (let i = next.length - 1; i >= 0; i--) {
if (!isNoise(next[i])) {
lastLine = next[i];
break;
}
}
} catch (_) {
lines = [];
}
}
async function toggle() {
expanded = !expanded;
if (expanded) {
loading = true;
await refresh();
loading = false;
atBottom = true;
refreshTimer = setInterval(refresh, 1000);
} else if (refreshTimer) {
clearInterval(refreshTimer);
refreshTimer = null;
}
}
onMount(() => {
let unlisten: UnlistenFn | null = null;
let disposed = false;
if (status === "downloading" || status === "paused" || status === "error") {
void refresh();
}
listen<{ id: number; line?: string }>("download-log-update", (event) => {
if (!event.payload || event.payload.id !== id) return;
const line = event.payload.line;
if (typeof line === "string" && !isNoise(line)) {
lastLine = line;
}
if (expanded) void refresh();
}).then((fn) => {
if (disposed) fn();
else unlisten = fn;
});
return () => {
disposed = true;
if (unlisten) unlisten();
};
});
$effect(() => {
if (expanded && atBottom && preEl && lines.length) {
queueMicrotask(() => {
if (preEl) preEl.scrollTop = preEl.scrollHeight;
});
}
});
function onScroll() {
if (!preEl) return;
const threshold = 12;
atBottom = preEl.scrollHeight - preEl.scrollTop - preEl.clientHeight <= threshold;
}
async function copyLog() {
try {
await navigator.clipboard.writeText(lines.join("\n"));
showToast("success", $t("downloads.log.copied"));
} catch (_) {
showToast("error", $t("downloads.log.copy_failed"));
}
}
$effect(() => {
return () => {
if (refreshTimer) {
clearInterval(refreshTimer);
refreshTimer = null;
}
};
});
</script>
<div class="download-log">
{#if showLast}
<div class="log-last" title={lastLine}>
<span class="log-last-dot" aria-hidden="true"></span>
<code>{lastLine}</code>
</div>
{/if}
<button
type="button"
class="log-toggle"
onclick={toggle}
aria-expanded={expanded}
title={expanded ? $t("downloads.log.hide") : $t("downloads.log.show")}
>
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z" />
<polyline points="14 2 14 8 20 8" />
<line x1="8" y1="13" x2="16" y2="13" />
<line x1="8" y1="17" x2="14" y2="17" />
</svg>
<span>{expanded ? $t("downloads.log.hide") : $t("downloads.log.show")}</span>
</button>
{#if expanded}
<div class="log-panel">
<div class="log-header">
<span class="log-count">
{lines.length}{loading ? "…" : ""}
</span>
<button type="button" class="log-copy" onclick={copyLog} disabled={!lines.length}>
{$t("downloads.log.copy")}
</button>
</div>
{#if !lines.length && !loading}
<div class="log-empty">{$t("downloads.log.empty")}</div>
{:else}
<pre bind:this={preEl} onscroll={onScroll}>{lines.join("\n")}</pre>
{/if}
</div>
{/if}
</div>
<style>
.download-log {
display: flex;
flex-direction: column;
gap: 6px;
margin-top: 6px;
}
.log-last {
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
color: var(--text-faint);
font-size: 11px;
}
.log-last-dot {
width: 5px;
height: 5px;
border-radius: var(--radius-full);
background: var(--text-faint);
flex-shrink: 0;
}
.log-last code {
font-family: var(--font-mono);
font-size: 11px;
line-height: 1.4;
color: var(--text-dim);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 0;
}
.log-toggle {
display: inline-flex;
align-items: center;
gap: 4px;
height: 20px;
padding: 0 6px;
margin-left: -6px;
background: transparent;
border: none;
border-radius: var(--radius-xs);
color: var(--text-dim);
font-size: var(--text-xs);
font-weight: 500;
cursor: pointer;
align-self: flex-start;
transition: background-color var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.log-toggle:hover {
background: var(--fill-2);
color: var(--text);
}
.log-panel {
border: none;
border-radius: var(--border-radius);
background: var(--popup-bg);
overflow: hidden;
}
.log-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 6px 8px;
border-bottom: none;
font-size: 11px;
color: var(--tertiary);
}
.log-copy {
padding: 2px 8px;
border: none;
background: transparent;
border-radius: var(--border-radius);
color: var(--secondary);
font-size: 11px;
cursor: pointer;
transition: background-color 0.12s, border-color 0.12s;
}
.log-copy:hover:not(:disabled) {
background: var(--button-hover);
border-color: var(--input-border);
}
.log-copy:disabled {
opacity: 0.5;
cursor: not-allowed;
}
pre {
margin: 0;
padding: 8px;
max-height: 220px;
overflow-y: auto;
font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
font-size: 11px;
line-height: 1.5;
color: var(--secondary);
white-space: pre-wrap;
word-break: break-all;
background: var(--input-bg);
}
.log-empty {
padding: 12px;
text-align: center;
font-size: 11px;
color: var(--tertiary);
}
</style>