feat(ui): add copy-last-40-lines button to log visualizer

This commit is contained in:
mika kuns
2026-08-05 20:26:04 +02:00
parent bdee731376
commit 8c251c78b1
5 changed files with 147 additions and 4 deletions
@@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input.Platform;
using ClaudeDo.Data.Models;
using ClaudeDo.Ui.Localization;
using ClaudeDo.Ui.Services;
@@ -18,7 +21,10 @@ namespace ClaudeDo.Ui.ViewModels.Modals;
/// </summary>
public sealed partial class LogVisualizerViewModel : ViewModelBase
{
private const int CopyLastCount = 40;
private readonly IWorkerClient _worker;
private readonly Func<string, Task<bool>> _copyToClipboard;
private IReadOnlyList<WorkerLogEntry> _all = Array.Empty<WorkerLogEntry>();
public ObservableCollection<LogVisualizerRow> Rows { get; } = new();
@@ -28,7 +34,14 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
public Action? CloseAction { get; set; }
public LogVisualizerViewModel(IWorkerClient worker) => _worker = worker;
public LogVisualizerViewModel(IWorkerClient worker) : this(worker, CopyToClipboardAsync) { }
internal LogVisualizerViewModel(IWorkerClient worker, Func<string, Task<bool>> copyToClipboard)
{
_worker = worker;
_copyToClipboard = copyToClipboard;
Rows.CollectionChanged += (_, _) => CopyLastCommand.NotifyCanExecuteChanged();
}
[RelayCommand]
public async Task RefreshAsync()
@@ -53,6 +66,33 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
}
[RelayCommand] private void Close() => CloseAction?.Invoke();
private bool CanCopyLast() => Rows.Count > 0;
[RelayCommand(CanExecute = nameof(CanCopyLast))]
private async Task CopyLastAsync()
{
var count = Math.Min(Rows.Count, CopyLastCount);
var ok = await _copyToClipboard(BuildCopyText());
StatusText = ok
? Loc.T("modals.logVisualizer.copied", count)
: Loc.T("modals.logVisualizer.copyFailed");
}
internal string BuildCopyText() =>
string.Join('\n', Rows.Take(CopyLastCount).Reverse()
.Select(r => $"{r.Time} {r.Level.ToString().ToUpperInvariant()} {r.Message}"));
private static async Task<bool> CopyToClipboardAsync(string text)
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop &&
desktop.MainWindow?.Clipboard is { } clipboard)
{
try { await clipboard.SetTextAsync(text); return true; }
catch { return false; }
}
return false;
}
}
public sealed record LogVisualizerRow(string Time, string Message, WorkerLogLevel Level);