Skip to content

Commit 63803c9

Browse files
committed
feature: show command running time in logs window (#1253)
Signed-off-by: leo <[email protected]>
1 parent 825b74c commit 63803c9

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

src/ViewModels/CommandLog.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public DateTime StartTime
1818
get;
1919
} = DateTime.Now;
2020

21+
public DateTime EndTime
22+
{
23+
get;
24+
private set;
25+
} = DateTime.Now;
26+
2127
public bool IsComplete
2228
{
2329
get;
@@ -64,6 +70,8 @@ public void Complete()
6470
_builder.Clear();
6571
_builder = null;
6672

73+
EndTime = DateTime.Now;
74+
6775
OnPropertyChanged(nameof(IsComplete));
6876

6977
if (_onNewLineReceived != null)

src/Views/CommandLogTime.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Threading;
3+
4+
using Avalonia;
5+
using Avalonia.Controls;
6+
using Avalonia.Interactivity;
7+
using Avalonia.Threading;
8+
9+
namespace SourceGit.Views
10+
{
11+
public class CommandLogTime : TextBlock
12+
{
13+
public static readonly StyledProperty<ViewModels.CommandLog> LogProperty =
14+
AvaloniaProperty.Register<CommandLogTime, ViewModels.CommandLog>(nameof(Log), null);
15+
16+
public ViewModels.CommandLog Log
17+
{
18+
get => GetValue(LogProperty);
19+
set => SetValue(LogProperty, value);
20+
}
21+
22+
protected override Type StyleKeyOverride => typeof(TextBlock);
23+
24+
protected override void OnUnloaded(RoutedEventArgs e)
25+
{
26+
base.OnUnloaded(e);
27+
StopTimer();
28+
}
29+
30+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
31+
{
32+
base.OnPropertyChanged(change);
33+
34+
if (change.Property == LogProperty)
35+
{
36+
StopTimer();
37+
38+
if (change.NewValue is ViewModels.CommandLog log)
39+
SetupCommandLog(log);
40+
else
41+
Text = string.Empty;
42+
}
43+
}
44+
45+
private void SetupCommandLog(ViewModels.CommandLog log)
46+
{
47+
Text = GetDisplayText(log);
48+
if (log.IsComplete)
49+
return;
50+
51+
_refreshTimer = new Timer(_ =>
52+
{
53+
Dispatcher.UIThread.Invoke(() =>
54+
{
55+
Text = GetDisplayText(log);
56+
if (log.IsComplete)
57+
StopTimer();
58+
});
59+
}, null, 0, 100);
60+
}
61+
62+
private void StopTimer()
63+
{
64+
if (_refreshTimer is { })
65+
{
66+
_refreshTimer.Dispose();
67+
_refreshTimer = null;
68+
}
69+
}
70+
71+
private string GetDisplayText(ViewModels.CommandLog log)
72+
{
73+
var endTime = log.IsComplete ? log.EndTime : DateTime.Now;
74+
var duration = (endTime - log.StartTime).ToString(@"hh\:mm\:ss\.fff");
75+
return $"{log.StartTime:T} ({duration})";
76+
}
77+
78+
private Timer _refreshTimer = null;
79+
}
80+
}

src/Views/ViewLogs.axaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<Grid.ColumnDefinitions>
7878
<ColumnDefinition Width="Auto"/>
7979
<ColumnDefinition Width="*"/>
80-
<ColumnDefinition Width="Auto" SharedSizeGroup="SearchCommitTimeColumn"/>
80+
<ColumnDefinition Width="Auto" SharedSizeGroup="TimeColumn"/>
8181
</Grid.ColumnDefinitions>
8282
<v:LoadingIcon Grid.Column="0"
8383
Width="14" Height="14"
@@ -91,12 +91,12 @@
9191
VerticalAlignment="Center"
9292
TextTrimming="CharacterEllipsis"/>
9393

94-
<TextBlock Grid.Column="2"
95-
Classes="primary"
96-
Margin="4,0"
97-
Foreground="{DynamicResource Brush.FG2}"
98-
Text="{Binding StartTime, StringFormat='{}{0:T}'}"
99-
HorizontalAlignment="Right" VerticalAlignment="Center"/>
94+
<v:CommandLogTime Grid.Column="2"
95+
Classes="primary"
96+
Margin="4,0"
97+
Foreground="{DynamicResource Brush.FG2}"
98+
Log="{Binding}"
99+
HorizontalAlignment="Right" VerticalAlignment="Center"/>
100100
</Grid>
101101
</DataTemplate>
102102
</ListBox.ItemTemplate>

0 commit comments

Comments
 (0)