refactor
This commit is contained in:
parent
9e6a42f626
commit
b3c7aeed4c
@ -10,9 +10,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
"github.com/laktak/chkbit/check"
|
"github.com/laktak/chkbit"
|
||||||
"github.com/laktak/chkbit/term"
|
"github.com/laktak/chkbit/cmd/chkbit/util"
|
||||||
"github.com/laktak/chkbit/util"
|
"github.com/laktak/lterm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Progress int
|
type Progress int
|
||||||
@ -31,14 +31,14 @@ const (
|
|||||||
|
|
||||||
var appVersion = "vdev"
|
var appVersion = "vdev"
|
||||||
var (
|
var (
|
||||||
termBG = term.Bg8(240)
|
termBG = lterm.Bg8(240)
|
||||||
termSep = "|"
|
termSep = "|"
|
||||||
termSepFG = term.Fg8(235)
|
termSepFG = lterm.Fg8(235)
|
||||||
termFG1 = term.Fg8(255)
|
termFG1 = lterm.Fg8(255)
|
||||||
termFG2 = term.Fg8(228)
|
termFG2 = lterm.Fg8(228)
|
||||||
termFG3 = term.Fg8(202)
|
termFG3 = lterm.Fg8(202)
|
||||||
termOKFG = term.Fg4(2)
|
termOKFG = lterm.Fg4(2)
|
||||||
termAlertFG = term.Fg4(1)
|
termAlertFG = lterm.Fg4(1)
|
||||||
)
|
)
|
||||||
|
|
||||||
var cli struct {
|
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)
|
m.logger.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Main) logStatus(stat check.Status, path string) {
|
func (m *Main) logStatus(stat chkbit.Status, path string) {
|
||||||
if stat == check.STATUS_UPDATE_INDEX {
|
if stat == chkbit.STATUS_UPDATE_INDEX {
|
||||||
m.numIdxUpd++
|
m.numIdxUpd++
|
||||||
} else {
|
} else {
|
||||||
if stat == check.STATUS_ERR_DMG {
|
if stat == chkbit.STATUS_ERR_DMG {
|
||||||
m.total++
|
m.total++
|
||||||
m.dmgList = append(m.dmgList, path)
|
m.dmgList = append(m.dmgList, path)
|
||||||
} else if stat == check.STATUS_PANIC {
|
} else if stat == chkbit.STATUS_PANIC {
|
||||||
m.errList = append(m.errList, path)
|
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++
|
m.total++
|
||||||
if stat == check.STATUS_UPDATE {
|
if stat == chkbit.STATUS_UPDATE {
|
||||||
m.numUpd++
|
m.numUpd++
|
||||||
} else if stat == check.STATUS_NEW {
|
} else if stat == chkbit.STATUS_NEW {
|
||||||
m.numNew++
|
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)
|
m.log(stat.String() + " " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,12 +107,12 @@ func (m *Main) logStatus(stat check.Status, path string) {
|
|||||||
if stat.IsErrorOrWarning() {
|
if stat.IsErrorOrWarning() {
|
||||||
col = termAlertFG
|
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)
|
last := time.Now().Add(-updateInterval)
|
||||||
stat := ""
|
stat := ""
|
||||||
for {
|
for {
|
||||||
@ -120,13 +120,13 @@ func (m *Main) showStatus(context *check.Context) {
|
|||||||
case item := <-context.LogQueue:
|
case item := <-context.LogQueue:
|
||||||
if item == nil {
|
if item == nil {
|
||||||
if m.progress == Fancy {
|
if m.progress == Fancy {
|
||||||
term.Printline("")
|
lterm.Printline("")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.logStatus(item.Stat, item.Message)
|
m.logStatus(item.Stat, item.Message)
|
||||||
if m.progress == Fancy {
|
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 {
|
} else {
|
||||||
fmt.Print(m.total, "\r")
|
fmt.Print(m.total, "\r")
|
||||||
}
|
}
|
||||||
@ -150,7 +150,7 @@ func (m *Main) showStatus(context *check.Context) {
|
|||||||
stat = util.LeftTruncate(stat, m.termWidth-1)
|
stat = util.LeftTruncate(stat, m.termWidth-1)
|
||||||
stat = strings.Replace(stat, "$", termSepFG+termSep+termFG2, 1)
|
stat = strings.Replace(stat, "$", termSepFG+termSep+termFG2, 1)
|
||||||
stat = strings.Replace(stat, "$", termSepFG+termSep+termFG3, 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 {
|
} else if m.progress == Plain {
|
||||||
fmt.Print(m.total, "\r")
|
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 {
|
if cli.Update && cli.ShowIgnoredOnly {
|
||||||
fmt.Println("Error: use either --update or --show-ignored-only!")
|
fmt.Println("Error: use either --update or --show-ignored-only!")
|
||||||
return nil
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil
|
return nil
|
||||||
@ -183,11 +183,11 @@ func (m *Main) process() *check.Context {
|
|||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Main) printResult(context *check.Context) {
|
func (m *Main) printResult(context *chkbit.Context) {
|
||||||
cprint := func(col, text string) {
|
cprint := func(col, text string) {
|
||||||
if m.progress != Quiet {
|
if m.progress != Quiet {
|
||||||
if m.progress == Fancy {
|
if m.progress == Fancy {
|
||||||
term.Printline(col, text, term.Reset)
|
lterm.Printline(col, text, lterm.Reset)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(text)
|
fmt.Println(text)
|
||||||
}
|
}
|
||||||
@ -196,9 +196,9 @@ func (m *Main) printResult(context *check.Context) {
|
|||||||
|
|
||||||
eprint := func(col, text string) {
|
eprint := func(col, text string) {
|
||||||
if m.progress == Fancy {
|
if m.progress == Fancy {
|
||||||
term.Write(col)
|
lterm.Write(col)
|
||||||
fmt.Fprintln(os.Stderr, text)
|
fmt.Fprintln(os.Stderr, text)
|
||||||
term.Write(term.Reset)
|
lterm.Write(lterm.Reset)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(os.Stderr, text)
|
fmt.Fprintln(os.Stderr, text)
|
||||||
}
|
}
|
||||||
@ -324,7 +324,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
termWidth := term.GetWidth()
|
termWidth := lterm.GetWidth()
|
||||||
m := &Main{
|
m := &Main{
|
||||||
logger: log.New(io.Discard, "", 0),
|
logger: log.New(io.Discard, "", 0),
|
||||||
termWidth: termWidth,
|
termWidth: termWidth,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
3
go.mod
3
go.mod
@ -4,11 +4,12 @@ go 1.22.3
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alecthomas/kong v0.9.0
|
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
|
lukechampine.com/blake3 v1.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
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
|
golang.org/x/term v0.23.0 // indirect
|
||||||
)
|
)
|
||||||
|
8
go.sum
8
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/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 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
github.com/laktak/lterm v1.0.0 h1:hxRsBDHIWlMn+IV7C+6/tk4y81WqcO8F6G+52wSZUf4=
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
github.com/laktak/lterm v1.0.0/go.mod h1:zwGyZi5PNuySqsDsRVNvBBYANy9k61oYgW6Flsm2AZg=
|
||||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
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 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
|
||||||
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
|
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
|
||||||
lukechampine.com/blake3 v1.3.0 h1:sJ3XhFINmHSrYCgl958hscfIa3bw8x4DqMP3u1YvoYE=
|
lukechampine.com/blake3 v1.3.0 h1:sJ3XhFINmHSrYCgl958hscfIa3bw8x4DqMP3u1YvoYE=
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@ -17,3 +17,5 @@ fi
|
|||||||
$script_dir/build
|
$script_dir/build
|
||||||
|
|
||||||
"$base_dir/chkbit" -u /tmp/chkbit
|
"$base_dir/chkbit" -u /tmp/chkbit
|
||||||
|
# todo: validate
|
||||||
|
|
||||||
|
@ -4,4 +4,4 @@ set -e
|
|||||||
script_dir=$(dirname "$(realpath "$0")")
|
script_dir=$(dirname "$(realpath "$0")")
|
||||||
cd $script_dir/..
|
cd $script_dir/..
|
||||||
|
|
||||||
go test -v ./util
|
go test -v ./cmd/chkbit/util
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
type Status string
|
type Status string
|
||||||
|
|
82
term/term.go
82
term/term.go
@ -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
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package check
|
package chkbit
|
||||||
|
|
||||||
type WorkItem struct {
|
type WorkItem struct {
|
||||||
path string
|
path string
|
Loading…
x
Reference in New Issue
Block a user