Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Aug 16, 2024
1 parent 9e6a42f commit b3c7aee
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 145 deletions.
60 changes: 30 additions & 30 deletions cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -107,26 +107,26 @@ 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 {
select {
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")
}
Expand All @@ -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")
}
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -324,7 +324,7 @@ func main() {
}
}()

termWidth := term.GetWidth()
termWidth := lterm.GetWidth()
m := &Main{
logger: log.New(io.Discard, "", 0),
termWidth: termWidth,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion check/context.go → context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

import (
"errors"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion check/hashfile.go → hashfile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

import (
"crypto/md5"
Expand Down
2 changes: 1 addition & 1 deletion check/ignore.go → ignore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion check/index.go → index.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

import (
"encoding/json"
Expand Down
2 changes: 2 additions & 0 deletions scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ fi
$script_dir/build

"$base_dir/chkbit" -u /tmp/chkbit
# todo: validate

2 changes: 1 addition & 1 deletion scripts/tests
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -e
script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

go test -v ./util
go test -v ./cmd/chkbit/util
2 changes: 1 addition & 1 deletion check/status.go → status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

type Status string

Expand Down
82 changes: 0 additions & 82 deletions term/term.go

This file was deleted.

21 changes: 0 additions & 21 deletions term/term_windows.go

This file was deleted.

2 changes: 1 addition & 1 deletion check/worker.go → worker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package chkbit

type WorkItem struct {
path string
Expand Down

0 comments on commit b3c7aee

Please sign in to comment.