<refactor>

This commit is contained in:
nsnail 2022-12-06 21:39:26 +08:00
parent db83230391
commit e9e94460a9

View File

@ -18,24 +18,27 @@ public class Main : ToolBase<Option>
{ {
_gitOutputEnc = Encoding.GetEncoding(Opt.GitOutputEncoding); _gitOutputEnc = Encoding.GetEncoding(Opt.GitOutputEncoding);
if (!Directory.Exists(Opt.Path)) if (!Directory.Exists(Opt.Path))
throw new ArgumentException(nameof(Opt.Path), string.Format(Str.PathNotFound, Opt.Path)); throw new ArgumentException(nameof(Opt.Path) //
, string.Format(Str.PathNotFound, Opt.Path));
} }
private async ValueTask DirHandle(string dir, CancellationToken cancelToken) private async ValueTask DirHandle(string dir, CancellationToken cancelToken)
{ {
var index = _repoPathList.FindIndex(x => x == dir); var row = _repoPathList.FindIndex(x => x == dir); // 行号
var tAnimate = LoadingAnimate(_POST_Y_LOADING, _cursorPosBackup.y + index, out var cts); var tAnimate = LoadingAnimate(_POST_Y_LOADING, _cursorPosBackup.y + row, out var cts);
// 打印 git command rsp
void ExecRspReceived(object sender, DataReceivedEventArgs e) void ExecRspReceived(object sender, DataReceivedEventArgs e)
{ {
if (e.Data is null) return; if (e.Data is null) return;
var msg = Encoding.UTF8.GetString(_gitOutputEnc.GetBytes(e.Data)); var msg = Encoding.UTF8.GetString(_gitOutputEnc.GetBytes(e.Data));
ConcurrentWrite(_POS_Y_MSG, _cursorPosBackup.y + index, new string(' ', Console.WindowWidth - _POS_Y_MSG)); ConcurrentWrite(_POS_Y_MSG, _cursorPosBackup.y + row, new string(' ', Console.WindowWidth - _POS_Y_MSG));
ConcurrentWrite(_POS_Y_MSG, _cursorPosBackup.y + index, msg); ConcurrentWrite(_POS_Y_MSG, _cursorPosBackup.y + row, msg);
} }
// 启动git进程
{
var startInfo = new ProcessStartInfo { var startInfo = new ProcessStartInfo {
CreateNoWindow = true CreateNoWindow = true
, WorkingDirectory = dir , WorkingDirectory = dir
@ -51,7 +54,7 @@ public class Main : ToolBase<Option>
p.BeginOutputReadLine(); p.BeginOutputReadLine();
p.BeginErrorReadLine(); p.BeginErrorReadLine();
await p.WaitForExitAsync(); await p.WaitForExitAsync();
}
cts.Cancel(); cts.Cancel();
await tAnimate; await tAnimate;
@ -59,13 +62,14 @@ public class Main : ToolBase<Option>
} }
private void StashCurorPos() private void StashCurorPos()
{ {
_cursorPosBackup = Console.GetCursorPosition(); _cursorPosBackup = Console.GetCursorPosition();
} }
public override async Task Run() public override async Task Run()
{
// 查找git仓库目录
{ {
Console.Write(Str.FindGitReps, Opt.Path); Console.Write(Str.FindGitReps, Opt.Path);
StashCurorPos(); StashCurorPos();
@ -74,17 +78,16 @@ public class Main : ToolBase<Option>
_repoPathList = Directory.GetDirectories(Opt.Path, ".git", SearchOption.AllDirectories) _repoPathList = Directory.GetDirectories(Opt.Path, ".git", SearchOption.AllDirectories)
.Select(x => Directory.GetParent(x)!.FullName) .Select(x => Directory.GetParent(x)!.FullName)
.ToList(); .ToList();
cts.Cancel(); cts.Cancel();
await tAnimate; await tAnimate;
cts.Dispose(); cts.Dispose();
}
// 打印git仓库目录
{
Console.WriteLine(Str.Ok); Console.WriteLine(Str.Ok);
StashCurorPos(); StashCurorPos();
{
var i = 0; var i = 0;
Console.WriteLine( // Console.WriteLine( //
string.Join(Environment.NewLine string.Join(Environment.NewLine
@ -94,9 +97,8 @@ public class Main : ToolBase<Option>
); );
} }
// 并行执行git命令
await Parallel.ForEachAsync(_repoPathList, DirHandle); await Parallel.ForEachAsync(_repoPathList, DirHandle);
Console.SetCursorPosition(_cursorPosBackup.x, _cursorPosBackup.y + _repoPathList.Count); Console.SetCursorPosition(_cursorPosBackup.x, _cursorPosBackup.y + _repoPathList.Count);
} }
} }