From b3c7aeed4cb645bd1f4be22ec0361543abd7e570 Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Fri, 16 Aug 2024 23:10:58 +0200 Subject: [PATCH] refactor --- cmd/chkbit/main.go | 60 +++++++-------- {util => cmd/chkbit/util}/fm.go | 0 {util => cmd/chkbit/util}/lang.go | 0 {util => cmd/chkbit/util}/sparkline.go | 0 {util => cmd/chkbit/util}/sparkline_test.go | 0 {util => cmd/chkbit/util}/strings.go | 0 {util => cmd/chkbit/util}/strings_test.go | 0 check/context.go => context.go | 2 +- go.mod | 3 +- go.sum | 8 +- check/hashfile.go => hashfile.go | 2 +- check/ignore.go => ignore.go | 2 +- check/index.go => index.go | 2 +- scripts/run_tests | 2 + scripts/tests | 2 +- check/status.go => status.go | 2 +- term/term.go | 82 --------------------- term/term_windows.go | 21 ------ check/worker.go => worker.go | 2 +- 19 files changed, 45 insertions(+), 145 deletions(-) rename {util => cmd/chkbit/util}/fm.go (100%) rename {util => cmd/chkbit/util}/lang.go (100%) rename {util => cmd/chkbit/util}/sparkline.go (100%) rename {util => cmd/chkbit/util}/sparkline_test.go (100%) rename {util => cmd/chkbit/util}/strings.go (100%) rename {util => cmd/chkbit/util}/strings_test.go (100%) rename check/context.go => context.go (99%) rename check/hashfile.go => hashfile.go (98%) rename check/ignore.go => ignore.go (99%) rename check/index.go => index.go (99%) rename check/status.go => status.go (98%) delete mode 100644 term/term.go delete mode 100644 term/term_windows.go rename check/worker.go => worker.go (98%) diff --git a/cmd/chkbit/main.go b/cmd/chkbit/main.go index e93d8b8..540dc19 100644 --- a/cmd/chkbit/main.go +++ b/cmd/chkbit/main.go @@ -10,9 +10,9 @@ import ( "time" "github.com/alecthomas/kong" - "github.com/laktak/chkbit/check" - "github.com/laktak/chkbit/term" - "github.com/laktak/chkbit/util" + "github.com/laktak/chkbit" + "github.com/laktak/chkbit/cmd/chkbit/util" + "github.com/laktak/lterm" ) type Progress int @@ -31,14 +31,14 @@ const ( var appVersion = "vdev" var ( - termBG = term.Bg8(240) + termBG = lterm.Bg8(240) termSep = "|" - termSepFG = term.Fg8(235) - termFG1 = term.Fg8(255) - termFG2 = term.Fg8(228) - termFG3 = term.Fg8(202) - termOKFG = term.Fg4(2) - termAlertFG = term.Fg4(1) + termSepFG = lterm.Fg8(235) + termFG1 = lterm.Fg8(255) + termFG2 = lterm.Fg8(228) + termFG3 = lterm.Fg8(202) + termOKFG = lterm.Fg4(2) + termAlertFG = lterm.Fg4(1) ) var cli struct { @@ -80,25 +80,25 @@ func (m *Main) log(text string) { m.logger.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), text) } -func (m *Main) logStatus(stat check.Status, path string) { - if stat == check.STATUS_UPDATE_INDEX { +func (m *Main) logStatus(stat chkbit.Status, path string) { + if stat == chkbit.STATUS_UPDATE_INDEX { m.numIdxUpd++ } else { - if stat == check.STATUS_ERR_DMG { + if stat == chkbit.STATUS_ERR_DMG { m.total++ m.dmgList = append(m.dmgList, path) - } else if stat == check.STATUS_PANIC { + } else if stat == chkbit.STATUS_PANIC { m.errList = append(m.errList, path) - } else if stat == check.STATUS_OK || stat == check.STATUS_UPDATE || stat == check.STATUS_NEW { + } else if stat == chkbit.STATUS_OK || stat == chkbit.STATUS_UPDATE || stat == chkbit.STATUS_NEW { m.total++ - if stat == check.STATUS_UPDATE { + if stat == chkbit.STATUS_UPDATE { m.numUpd++ - } else if stat == check.STATUS_NEW { + } else if stat == chkbit.STATUS_NEW { m.numNew++ } } - if m.logVerbose || stat != check.STATUS_OK && stat != check.STATUS_IGNORE { + if m.logVerbose || stat != chkbit.STATUS_OK && stat != chkbit.STATUS_IGNORE { m.log(stat.String() + " " + path) } @@ -107,12 +107,12 @@ func (m *Main) logStatus(stat check.Status, path string) { if stat.IsErrorOrWarning() { col = termAlertFG } - term.Printline(col, stat.String(), " ", path, term.Reset) + lterm.Printline(col, stat.String(), " ", path, lterm.Reset) } } } -func (m *Main) showStatus(context *check.Context) { +func (m *Main) showStatus(context *chkbit.Context) { last := time.Now().Add(-updateInterval) stat := "" for { @@ -120,13 +120,13 @@ func (m *Main) showStatus(context *check.Context) { case item := <-context.LogQueue: if item == nil { if m.progress == Fancy { - term.Printline("") + lterm.Printline("") } return } m.logStatus(item.Stat, item.Message) if m.progress == Fancy { - term.Write(termBG, termFG1, stat, term.ClearLine(0), term.Reset, "\r") + lterm.Write(termBG, termFG1, stat, lterm.ClearLine(0), lterm.Reset, "\r") } else { fmt.Print(m.total, "\r") } @@ -150,7 +150,7 @@ func (m *Main) showStatus(context *check.Context) { stat = util.LeftTruncate(stat, m.termWidth-1) stat = strings.Replace(stat, "$", termSepFG+termSep+termFG2, 1) stat = strings.Replace(stat, "$", termSepFG+termSep+termFG3, 1) - term.Write(termBG, termFG1, stat, term.ClearLine(0), term.Reset, "\r") + lterm.Write(termBG, termFG1, stat, lterm.ClearLine(0), lterm.Reset, "\r") } else if m.progress == Plain { fmt.Print(m.total, "\r") } @@ -159,13 +159,13 @@ func (m *Main) showStatus(context *check.Context) { } } -func (m *Main) process() *check.Context { +func (m *Main) process() *chkbit.Context { if cli.Update && cli.ShowIgnoredOnly { fmt.Println("Error: use either --update or --show-ignored-only!") return nil } - context, err := check.NewContext(cli.Workers, cli.Force, cli.Update, cli.ShowIgnoredOnly, cli.Algo, cli.SkipSymlinks, cli.IndexName, cli.IgnoreName) + context, err := chkbit.NewContext(cli.Workers, cli.Force, cli.Update, cli.ShowIgnoredOnly, cli.Algo, cli.SkipSymlinks, cli.IndexName, cli.IgnoreName) if err != nil { fmt.Println(err) return nil @@ -183,11 +183,11 @@ func (m *Main) process() *check.Context { return context } -func (m *Main) printResult(context *check.Context) { +func (m *Main) printResult(context *chkbit.Context) { cprint := func(col, text string) { if m.progress != Quiet { if m.progress == Fancy { - term.Printline(col, text, term.Reset) + lterm.Printline(col, text, lterm.Reset) } else { fmt.Println(text) } @@ -196,9 +196,9 @@ func (m *Main) printResult(context *check.Context) { eprint := func(col, text string) { if m.progress == Fancy { - term.Write(col) + lterm.Write(col) fmt.Fprintln(os.Stderr, text) - term.Write(term.Reset) + lterm.Write(lterm.Reset) } else { fmt.Fprintln(os.Stderr, text) } @@ -324,7 +324,7 @@ func main() { } }() - termWidth := term.GetWidth() + termWidth := lterm.GetWidth() m := &Main{ logger: log.New(io.Discard, "", 0), termWidth: termWidth, diff --git a/util/fm.go b/cmd/chkbit/util/fm.go similarity index 100% rename from util/fm.go rename to cmd/chkbit/util/fm.go diff --git a/util/lang.go b/cmd/chkbit/util/lang.go similarity index 100% rename from util/lang.go rename to cmd/chkbit/util/lang.go diff --git a/util/sparkline.go b/cmd/chkbit/util/sparkline.go similarity index 100% rename from util/sparkline.go rename to cmd/chkbit/util/sparkline.go diff --git a/util/sparkline_test.go b/cmd/chkbit/util/sparkline_test.go similarity index 100% rename from util/sparkline_test.go rename to cmd/chkbit/util/sparkline_test.go diff --git a/util/strings.go b/cmd/chkbit/util/strings.go similarity index 100% rename from util/strings.go rename to cmd/chkbit/util/strings.go diff --git a/util/strings_test.go b/cmd/chkbit/util/strings_test.go similarity index 100% rename from util/strings_test.go rename to cmd/chkbit/util/strings_test.go diff --git a/check/context.go b/context.go similarity index 99% rename from check/context.go rename to context.go index 08b9186..203485c 100644 --- a/check/context.go +++ b/context.go @@ -1,4 +1,4 @@ -package check +package chkbit import ( "errors" diff --git a/go.mod b/go.mod index 1749bb0..1c8d7a4 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,12 @@ go 1.22.3 require ( github.com/alecthomas/kong v0.9.0 - golang.org/x/sys v0.23.0 + github.com/laktak/lterm v1.0.0 lukechampine.com/blake3 v1.3.0 ) require ( github.com/klauspost/cpuid/v2 v2.0.9 // indirect + golang.org/x/sys v0.24.0 // indirect golang.org/x/term v0.23.0 // indirect ) diff --git a/go.sum b/go.sum index de934f0..1df663f 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,10 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +github.com/laktak/lterm v1.0.0 h1:hxRsBDHIWlMn+IV7C+6/tk4y81WqcO8F6G+52wSZUf4= +github.com/laktak/lterm v1.0.0/go.mod h1:zwGyZi5PNuySqsDsRVNvBBYANy9k61oYgW6Flsm2AZg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= lukechampine.com/blake3 v1.3.0 h1:sJ3XhFINmHSrYCgl958hscfIa3bw8x4DqMP3u1YvoYE= diff --git a/check/hashfile.go b/hashfile.go similarity index 98% rename from check/hashfile.go rename to hashfile.go index 99b5414..4e3f12a 100644 --- a/check/hashfile.go +++ b/hashfile.go @@ -1,4 +1,4 @@ -package check +package chkbit import ( "crypto/md5" diff --git a/check/ignore.go b/ignore.go similarity index 99% rename from check/ignore.go rename to ignore.go index 1dcf12b..b60b497 100644 --- a/check/ignore.go +++ b/ignore.go @@ -1,4 +1,4 @@ -package check +package chkbit import ( "bufio" diff --git a/check/index.go b/index.go similarity index 99% rename from check/index.go rename to index.go index bde77d6..aa6a3c9 100644 --- a/check/index.go +++ b/index.go @@ -1,4 +1,4 @@ -package check +package chkbit import ( "encoding/json" diff --git a/scripts/run_tests b/scripts/run_tests index 3753676..fb708b5 100755 --- a/scripts/run_tests +++ b/scripts/run_tests @@ -17,3 +17,5 @@ fi $script_dir/build "$base_dir/chkbit" -u /tmp/chkbit +# todo: validate + diff --git a/scripts/tests b/scripts/tests index 5f3faa3..08aa460 100755 --- a/scripts/tests +++ b/scripts/tests @@ -4,4 +4,4 @@ set -e script_dir=$(dirname "$(realpath "$0")") cd $script_dir/.. -go test -v ./util +go test -v ./cmd/chkbit/util diff --git a/check/status.go b/status.go similarity index 98% rename from check/status.go rename to status.go index a3bfa2d..48fdf8f 100644 --- a/check/status.go +++ b/status.go @@ -1,4 +1,4 @@ -package check +package chkbit type Status string diff --git a/term/term.go b/term/term.go deleted file mode 100644 index 3d6c2d1..0000000 --- a/term/term.go +++ /dev/null @@ -1,82 +0,0 @@ -package term - -import ( - "fmt" - "os" - - "golang.org/x/term" -) - -var ( - isTerm = false - noColor = false - stdoutFd = 0 -) - -func init() { - stdoutFd = int(os.Stdout.Fd()) - isTerm = term.IsTerminal(stdoutFd) - if isTerm { - noColor = os.Getenv("NO_COLOR") != "" - } else { - noColor = true - } -} - -const ( - Reset = "\033[0m" - Bold = "\033[01m" - Disable = "\033[02m" - Underline = "\033[04m" - Reverse = "\033[07m" - Strikethrough = "\033[09m" - Invisible = "\033[08m" -) - -func Write(text ...interface{}) { - fmt.Print(text...) -} - -func Printline(text ...interface{}) { - fmt.Print(text...) - fmt.Println(ClearLine(0)) -} - -func Fg4(col int) string { - if noColor { - return "" - } - if col < 8 { - return fmt.Sprintf("\033[%dm", 30+col) - } - return fmt.Sprintf("\033[%dm", 90-8+col) -} - -func Fg8(col int) string { - if noColor { - return "" - } - return fmt.Sprintf("\033[38;5;%dm", col) -} - -func Bg8(col int) string { - if noColor { - return "" - } - return fmt.Sprintf("\033[48;5;%dm", col) -} - -func ClearLine(opt int) string { - // 0=to end, 1=from start, 2=all - return fmt.Sprintf("\033[%dK", opt) -} - -func GetWidth() int { - if isTerm { - width, _, err := term.GetSize(stdoutFd) - if err == nil { - return width - } - } - return 80 -} diff --git a/term/term_windows.go b/term/term_windows.go deleted file mode 100644 index 933e932..0000000 --- a/term/term_windows.go +++ /dev/null @@ -1,21 +0,0 @@ -package term - -import ( - "os" - - "golang.org/x/sys/windows" -) - -// from https://github.com/fatih/color - -func init() { - // Opt-in for ansi color support for current process. - // https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#output-sequences - var outMode uint32 - out := windows.Handle(os.Stdout.Fd()) - if err := windows.GetConsoleMode(out, &outMode); err != nil { - return - } - outMode |= windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING - _ = windows.SetConsoleMode(out, outMode) -} diff --git a/check/worker.go b/worker.go similarity index 98% rename from check/worker.go rename to worker.go index eff0c7a..d00da7c 100644 --- a/check/worker.go +++ b/worker.go @@ -1,4 +1,4 @@ -package check +package chkbit type WorkItem struct { path string