feature: show command running time in logs window (#1253)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-04-29 16:36:05 +08:00
parent 825b74c2a3
commit 63803c9b88
No known key found for this signature in database
3 changed files with 95 additions and 7 deletions

View file

@ -18,6 +18,12 @@ namespace SourceGit.ViewModels
get;
} = DateTime.Now;
public DateTime EndTime
{
get;
private set;
} = DateTime.Now;
public bool IsComplete
{
get;
@ -64,6 +70,8 @@ namespace SourceGit.ViewModels
_builder.Clear();
_builder = null;
EndTime = DateTime.Now;
OnPropertyChanged(nameof(IsComplete));
if (_onNewLineReceived != null)

View file

@ -0,0 +1,80 @@
using System;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
namespace SourceGit.Views
{
public class CommandLogTime : TextBlock
{
public static readonly StyledProperty<ViewModels.CommandLog> LogProperty =
AvaloniaProperty.Register<CommandLogTime, ViewModels.CommandLog>(nameof(Log), null);
public ViewModels.CommandLog Log
{
get => GetValue(LogProperty);
set => SetValue(LogProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
StopTimer();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == LogProperty)
{
StopTimer();
if (change.NewValue is ViewModels.CommandLog log)
SetupCommandLog(log);
else
Text = string.Empty;
}
}
private void SetupCommandLog(ViewModels.CommandLog log)
{
Text = GetDisplayText(log);
if (log.IsComplete)
return;
_refreshTimer = new Timer(_ =>
{
Dispatcher.UIThread.Invoke(() =>
{
Text = GetDisplayText(log);
if (log.IsComplete)
StopTimer();
});
}, null, 0, 100);
}
private void StopTimer()
{
if (_refreshTimer is { })
{
_refreshTimer.Dispose();
_refreshTimer = null;
}
}
private string GetDisplayText(ViewModels.CommandLog log)
{
var endTime = log.IsComplete ? log.EndTime : DateTime.Now;
var duration = (endTime - log.StartTime).ToString(@"hh\:mm\:ss\.fff");
return $"{log.StartTime:T} ({duration})";
}
private Timer _refreshTimer = null;
}
}

View file

@ -77,7 +77,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="SearchCommitTimeColumn"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="TimeColumn"/>
</Grid.ColumnDefinitions>
<v:LoadingIcon Grid.Column="0"
Width="14" Height="14"
@ -91,11 +91,11 @@
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"/>
<TextBlock Grid.Column="2"
<v:CommandLogTime Grid.Column="2"
Classes="primary"
Margin="4,0"
Foreground="{DynamicResource Brush.FG2}"
Text="{Binding StartTime, StringFormat='{}{0:T}'}"
Log="{Binding}"
HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>