feat(ui): add copy-last-40-lines button to log visualizer
This commit is contained in:
@@ -310,6 +310,10 @@
|
|||||||
"refresh": "Aktualisieren",
|
"refresh": "Aktualisieren",
|
||||||
"empty": "Keine Logs in den letzten 30 Minuten.",
|
"empty": "Keine Logs in den letzten 30 Minuten.",
|
||||||
"count": "{0} Einträge",
|
"count": "{0} Einträge",
|
||||||
|
"copyLast": "Letzte 40 kopieren",
|
||||||
|
"copyLastTooltip": "Kopiert die letzten 40 sichtbaren Zeilen in die Zwischenablage",
|
||||||
|
"copied": "{0} Zeilen kopiert",
|
||||||
|
"copyFailed": "Kopieren fehlgeschlagen",
|
||||||
"footerHint": "logs",
|
"footerHint": "logs",
|
||||||
"openTooltip": "Aktuelle Worker-Logs anzeigen"
|
"openTooltip": "Aktuelle Worker-Logs anzeigen"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -311,7 +311,11 @@
|
|||||||
"empty": "No logs in the last 30 minutes.",
|
"empty": "No logs in the last 30 minutes.",
|
||||||
"count": "{0} entries",
|
"count": "{0} entries",
|
||||||
"footerHint": "logs",
|
"footerHint": "logs",
|
||||||
"openTooltip": "View recent worker logs"
|
"openTooltip": "View recent worker logs",
|
||||||
|
"copyLast": "Copy last 40",
|
||||||
|
"copyLastTooltip": "Copy the last 40 visible lines to the clipboard",
|
||||||
|
"copied": "{0} lines copied",
|
||||||
|
"copyFailed": "Copy failed"
|
||||||
},
|
},
|
||||||
"about": {
|
"about": {
|
||||||
"title": "ABOUT",
|
"title": "ABOUT",
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
|
using Avalonia.Input.Platform;
|
||||||
using ClaudeDo.Data.Models;
|
using ClaudeDo.Data.Models;
|
||||||
using ClaudeDo.Ui.Localization;
|
using ClaudeDo.Ui.Localization;
|
||||||
using ClaudeDo.Ui.Services;
|
using ClaudeDo.Ui.Services;
|
||||||
@@ -18,7 +21,10 @@ namespace ClaudeDo.Ui.ViewModels.Modals;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed partial class LogVisualizerViewModel : ViewModelBase
|
public sealed partial class LogVisualizerViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
|
private const int CopyLastCount = 40;
|
||||||
|
|
||||||
private readonly IWorkerClient _worker;
|
private readonly IWorkerClient _worker;
|
||||||
|
private readonly Func<string, Task<bool>> _copyToClipboard;
|
||||||
private IReadOnlyList<WorkerLogEntry> _all = Array.Empty<WorkerLogEntry>();
|
private IReadOnlyList<WorkerLogEntry> _all = Array.Empty<WorkerLogEntry>();
|
||||||
|
|
||||||
public ObservableCollection<LogVisualizerRow> Rows { get; } = new();
|
public ObservableCollection<LogVisualizerRow> Rows { get; } = new();
|
||||||
@@ -28,7 +34,14 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
|
|||||||
|
|
||||||
public Action? CloseAction { get; set; }
|
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]
|
[RelayCommand]
|
||||||
public async Task RefreshAsync()
|
public async Task RefreshAsync()
|
||||||
@@ -53,6 +66,33 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand] private void Close() => CloseAction?.Invoke();
|
[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);
|
public sealed record LogVisualizerRow(string Time, string Message, WorkerLogLevel Level);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
<ctl:ModalShell Title="{loc:Tr modals.logVisualizer.title}" CloseCommand="{Binding CloseCommand}">
|
<ctl:ModalShell Title="{loc:Tr modals.logVisualizer.title}" CloseCommand="{Binding CloseCommand}">
|
||||||
<DockPanel LastChildFill="True">
|
<DockPanel LastChildFill="True">
|
||||||
<!-- Toolbar: filter · status · refresh -->
|
<!-- Toolbar: filter · status · refresh -->
|
||||||
<Grid DockPanel.Dock="Top" ColumnDefinitions="Auto,*,Auto" Margin="14,10">
|
<Grid DockPanel.Dock="Top" ColumnDefinitions="Auto,*,Auto,Auto" Margin="14,10">
|
||||||
<CheckBox Grid.Column="0"
|
<CheckBox Grid.Column="0"
|
||||||
Content="{loc:Tr modals.logVisualizer.warnErrorOnly}"
|
Content="{loc:Tr modals.logVisualizer.warnErrorOnly}"
|
||||||
IsChecked="{Binding WarnErrorOnly}"
|
IsChecked="{Binding WarnErrorOnly}"
|
||||||
@@ -31,7 +31,11 @@
|
|||||||
Text="{Binding StatusText}"
|
Text="{Binding StatusText}"
|
||||||
HorizontalAlignment="Center" VerticalAlignment="Center"
|
HorizontalAlignment="Center" VerticalAlignment="Center"
|
||||||
Foreground="{DynamicResource TextMuteBrush}"/>
|
Foreground="{DynamicResource TextMuteBrush}"/>
|
||||||
<Button Grid.Column="2" Classes="btn"
|
<Button Grid.Column="2" Classes="btn" Margin="0,0,8,0"
|
||||||
|
Content="{loc:Tr modals.logVisualizer.copyLast}"
|
||||||
|
ToolTip.Tip="{loc:Tr modals.logVisualizer.copyLastTooltip}"
|
||||||
|
Command="{Binding CopyLastCommand}"/>
|
||||||
|
<Button Grid.Column="3" Classes="btn"
|
||||||
Content="{loc:Tr modals.logVisualizer.refresh}"
|
Content="{loc:Tr modals.logVisualizer.refresh}"
|
||||||
Command="{Binding RefreshCommand}"/>
|
Command="{Binding RefreshCommand}"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -85,4 +85,95 @@ public class LogVisualizerViewModelTests
|
|||||||
|
|
||||||
Assert.Equal(new[] { "newest", "middle", "oldest" }, vm.Rows.Select(r => r.Message));
|
Assert.Equal(new[] { "newest", "middle", "oldest" }, vm.Rows.Select(r => r.Message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLast_orders_chronologically_oldest_first()
|
||||||
|
{
|
||||||
|
var t0 = new DateTime(2026, 6, 23, 8, 0, 0, DateTimeKind.Utc);
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(new[]
|
||||||
|
{
|
||||||
|
E(WorkerLogLevel.Info, "oldest", t0),
|
||||||
|
E(WorkerLogLevel.Warn, "middle", t0.AddMinutes(1)),
|
||||||
|
E(WorkerLogLevel.Error, "newest", t0.AddMinutes(2)),
|
||||||
|
}));
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
|
||||||
|
var text = vm.BuildCopyText();
|
||||||
|
|
||||||
|
var lines = text.Split('\n');
|
||||||
|
Assert.Equal(3, lines.Length);
|
||||||
|
Assert.EndsWith("INFO oldest", lines[0]);
|
||||||
|
Assert.EndsWith("WARN middle", lines[1]);
|
||||||
|
Assert.EndsWith("ERROR newest", lines[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLast_caps_at_40_most_recent_entries()
|
||||||
|
{
|
||||||
|
var t0 = new DateTime(2026, 6, 23, 8, 0, 0, DateTimeKind.Utc);
|
||||||
|
var entries = Enumerable.Range(0, 50)
|
||||||
|
.Select(i => E(WorkerLogLevel.Info, $"line{i}", t0.AddMinutes(i)))
|
||||||
|
.ToArray();
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(entries));
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
|
||||||
|
var lines = vm.BuildCopyText().Split('\n');
|
||||||
|
|
||||||
|
Assert.Equal(40, lines.Length);
|
||||||
|
Assert.EndsWith("line10", lines[0]);
|
||||||
|
Assert.EndsWith("line49", lines[^1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLast_respects_active_warn_error_filter()
|
||||||
|
{
|
||||||
|
var t0 = new DateTime(2026, 6, 23, 8, 0, 0, DateTimeKind.Utc);
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(new[]
|
||||||
|
{
|
||||||
|
E(WorkerLogLevel.Info, "skipped", t0),
|
||||||
|
E(WorkerLogLevel.Warn, "kept", t0.AddMinutes(1)),
|
||||||
|
}));
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
vm.WarnErrorOnly = true;
|
||||||
|
|
||||||
|
var lines = vm.BuildCopyText().Split('\n');
|
||||||
|
|
||||||
|
Assert.Single(lines);
|
||||||
|
Assert.EndsWith("WARN kept", lines[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLastCommand_disabled_when_no_rows()
|
||||||
|
{
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(Array.Empty<WorkerLogEntry>()));
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
|
||||||
|
Assert.False(vm.CopyLastCommand.CanExecute(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLastCommand_copies_and_reports_count_on_success()
|
||||||
|
{
|
||||||
|
string? copied = null;
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(new[] { E(WorkerLogLevel.Info, "a") }),
|
||||||
|
text => { copied = text; return Task.FromResult(true); });
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
|
||||||
|
await vm.CopyLastCommand.ExecuteAsync(null);
|
||||||
|
|
||||||
|
Assert.EndsWith("INFO a", copied);
|
||||||
|
Assert.Contains("1", vm.StatusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyLastCommand_reports_failure_when_clipboard_unavailable()
|
||||||
|
{
|
||||||
|
var vm = new LogVisualizerViewModel(new FakeClient(new[] { E(WorkerLogLevel.Info, "a") }),
|
||||||
|
_ => Task.FromResult(false));
|
||||||
|
await vm.RefreshAsync();
|
||||||
|
|
||||||
|
await vm.CopyLastCommand.ExecuteAsync(null);
|
||||||
|
|
||||||
|
Assert.False(string.IsNullOrEmpty(vm.StatusText));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user