From 82516385c0746bb8f79d92f43b1345776f727b9a Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 23 Oct 2022 14:07:43 -0700 Subject: [PATCH] use lazycore stuff --- go.mod | 2 +- go.sum | 4 ++-- pkg/gui/gui.go | 4 ++-- pkg/gui/list_panel.go | 5 +++-- pkg/utils/once_writer_test.go | 19 ------------------- pkg/utils/utils.go | 9 --------- .../lazycore/pkg}/utils/once_writer.go | 1 - .../jesseduffield/lazycore/pkg/utils/utils.go | 10 ++++++++++ vendor/modules.txt | 2 +- 9 files changed, 19 insertions(+), 37 deletions(-) delete mode 100644 pkg/utils/once_writer_test.go rename {pkg => vendor/github.com/jesseduffield/lazycore/pkg}/utils/once_writer.go (93%) diff --git a/go.mod b/go.mod index fb741878..fb69eded 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/jesseduffield/asciigraph v0.0.0-20190605104717-6d88e39309ee github.com/jesseduffield/gocui v0.3.1-0.20221023185936-ef06450f4fdc github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 - github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93 + github.com/jesseduffield/lazycore v0.0.0-20221023210126-718a4caea996 github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56 github.com/mattn/go-runewidth v0.0.14 github.com/mcuadros/go-lookup v0.0.0-20171110082742-5650f26be767 diff --git a/go.sum b/go.sum index 84dd33e4..ebcf084b 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/jesseduffield/gocui v0.3.1-0.20221023185936-ef06450f4fdc h1:Gi/uDpmlB github.com/jesseduffield/gocui v0.3.1-0.20221023185936-ef06450f4fdc/go.mod h1:znJuCDnF2Ph40YZSlBwdX/4GEofnIoWLGdT4mK5zRAU= github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0= github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo= -github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93 h1:zY7ymCjXC7fZeJVXDpiicYx6c2YFgvsdZSOdch2f7gU= -github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ= +github.com/jesseduffield/lazycore v0.0.0-20221023210126-718a4caea996 h1:CH1en6GpXSwnXl5Ehc4WX1NpS3uw9qbi7o9A4T2YYmA= +github.com/jesseduffield/lazycore v0.0.0-20221023210126-718a4caea996/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ= github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56 h1:33wSxJWU/f2TAozHYtJ8zqBxEnEVYM+22moLoiAkxvg= github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56/go.mod h1:FZJBwOhE+RXz8EVZfY+xnbCw2cVOwxlK3/aIi581z/s= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 6b3e9478..127ba591 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -12,11 +12,11 @@ import ( throttle "github.com/boz/go-throttle" "github.com/jesseduffield/gocui" + lcUtils "github.com/jesseduffield/lazycore/pkg/utils" "github.com/jesseduffield/lazydocker/pkg/commands" "github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/i18n" "github.com/jesseduffield/lazydocker/pkg/tasks" - "github.com/jesseduffield/lazydocker/pkg/utils" "github.com/sasha-s/go-deadlock" "github.com/sirupsen/logrus" ) @@ -188,7 +188,7 @@ func (gui *Gui) Run() error { // if the deadlock package wants to report a deadlock, we first need to // close the gui so that we can actually read what it prints. - deadlock.Opts.LogBuf = utils.NewOnceWriter(os.Stderr, func() { + deadlock.Opts.LogBuf = lcUtils.NewOnceWriter(os.Stderr, func() { gui.g.Close() }) diff --git a/pkg/gui/list_panel.go b/pkg/gui/list_panel.go index 5c20b3a7..ad970425 100644 --- a/pkg/gui/list_panel.go +++ b/pkg/gui/list_panel.go @@ -6,6 +6,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/gocui" + lcUtils "github.com/jesseduffield/lazycore/pkg/utils" "github.com/jesseduffield/lazydocker/pkg/utils" "github.com/samber/lo" ) @@ -19,14 +20,14 @@ type ListPanel[T comparable] struct { func (self *ListPanel[T]) setSelectedLineIdx(value int) { clampedValue := 0 if self.list.Len() > 0 { - clampedValue = utils.Clamp(value, 0, self.list.Len()-1) + clampedValue = lcUtils.Clamp(value, 0, self.list.Len()-1) } self.selectedIdx = clampedValue } func (self *ListPanel[T]) clampSelectedLineIdx() { - clamped := utils.Clamp(self.selectedIdx, 0, self.list.Len()-1) + clamped := lcUtils.Clamp(self.selectedIdx, 0, self.list.Len()-1) if clamped != self.selectedIdx { self.selectedIdx = clamped diff --git a/pkg/utils/once_writer_test.go b/pkg/utils/once_writer_test.go deleted file mode 100644 index 47f64bb6..00000000 --- a/pkg/utils/once_writer_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package utils - -import ( - "bytes" - "testing" -) - -func TestOnceWriter(t *testing.T) { - innerWriter := bytes.NewBuffer(nil) - counter := 0 - onceWriter := NewOnceWriter(innerWriter, func() { - counter += 1 - }) - _, _ = onceWriter.Write([]byte("hello")) - _, _ = onceWriter.Write([]byte("hello")) - if counter != 1 { - t.Errorf("expected counter to be 1, got %d", counter) - } -} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0d87ceeb..be86bd22 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -335,15 +335,6 @@ func IsValidHexValue(v string) bool { return true } -func Clamp(x int, min int, max int) int { - if x < min { - return min - } else if x > max { - return max - } - return x -} - // Style used on menu items that open another menu func OpensMenuStyle(str string) string { return ColoredString(fmt.Sprintf("%s...", str), color.FgMagenta) diff --git a/pkg/utils/once_writer.go b/vendor/github.com/jesseduffield/lazycore/pkg/utils/once_writer.go similarity index 93% rename from pkg/utils/once_writer.go rename to vendor/github.com/jesseduffield/lazycore/pkg/utils/once_writer.go index 5a04c9f3..aecf2036 100644 --- a/pkg/utils/once_writer.go +++ b/vendor/github.com/jesseduffield/lazycore/pkg/utils/once_writer.go @@ -5,7 +5,6 @@ import ( "sync" ) -// TODO: extract into lazycore repo. // This wraps a writer and ensures that before we actually write anything we call a given function first type OnceWriter struct { diff --git a/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go index 9d0c2cc6..06d828e4 100644 --- a/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go +++ b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go @@ -22,6 +22,16 @@ func Max(x, y int) int { return y } +// Clamp returns a value x restricted between min and max +func Clamp(x int, min int, max int) int { + if x < min { + return min + } else if x > max { + return max + } + return x +} + // GetLazyRootDirectory finds a lazy project root directory. // // It's used for cheatsheet scripts and integration tests. Not to be confused with finding the diff --git a/vendor/modules.txt b/vendor/modules.txt index 14ca217f..a135a6d7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -118,7 +118,7 @@ github.com/jesseduffield/gocui # github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 ## explicit; go 1.18 github.com/jesseduffield/kill -# github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93 +# github.com/jesseduffield/lazycore v0.0.0-20221023210126-718a4caea996 ## explicit; go 1.18 github.com/jesseduffield/lazycore/pkg/boxlayout github.com/jesseduffield/lazycore/pkg/utils