diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-10-09 08:31:14 -0700 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-10-09 08:31:14 -0700 |
commit | dba0edb9987b16738fb4b1c07e681388a84ce52d (patch) | |
tree | 935efd9877ca12c7757d98632d5bc89d159ab358 /vendor | |
parent | 7b4b42abd6f458eb667d322ec870bc6b20024812 (diff) |
use boxlayout from lazycore
Diffstat (limited to 'vendor')
36 files changed, 4548 insertions, 409 deletions
diff --git a/vendor/github.com/jesseduffield/lazycore/LICENSE b/vendor/github.com/jesseduffield/lazycore/LICENSE new file mode 100644 index 000000000..2a7175dcc --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Jesse Duffield + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go b/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go new file mode 100644 index 000000000..7cacfff98 --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go @@ -0,0 +1,212 @@ +package boxlayout + +import ( + "github.com/jesseduffield/generics/slices" + "github.com/jesseduffield/lazycore/pkg/utils" + "github.com/samber/lo" +) + +type Dimensions struct { + X0 int + X1 int + Y0 int + Y1 int +} + +type Direction int + +const ( + ROW Direction = iota + COLUMN +) + +// to give a high-level explanation of what's going on here. We layout our windows by arranging a bunch of boxes in the available space. +// If a box has children, it needs to specify how it wants to arrange those children: ROW or COLUMN. +// If a box represents a window, you can put the window name in the Window field. +// When determining how to divvy-up the available height (for row children) or width (for column children), we first +// give the boxes with a static `size` the space that they want. Then we apportion +// the remaining space based on the weights of the dynamic boxes (you can't define +// both size and weight at the same time: you gotta pick one). If there are two +// boxes, one with weight 1 and the other with weight 2, the first one gets 33% +// of the available space and the second one gets the remaining 66% + +type Box struct { + // Direction decides how the children boxes are laid out. ROW means the children will each form a row i.e. that they will be stacked on top of eachother. + Direction Direction + + // function which takes the width and height assigned to the box and decides which orientation it will have + ConditionalDirection func(width int, height int) Direction + + Children []*Box + + // function which takes the width and height assigned to the box and decides the layout of the children. + ConditionalChildren func(width int, height int) []*Box + + // Window refers to the name of the window this box represents, if there is one + Window string + + // static Size. If parent box's direction is ROW this refers to height, otherwise width + Size int + + // dynamic size. Once all statically sized children have been considered, Weight decides how much of the remaining space will be taken up by the box + // TODO: consider making there be one int and a type enum so we can't have size and Weight simultaneously defined + Weight int +} + +func ArrangeWindows(root *Box, x0, y0, width, height int) map[string]Dimensions { + children := root.getChildren(width, height) + if len(children) == 0 { + // leaf node + if root.Window != "" { + dimensionsForWindow := Dimensions{X0: x0, Y0: y0, X1: x0 + width - 1, Y1: y0 + height - 1} + return map[string]Dimensions{root.Window: dimensionsForWindow} + } + return map[string]Dimensions{} + } + + direction := root.getDirection(width, height) + + var availableSize int + if direction == COLUMN { + availableSize = width + } else { + availableSize = height + } + + sizes := calcSizes(children, availableSize) + + result := map[string]Dimensions{} + offset := 0 + for i, child := range children { + boxSize := sizes[i] + + var resultForChild map[string]Dimensions + if direction == COLUMN { + resultForChild = ArrangeWindows(child, x0+offset, y0, boxSize, height) + } else { + resultForChild = ArrangeWindows(child, x0, y0+offset, width, boxSize) + } + + result = mergeDimensionMaps(result, resultForChild) + offset += boxSize + } + + return result +} + +func calcSizes(boxes []*Box, availableSpace int) []int { + normalizedWeights := normalizeWeights(slices.Map(boxes, func(box *Box) int { return box.Weight })) + + totalWeight := 0 + reservedSpace := 0 + for i, box := range boxes { + if box.isStatic() { + reservedSpace += box.Size + } else { + totalWeight += normalizedWeights[i] + } + } + + dynamicSpace := utils.Max(0, availableSpace-reservedSpace) + + unitSize := 0 + extraSpace := 0 + if totalWeight > 0 { + unitSize = dynamicSpace / totalWeight + extraSpace = dynamicSpace % totalWeight + } + + result := make([]int, len(boxes)) + for i, box := range boxes { + if box.isStatic() { + // assuming that only one static child can have a size greater than the + // available space. In that case we just crop the size to what's available + result[i] = utils.Min(availableSpace, box.Size) + } else { + result[i] = unitSize * normalizedWeights[i] + } + } + + // distribute the remainder across dynamic boxes. + for extraSpace > 0 { + for i, weight := range normalizedWeights { + if weight > 0 { + result[i]++ + extraSpace-- + normalizedWeights[i]-- + + if extraSpace == 0 { + break + } + } + } + } + + return result +} + +// removes common multiple from weights e.g. if we get 2, 4, 4 we return 1, 2, 2. +func normalizeWeights(weights []int) []int { + if len(weights) == 0 { + return []int{} + } + + // to spare us some computation we'll exit early if any of our weights is 1 + if slices.Some(weights, func(weight int) bool { return weight == 1 }) { + return weights + } + + // map weights to factorSlices and find the lowest common factor + positiveWeights := slices.Filter(weights, func(weight int) bool { return weight > 0 }) + factorSlices := slices.Map(positiveWeights, func(weight int) []int { return calcFactors(weight) }) + commonFactors := factorSlices[0] + for _, factors := range factorSlices { + commonFactors = lo.Intersect(commonFactors, factors) + } + + if len(commonFactors) == 0 { + return weights + } + + newWeights := slices.Map(weights, func(weight int) int { return weight / commonFactors[0] }) + + return normalizeWeights(newWeights) +} + +func calcFactors(n int) []int { + factors := []int{} + for i := 2; i <= n; i++ { + if n%i == 0 { + factors = append(factors, i) + } + } + return factors +} + +func (b *Box) isStatic() bool { + return b.Size > 0 +} + +func (b *Box) getDirection(width int, height int) Direction { + if b.ConditionalDirection != nil { + return b.ConditionalDirection(width, height) + } + return b.Direction +} + +func (b *Box) getChildren(width int, height int) []*Box { + if b.ConditionalChildren != nil { + return b.ConditionalChildren(width, height) + } + return b.Children +} + +func mergeDimensionMaps(a map[string]Dimensions, b map[string]Dimensions) map[string]Dimensions { + result := map[string]Dimensions{} + for _, dimensionMap := range []map[string]Dimensions{a, b} { + for k, v := range dimensionMap { + result[k] = v + } + } + return result +} diff --git a/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go new file mode 100644 index 000000000..9fa933762 --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go @@ -0,0 +1,16 @@ +package utils + +// Min returns the minimum of two integers +func Min(x, y int) int { + if x < y { + return x + } + return y +} + +func Max(x, y int) int { + if x > y { + return x + } + return y +} diff --git a/vendor/github.com/samber/lo/.gitignore b/vendor/github.com/samber/lo/.gitignore index 3aa3a0ad4..e5ecc5c40 100644 --- a/vendor/github.com/samber/lo/.gitignore +++ b/vendor/github.com/samber/lo/.gitignore @@ -34,3 +34,5 @@ go.work cover.out cover.html .vscode + +.idea/ diff --git a/vendor/github.com/samber/lo/CHANGELOG.md b/vendor/github.com/samber/lo/CHANGELOG.md index aabeed120..9ba32f1b6 100644 --- a/vendor/github.com/samber/lo/CHANGELOG.md +++ b/vendor/github.com/samber/lo/CHANGELOG.md @@ -1,5 +1,281 @@ # Changelog +@samber: I sometimes forget to update this file. Ping me on [Twitter](https://twitter.com/samuelberthe) or open an issue in case of error. We need to keep a clear changelog for easier lib upgrade. + +## 1.31.0 (2022-10-06) + +Adding: + +- lo.SliceToChannel +- lo.Generator +- lo.Batch +- lo.BatchWithTimeout + +## 1.30.1 (2022-10-06) + +Fix: + +- lo.Try1: remove generic type +- lo.Validate: format error properly + +## 1.30.0 (2022-10-04) + +Adding: + +- lo.TernaryF +- lo.Validate + +## 1.29.0 (2022-10-02) + +Adding: + +- lo.ErrorAs +- lo.TryOr +- lo.TryOrX + +## 1.28.0 (2022-09-05) + +Adding: + +- lo.ChannelDispatcher with 6 dispatching strategies: + - lo.DispatchingStrategyRoundRobin + - lo.DispatchingStrategyRandom + - lo.DispatchingStrategyWeightedRandom + - lo.DispatchingStrategyFirst + - lo.DispatchingStrategyLeast + - lo.DispatchingStrategyMost + +## 1.27.1 (2022-08-15) + +Bugfix: + +- Removed comparable constraint for lo.FindKeyBy + +## 1.27.0 (2022-07-29) + +Breaking: + +- Change of MapToSlice prototype: `MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) []R` -> `MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(K, V) R) []R` + +Added: + +- lo.ChunkString +- lo.SliceToMap (alias to lo.Associate) + +## 1.26.0 (2022-07-24) + +Adding: + +- lo.Associate +- lo.ReduceRight +- lo.FromPtrOr +- lo.MapToSlice +- lo.IsSorted +- lo.IsSortedByKey + +## 1.25.0 (2022-07-04) + +Adding: + +- lo.FindUniques +- lo.FindUniquesBy +- lo.FindDuplicates +- lo.FindDuplicatesBy +- lo.IsNotEmpty + +## 1.24.0 (2022-07-04) + +Adding: + +- lo.Without +- lo.WithoutEmpty + +## 1.23.0 (2022-07-04) + +Adding: + +- lo.FindKey +- lo.FindKeyBy + +## 1.22.0 (2022-07-04) + +Adding: + +- lo.Slice +- lo.FromPtr +- lo.IsEmpty +- lo.Compact +- lo.ToPairs: alias to lo.Entries +- lo.FromPairs: alias to lo.FromEntries +- lo.Partial + +Change: + +- lo.Must + lo.MustX: add context to panic message + +Fix: + +- lo.Nth: out of bound exception (#137) + +## 1.21.0 (2022-05-10) + +Adding: + +- lo.ToAnySlice +- lo.FromAnySlice + +## 1.20.0 (2022-05-02) + +Adding: + +- lo.Synchronize +- lo.SumBy + +Change: +- Removed generic type definition for lo.Try0: `lo.Try0[T]()` -> `lo.Try0()` + +## 1.19.0 (2022-04-30) + +Adding: + +- lo.RepeatBy +- lo.Subset +- lo.Replace +- lo.ReplaceAll +- lo.Substring +- lo.RuneLength + +## 1.18.0 (2022-04-28) + +Adding: + +- lo.SomeBy +- lo.EveryBy +- lo.None +- lo.NoneBy + +## 1.17.0 (2022-04-27) + +Adding: + +- lo.Unpack2 -> lo.Unpack3 +- lo.Async0 -> lo.Async6 + +## 1.16.0 (2022-04-26) + +Adding: + +- lo.AttemptWithDelay + +## 1.15.0 (2022-04-22) + +Improvement: + +- lo.Must: error or boolean value + +## 1.14.0 (2022-04-21) + +Adding: + +- lo.Coalesce + +## 1.13.0 (2022-04-14) + +Adding: + +- PickBy +- PickByKeys +- PickByValues +- OmitBy +- OmitByKeys +- OmitByValues +- Clamp +- MapKeys +- Invert +- IfF + ElseIfF + ElseF +- T0() + T1() + T2() + T3() + ... + +## 1.12.0 (2022-04-12) + +Adding: + +- Must +- Must{0-6} +- FindOrElse +- Async +- MinBy +- MaxBy +- Count +- CountBy +- FindIndexOf +- FindLastIndexOf +- FilterMap + +## 1.11.0 (2022-03-11) + +Adding: + +- Try +- Try{0-6} +- TryWitchValue +- TryCatch +- TryCatchWitchValue +- Debounce +- Reject + +## 1.10.0 (2022-03-11) + +Adding: + +- Range +- RangeFrom +- RangeWithSteps + +## 1.9.0 (2022-03-10) + +Added + +- Drop +- DropRight +- DropWhile +- DropRightWhile + +## 1.8.0 (2022-03-10) + +Adding Union. + +## 1.7.0 (2022-03-09) + +Adding ContainBy + +Adding MapValues + +Adding FlatMap + +## 1.6.0 (2022-03-07) + +Fixed PartitionBy. + +Adding Sample + +Adding Samples + +## 1.5.0 (2022-03-07) + +Adding Times + +Adding Attempt + +Adding Repeat + +## 1.4.0 (2022-03-07) + +- adding tuple types (2->9) +- adding Zip + Unzip +- adding lo.PartitionBy + lop.PartitionBy +- adding lop.GroupBy +- fixing Nth + ## 1.3.0 (2022-03-03) Last and Nth return errors diff --git a/vendor/github.com/samber/lo/Dockerfile b/vendor/github.com/samber/lo/Dockerfile index 9f9f87192..bd01bbbb4 100644 --- a/vendor/github.com/samber/lo/Dockerfile +++ b/vendor/github.com/samber/lo/Dockerfile @@ -1,8 +1,8 @@ -FROM golang:1.18rc1-bullseye +FROM golang:1.18 WORKDIR /go/src/github.com/samber/lo -COPY Makefile go.* /go/src/github.com/samber/lo/ +COPY Makefile go.* ./ RUN make tools diff --git a/vendor/github.com/samber/lo/Makefile b/vendor/github.com/samber/lo/Makefile index 11b09cd25..57bb49159 100644 --- a/vendor/github.com/samber/lo/Makefile +++ b/vendor/github.com/samber/lo/Makefile @@ -1,10 +1,5 @@ BIN=go -# BIN=go1.18beta1 - -go1.18beta1: - go install golang.org/dl/go1.18beta1@latest - go1.18beta1 download build: ${BIN} build -v ./... @@ -12,15 +7,15 @@ build: test: go test -race -v ./... watch-test: - reflex -R assets.go -t 50ms -s -- sh -c 'gotest -race -v ./...' + reflex -t 50ms -s -- sh -c 'gotest -race -v ./...' bench: go test -benchmem -count 3 -bench ./... watch-bench: - reflex -R assets.go -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' + reflex -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' coverage: - ${BIN} test -v -coverprofile cover.out . + ${BIN} test -v -coverprofile=cover.out -covermode=atomic . ${BIN} tool cover -html=cover.out -o cover.html # tools @@ -31,7 +26,7 @@ tools: ${BIN} install github.com/jondot/goweight@latest ${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest ${BIN} get -t -u golang.org/x/tools/cmd/cover - ${BIN} get -t -u github.com/sonatype-nexus-community/nancy@latest + ${BIN} install github.com/sonatype-nexus-community/nancy@latest go mod tidy lint: @@ -40,11 +35,9 @@ lint-fix: golangci-lint run --timeout 60s --max-same-issues 50 --fix ./... audit: tools - ${BIN} mod tidy ${BIN} list -json -m all | nancy sleuth outdated: tools - ${BIN} mod tidy ${BIN} list -u -m -json all | go-mod-outdated -update -direct weight: tools diff --git a/vendor/github.com/samber/lo/README.md b/vendor/github.com/samber/lo/README.md index 48dfdc004..49cbe49da 100644 --- a/vendor/github.com/samber/lo/README.md +++ b/vendor/github.com/samber/lo/README.md @@ -1,10 +1,12 @@ # lo -![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) +[![tag](https://img.shields.io/github/tag/samber/lo.svg)](https://github.com/samber/lo/releases) [![GoDoc](https://godoc.org/github.com/samber/lo?status.svg)](https://pkg.go.dev/github.com/samber/lo) +![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) [![Go report](https://goreportcard.com/badge/github.com/samber/lo)](https://goreportcard.com/report/github.com/samber/lo) +[![codecov](https://codecov.io/gh/samber/lo/branch/master/graph/badge.svg)](https://codecov.io/gh/samber/lo) -✨ **`lo` is a Lodash-style Go library based on Go 1.18+ Generics.** +✨ **`samber/lo` is a Lodash-style Go library based on Go 1.18+ Generics.** This project started as an experiment with the new generics implementation. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe. @@ -12,16 +14,25 @@ As expected, benchmarks demonstrate that generics will be much faster than imple In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names `slices` and `maps`). I feel this library is legitimate and offers many more valuable abstractions. -### Why this name? +**See also:** + +- [samber/do](https://github.com/samber/do): A dependency injection toolkit based on Go 1.18+ Generics +- [samber/mo](https://github.com/samber/mo): Monads based on Go 1.18+ Generics (Option, Result, Either...) + +**Why this name?** I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name. ## 🚀 Install ```sh -go get github.com/samber/lo +go get github.com/samber/lo@v1 ``` +This library is v1 and follows SemVer strictly. + +No breaking changes will be made to exported APIs before v2.0.0. + ## 💡 Usage You can import `lo` using: @@ -48,78 +59,187 @@ GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/sam Supported helpers for slices: -- Filter -- Map -- FlatMap -- Reduce -- ForEach -- Times -- Uniq -- UniqBy -- GroupBy -- Chunk -- PartitionBy -- Flatten -- Shuffle -- Reverse -- Fill -- Repeat -- KeyBy -- Drop -- DropRight -- DropWhile -- DropRightWhile +- [Filter](#filter) +- [Map](#map) +- [FilterMap](#filtermap) +- [FlatMap](#flatmap) +- [Reduce](#reduce) +- [ReduceRight](#reduceright) +- [ForEach](#foreach) +- [Times](#times) +- [Uniq](#uniq) +- [UniqBy](#uniqby) +- [GroupBy](#groupby) +- [Chunk](#chunk) +- [PartitionBy](#partitionby) +- [Flatten](#flatten) +- [Shuffle](#shuffle) +- [Reverse](#reverse) +- [Fill](#fill) +- [Repeat](#repeat) +- [RepeatBy](#repeatby) +- [KeyBy](#keyby) +- [Associate / SliceToMap](#associate-alias-slicetomap) +- [Drop](#drop) +- [DropRight](#dropright) +- [DropWhile](#dropwhile) +- [DropRightWhile](#droprightwhile) +- [Reject](#reject) +- [Count](#count) +- [CountBy](#countby) +- [Subset](#subset) +- [Slice](#slice) +- [Replace](#replace) +- [ReplaceAll](#replaceall) +- [Compact](#compact) +- [IsSorted](#issorted) +- [IsSortedByKey](#issortedbykey) Supported helpers for maps: -- Keys -- Values -- Entries -- FromEntries -- Assign (merge of maps) -- MapValues +- [Keys](#keys) +- [Values](#values) +- [PickBy](#pickby) +- [PickByKeys](#pickbykeys) +- [PickB |