feature: git command logs

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-04-17 12:30:20 +08:00
parent 928a0ad3c5
commit 8b39df32cc
No known key found for this signature in database
101 changed files with 1040 additions and 573 deletions

View file

@ -0,0 +1,87 @@
using System;
using System.Text;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class CommandLog : ObservableObject, Models.ICommandLog
{
public string Name
{
get;
private set;
} = string.Empty;
public DateTime Time
{
get;
} = DateTime.Now;
public string TimeStr
{
get => Time.ToString("T");
}
public bool IsComplete
{
get;
private set;
} = false;
public string Content
{
get
{
return IsComplete ? _content : _builder.ToString();
}
}
public CommandLog(string name)
{
Name = name;
}
public void Register(Action<string> handler)
{
if (!IsComplete)
_onNewLineReceived += handler;
}
public void AppendLine(string line = null)
{
var newline = line ?? string.Empty;
Dispatcher.UIThread.Invoke(() =>
{
_builder.AppendLine(newline);
_onNewLineReceived?.Invoke(newline);
});
}
public void Complete()
{
IsComplete = true;
Dispatcher.UIThread.Invoke(() =>
{
_content = _builder.ToString();
_builder.Clear();
_builder = null;
OnPropertyChanged(nameof(IsComplete));
if (_onNewLineReceived != null)
{
var dumpHandlers = _onNewLineReceived.GetInvocationList();
foreach (var d in dumpHandlers)
_onNewLineReceived -= (Action<string>)d;
}
});
}
private string _content = string.Empty;
private StringBuilder _builder = new StringBuilder();
private event Action<string> _onNewLineReceived;
}
}