Skip to content

Commit

Permalink
优化输出
Browse files Browse the repository at this point in the history
  • Loading branch information
jqk committed Sep 12, 2023
1 parent 6e16b78 commit e0cff25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
35 changes: 17 additions & 18 deletions file-diff-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@ import (
"time"
)

const (
ArgumentError = 1
RuntimeError = 2
)

func main() {
showVersion()

argCount := len(os.Args)

if argCount == 1 {
showHelp()
} else if argCount != 2 {
showError("Argument error", errors.New("wrong number of argument"), true)
os.Exit(1)
os.Exit(ArgumentError)
} else {
config, err := filediff.LoadConfigFromFile(os.Args[1])
if err != nil {
showError("Load config error", err, false)
os.Exit(2)
os.Exit(RuntimeError)
}

var groupResult *filediff.GroupResult = nil
var scanBaseResult, scanTargetResult *filediff.ScanResult = nil, nil
lastCount := 0
lastCount, count := 0, 0
done := make(chan struct{}) // 用于协程同步的通道。
fileScanedHandler := func(*filediff.FileIdentity) {
count++
}

go func() { // 启动协程执行任务,主线程负责输出工作状态及结果。
if strings.EqualFold(config.Action, filediff.ActionCompare) {
Expand Down Expand Up @@ -57,32 +64,24 @@ func main() {
select {
case <-done: // 等待扫描协程工作结束。
if err != nil { // 错误已经在协程结束前打印过了。
os.Exit(2)
os.Exit(RuntimeError)
} else if groupResult != nil {
showGroupResult(groupResult, config)
} else if scanBaseResult != nil {
showScanResult(scanBaseResult, config, config.CompareBase.ScanResultFile)
} else if scanTargetResult != nil {
showScanResult(scanTargetResult, config, config.CompareTarget.ScanResultFile)
}
return
default: // 打印扫描进度。
// 避免扫描文件时由于 IO 等待或文件很大,造成处理时间超过循环等待时间,进而重复显示同一数量。
if lastCount != countOfFiles {
lastCount = countOfFiles
showScanProgress(countOfFiles)
if lastCount != count {
lastCount = count
showScanProgress(count)
}
time.Sleep(sleepTime)
}
}
}
}

var lastFileIdentity *filediff.FileIdentity = nil // 暂时不知有什么用,先保留。
var countOfFiles = 0

func fileScanedHandler(fileIdentity *filediff.FileIdentity) {
if lastFileIdentity == nil || lastFileIdentity.Filename != fileIdentity.Filename {
lastFileIdentity = fileIdentity
countOfFiles++
}
// 默认结束并退出,相当于 os.Exit(0)。
}
2 changes: 1 addition & 1 deletion file-diff-cli/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func showVersion() {
fmt.Println()
fmt.Println("Copyright (c) 1999-2023 Not a dream Co., Ltd.")
fmt.Println("file difference grouper (fdg) 0.9.1, 2023-09-13")
fmt.Println("file difference grouper (fdg) 0.9.2, 2023-09-13")
fmt.Println()
}

Expand Down

0 comments on commit e0cff25

Please sign in to comment.