From 7156cc5f6aae4d560bb47c35afd157bc99b7f18e Mon Sep 17 00:00:00 2001 From: HAYAMA_Kaoru <56809588+zat-kaoru-hayama@users.noreply.github.com> Date: Fri, 8 Oct 2021 19:00:50 +0900 Subject: [PATCH] First commit --- go.mod | 9 +++++++++ go.sum | 7 +++++++ main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1bcec60 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/zat-kaoru-hayama/WatchTemp + +go 1.17 + +require ( + github.com/mattn/go-colorable v0.1.11 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2721be9 --- /dev/null +++ b/go.sum @@ -0,0 +1,7 @@ +github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs= +github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..016370c --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" + "time" + + "github.com/mattn/go-colorable" +) + +const ( + _STAMP_STYLE = "15:04:05.000" +) + +func mains() error { + if dispose := colorable.EnableColorsStdout(nil); dispose != nil { + defer dispose() + } + out := colorable.NewColorableStdout() + + tempPath := os.TempDir() + previous := make(map[string]struct{}) + filepath.Walk(tempPath, func(_path string, info fs.FileInfo, err error) error { + path := _path[len(tempPath):] + previous[path] = struct{}{} + return nil + }) + + tick := time.Tick(time.Second / 5) + for next := range tick { + current := make(map[string]struct{}) + stamp := next.Format(_STAMP_STYLE) + + filepath.Walk(tempPath, func(_path string, info fs.FileInfo, err error) error { + path := _path[len(tempPath):] + if _, ok := previous[path]; ok { + delete(previous, path) + } else { + fmt.Fprintf(out, "\x1B[32;1m%s Add %s\x1B[0m\n", stamp, path) + } + current[path] = struct{}{} + return nil + }) + for path := range previous { + fmt.Fprintf(out, "\x1B[31;1m%s Del %s\x1B[0m\n", stamp, path) + } + previous = current + } + return nil +} + +func main() { + if err := mains(); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } +}