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