<refactor>

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

View File

@ -18,40 +18,43 @@ 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 { {
CreateNoWindow = true var startInfo = new ProcessStartInfo {
, WorkingDirectory = dir CreateNoWindow = true
, FileName = "git" , WorkingDirectory = dir
, Arguments = Opt.Args , FileName = "git"
, UseShellExecute = false , Arguments = Opt.Args
, RedirectStandardOutput = true , UseShellExecute = false
, RedirectStandardError = true , RedirectStandardOutput = true
}; , RedirectStandardError = true
using var p = Process.Start(startInfo); };
p!.OutputDataReceived += ExecRspReceived; using var p = Process.Start(startInfo);
p.ErrorDataReceived += ExecRspReceived; p!.OutputDataReceived += ExecRspReceived;
p.BeginOutputReadLine(); p.ErrorDataReceived += ExecRspReceived;
p.BeginErrorReadLine(); p.BeginOutputReadLine();
await p.WaitForExitAsync(); p.BeginErrorReadLine();
await p.WaitForExitAsync();
}
cts.Cancel(); cts.Cancel();
await tAnimate; await tAnimate;
@ -59,7 +62,6 @@ public class Main : ToolBase<Option>
} }
private void StashCurorPos() private void StashCurorPos()
{ {
_cursorPosBackup = Console.GetCursorPosition(); _cursorPosBackup = Console.GetCursorPosition();
} }
@ -67,24 +69,25 @@ public class Main : ToolBase<Option>
public override async Task Run() public override async Task Run()
{ {
Console.Write(Str.FindGitReps, Opt.Path); // 查找git仓库目录
StashCurorPos();
var tAnimate = LoadingAnimate(_cursorPosBackup.x, _cursorPosBackup.y, out var cts);
_repoPathList = Directory.GetDirectories(Opt.Path, ".git", SearchOption.AllDirectories)
.Select(x => Directory.GetParent(x)!.FullName)
.ToList();
cts.Cancel();
await tAnimate;
cts.Dispose();
Console.WriteLine(Str.Ok);
StashCurorPos();
{ {
Console.Write(Str.FindGitReps, Opt.Path);
StashCurorPos();
var tAnimate = LoadingAnimate(_cursorPosBackup.x, _cursorPosBackup.y, out var cts);
_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; 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);
} }
} }