diff --git a/go.mod b/go.mod index 7bf8f3e7..cfe7d39f 100644 --- a/go.mod +++ b/go.mod @@ -70,7 +70,7 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect github.com/pkg/errors v0.9.1 - github.com/pterm/pterm v0.12.69 + github.com/pterm/pterm v0.12.71 github.com/rivo/uniseg v0.4.4 // indirect github.com/spf13/pflag v1.0.5 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index a33a7de4..cb5f0085 100644 --- a/go.sum +++ b/go.sum @@ -153,8 +153,8 @@ github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEej github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= -github.com/pterm/pterm v0.12.69 h1:fBCKnB8dSLAl8FlYRQAWYGp2WTI/Xm/tKJ21Hyo9USw= -github.com/pterm/pterm v0.12.69/go.mod h1:wl06ko9MHnqxz4oDV++IORDpjCzw6+mfrvf0MPj6fdk= +github.com/pterm/pterm v0.12.71 h1:KcEJ98EiVCbzDkFbktJ2gMlr4pn8IzyGb9bwK6ffkuA= +github.com/pterm/pterm v0.12.71/go.mod h1:SUAcoZjRt+yjPWlWba+/Fd8zJJ2lSXBQWf0Z0HbFiIQ= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= diff --git a/vendor/github.com/pterm/pterm/.gitignore b/vendor/github.com/pterm/pterm/.gitignore index e4354651..dc83dde7 100644 --- a/vendor/github.com/pterm/pterm/.gitignore +++ b/vendor/github.com/pterm/pterm/.gitignore @@ -15,7 +15,7 @@ vendor/ # This is where we test stuff -/experimenting/ +/experimenting/* /.history /.vscode diff --git a/vendor/github.com/pterm/pterm/README.md b/vendor/github.com/pterm/pterm/README.md index f05f2d93..4ac3f28e 100644 --- a/vendor/github.com/pterm/pterm/README.md +++ b/vendor/github.com/pterm/pterm/README.md @@ -99,10 +99,10 @@ go get github.com/pterm/pterm | :-------: | :-------: | :-------: | :-------: | :-------: | | Area
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/area) |Barchart
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/barchart) |Basictext
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/basictext) |Bigtext
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/bigtext) |Box
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/box) | | Bulletlist
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/bulletlist) |Center
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/center) |Coloring
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/coloring) |Demo
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/demo) |Header
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/header) | -| Interactive confirm
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_confirm) |Interactive continue
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_continue) |Interactive multiselect
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_multiselect) |Interactive select
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_select) |Interactive textinput
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput) | -| Logger
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/logger) |Multiple-live-printers
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/multiple-live-printers) |Panel
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/panel) |Paragraph
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/paragraph) |Prefix
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/prefix) | -| Progressbar
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/progressbar) |Section
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/section) |Slog
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/slog) |Spinner
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/spinner) |Style
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/style) | -| Table
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/table) |Theme
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/theme) |Tree
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/tree) | | | +| Heatmap
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/heatmap) |Interactive confirm
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_confirm) |Interactive continue
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_continue) |Interactive multiselect
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_multiselect) |Interactive select
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_select) | +| Interactive textinput
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/interactive_textinput) |Logger
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/logger) |Multiple-live-printers
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/multiple-live-printers) |Panel
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/panel) |Paragraph
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/paragraph) | +| Prefix
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/prefix) |Progressbar
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/progressbar) |Section
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/section) |Slog
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/slog) |Spinner
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/spinner) | +| Style
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/style) |Table
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/table) |Theme
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/theme) |Tree
[(Examples)](https://github.com/pterm/pterm/tree/master/_examples/tree) | | @@ -1648,6 +1648,241 @@ func main() { +### heatmap/custom_colors + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/custom_colors/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + pterm.Info.Println("The following table has no rgb (supported by every terminal), no axis data and a legend.") + pterm.Println() + + table := pterm.DefaultHeatmap.WithData(data).WithBoxed(false).WithAxisData(headerData).WithLegend(false).WithColors(pterm.BgBlue, pterm.BgRed, pterm.BgGreen, pterm.BgYellow).WithLegend() + table.Render() +} + +``` + +
+ +### heatmap/custom_legend + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/custom_legend/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + pterm.Info.Println("The following table has rgb (not supported by every terminal), axis data and a custom legend.") + pterm.Println() + + table := pterm.DefaultHeatmap.WithData(data).WithBoxed(false).WithAxisData(headerData).WithEnableRGB().WithLegendLabel("custom").WithLegendOnlyColoredCells() + table.Render() +} + +``` + +
+ +### heatmap/custom_rgb + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/custom_rgb/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + pterm.Info.Println("The following table has rgb (not supported by every terminal), axis data and a legend.") + pterm.Println() + + table2 := pterm.DefaultHeatmap.WithData(data).WithBoxed(false).WithAxisData(headerData).WithEnableRGB().WithRGBRange(pterm.NewRGB(0, 0, 255), pterm.NewRGB(255, 0, 0), pterm.NewRGB(0, 255, 0), pterm.NewRGB(255, 255, 0)) + table2.Render() +} + +``` + +
+ +### heatmap/demo + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/demo/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + table := pterm.DefaultHeatmap.WithAxisData(headerData).WithData(data).WithEnableRGB() + table.Render() +} + +``` + +
+ +### heatmap/no_grid + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/no_grid/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import ( + "github.com/pterm/pterm" +) + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + pterm.Info.Println("The following table has rgb (not supported by every terminal), axis data and a legend.") + pterm.Println() + + table2 := pterm.DefaultHeatmap.WithData(data).WithBoxed(false).WithAxisData(headerData).WithEnableRGB().WithLegend().WithGrid(false) + table2.Render() +} + +``` + +
+ +### heatmap/separated + +![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/heatmap/separated/animation.svg) + +
+ +SHOW SOURCE + +```go +package main + +import "github.com/pterm/pterm" + +func main() { + data := [][]float32{ + {0.9, 0.2, -0.7, 0.4, -0.5, 0.6, -0.3, 0.8, -0.1, -1.0, 0.1, -0.8, 0.3}, + {0.2, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.9, -0.9, -0.7, -0.5, -0.3}, + {0.4, 0.4, -0.3, -1.0, 0.3, -0.2, -0.9, 0.5, -0.3, -1.0, 0.6, -0.2, -0.9}, + {0.9, -0.5, -0.1, 0.3, 1, -0.7, -0.3, 0.1, 0.7, -0.9, -0.5, 0.2, 0.6}, + {0.5, 0.6, 0.1, -0.2, -0.7, 0.8, 0.6, 0.1, -0.5, -0.7, 0.7, 0.3, 0.0}, + } + + headerData := pterm.HeatmapAxis{ + XAxis: []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}, + YAxis: []string{"1", "2", "3", "4", "5"}, + } + + pterm.Info.Println("The following table has no rgb (supported by every terminal), no axis data and no legend.") + pterm.Println() + + table := pterm.DefaultHeatmap.WithData(data).WithBoxed(false).WithAxisData(headerData).WithLegend(false) + table.Render() +} + +``` + +
+ ### interactive_confirm/demo ![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/interactive_confirm/demo/animation.svg) diff --git a/vendor/github.com/pterm/pterm/bulletlist_printer.go b/vendor/github.com/pterm/pterm/bulletlist_printer.go index ed997aa5..8e6afcb0 100644 --- a/vendor/github.com/pterm/pterm/bulletlist_printer.go +++ b/vendor/github.com/pterm/pterm/bulletlist_printer.go @@ -116,10 +116,18 @@ func (l BulletListPrinter) Srender() (string, error) { item.BulletStyle = l.BulletStyle } } - if item.Bullet == "" { - ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(l.Bullet) + " " + item.TextStyle.Sprint(item.Text) + "\n" - } else { - ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(item.Bullet) + " " + item.TextStyle.Sprint(item.Text) + "\n" + + split := strings.Split(item.Text, "\n") + for i, line := range split { + if i == 0 { + if item.Bullet == "" { + ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(l.Bullet) + " " + item.TextStyle.Sprint(line) + "\n" + } else { + ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(item.Bullet) + " " + item.TextStyle.Sprint(line) + "\n" + } + } else { + ret += strings.Repeat(" ", item.Level) + strings.Repeat(" ", len(item.Bullet)) + " " + item.TextStyle.Sprint(line) + "\n" + } } } return ret, nil diff --git a/vendor/github.com/pterm/pterm/heatmap_printer.go b/vendor/github.com/pterm/pterm/heatmap_printer.go new file mode 100644 index 00000000..4fac71e3 --- /dev/null +++ b/vendor/github.com/pterm/pterm/heatmap_printer.go @@ -0,0 +1,744 @@ +package pterm + +import ( + "bytes" + "errors" + "io" + "math" + "strings" + + "github.com/pterm/pterm/internal" +) + +// DefaultHeatmap contains standards, which can be used to print a HeatmapPrinter. +var DefaultHeatmap = HeatmapPrinter{ + AxisStyle: &ThemeDefault.HeatmapHeaderStyle, + SeparatorStyle: &ThemeDefault.HeatmapSeparatorStyle, + VerticalSeparator: "│", + TopRightCornerSeparator: "└", + TopLeftCornerSeparator: "┘", + BottomLeftCornerSeparator: "┐", + BottomRightCornerSeparator: "┌", + HorizontalSeparator: "─", + TSeparator: "┬", + TReverseSeparator: "┴", + LSeparator: "├", + LReverseSeparator: "┤", + TCrossSeparator: "┼", + LegendLabel: "Legend", + Boxed: true, + Grid: true, + Legend: true, + TextRGB: RGB{0, 0, 0, false}, + RGBRange: []RGB{{R: 255, G: 0, B: 0, Background: true}, {R: 255, G: 165, B: 0, Background: true}, {R: 0, G: 255, B: 0, Background: true}}, + TextColor: FgBlack, + Colors: []Color{BgRed, BgLightRed, BgYellow, BgLightYellow, BgLightGreen, BgGreen}, + + EnableRGB: false, +} + +// HeatmapData is the type that contains the data of a HeatmapPrinter. +type HeatmapData [][]float32 + +type HeatmapAxis struct { + XAxis []string + YAxis []string +} + +// HeatmapPrinter is able to render tables. +type HeatmapPrinter struct { + HasHeader bool + AxisStyle *Style + VerticalSeparator string + TopRightCornerSeparator string + TopLeftCornerSeparator string + BottomLeftCornerSeparator string + BottomRightCornerSeparator string + HorizontalSeparator string + TSeparator string + TReverseSeparator string + LSeparator string + LReverseSeparator string + TCrossSeparator string + LegendLabel string + SeparatorStyle *Style + Data HeatmapData + Axis HeatmapAxis + Boxed bool + Grid bool + OnlyColoredCells bool + LegendOnlyColoredCells bool + EnableComplementaryColor bool + Legend bool + CellSize int + Colors []Color + TextColor Color + EnableRGB bool + RGBRange []RGB + TextRGB RGB + Writer io.Writer + + minValue float32 + maxValue float32 + + rgbLegendValue int +} + +var complementaryColors = map[Color]Color{ + BgBlack: FgLightWhite, + BgRed: FgCyan, + BgGreen: FgMagenta, + BgYellow: FgBlue, + BgBlue: FgYellow, + BgMagenta: FgGreen, + BgCyan: FgRed, + BgWhite: FgBlack, + BgDefault: FgBlack, + BgDarkGray: FgLightWhite, + BgLightRed: FgLightCyan, + BgLightGreen: FgLightMagenta, + BgLightYellow: FgLightBlue, + BgLightBlue: FgLightYellow, + BgLightMagenta: FgLightGreen, + BgLightCyan: FgLightRed, + BgLightWhite: FgBlack, +} + +// WithAxisData returns a new HeatmapPrinter, where the first line and row are headers. +func (p HeatmapPrinter) WithAxisData(hd HeatmapAxis) *HeatmapPrinter { + p.HasHeader = true + p.Axis = hd + return &p +} + +// WithAxisStyle returns a new HeatmapPrinter with a specific AxisStyle. +func (p HeatmapPrinter) WithAxisStyle(style *Style) *HeatmapPrinter { + p.AxisStyle = style + return &p +} + +// WithSeparatorStyle returns a new HeatmapPrinter with a specific SeparatorStyle. +func (p HeatmapPrinter) WithSeparatorStyle(style *Style) *HeatmapPrinter { + p.SeparatorStyle = style + return &p +} + +// WithData returns a new HeatmapPrinter with specific Data. +func (p HeatmapPrinter) WithData(data [][]float32) *HeatmapPrinter { + p.Data = data + return &p +} + +// WithTextColor returns a new HeatmapPrinter with a specific TextColor. +// This sets EnableComplementaryColor to false. +func (p HeatmapPrinter) WithTextColor(color Color) *HeatmapPrinter { + p.TextColor = color + p.EnableComplementaryColor = false + return &p +} + +// WithTextRGB returns a new HeatmapPrinter with a specific TextRGB. +// This sets EnableComplementaryColor to false. +func (p HeatmapPrinter) WithTextRGB(rgb RGB) *HeatmapPrinter { + p.TextRGB = rgb + p.EnableComplementaryColor = false + return &p +} + +// WithBoxed returns a new HeatmapPrinter with a box around the table. +// If set to true, Grid will be set to true too. +func (p HeatmapPrinter) WithBoxed(b ...bool) *HeatmapPrinter { + p.Boxed = internal.WithBoolean(b) + if p.Boxed && !p.Grid { + p.Grid = true + } + return &p +} + +// WithGrid returns a new HeatmapPrinter with a grid. +// If set to false, Boxed will be set to false too. +func (p HeatmapPrinter) WithGrid(b ...bool) *HeatmapPrinter { + b2 := internal.WithBoolean(b) + p.Grid = b2 + if !b2 && p.Boxed { + p.Boxed = false + } + return &p +} + +// WithEnableRGB returns a new HeatmapPrinter with RGB colors. +func (p HeatmapPrinter) WithEnableRGB(b ...bool) *HeatmapPrinter { + p.EnableRGB = internal.WithBoolean(b) + return &p +} + +// WithOnlyColoredCells returns a new HeatmapPrinter with only colored cells. +func (p HeatmapPrinter) WithOnlyColoredCells(b ...bool) *HeatmapPrinter { + b2 := internal.WithBoolean(b) + p.OnlyColoredCells = b2 + return &p +} + +// WithLegendOnlyColoredCells returns a new HeatmapPrinter with legend with only colored cells. +// This sets the Legend to true. +func (p HeatmapPrinter) WithLegendOnlyColoredCells(b ...bool) *HeatmapPrinter { + b2 := internal.WithBoolean(b) + p.LegendOnlyColoredCells = b2 + if b2 { + p.Legend = true + } + return &p +} + +// WithEnableComplementaryColor returns a new HeatmapPrinter with complement color. +func (p HeatmapPrinter) WithEnableComplementaryColor(b ...bool) *HeatmapPrinter { + p.EnableComplementaryColor = internal.WithBoolean(b) + return &p +} + +// WithLegend returns a new HeatmapPrinter with a legend. +func (p HeatmapPrinter) WithLegend(b ...bool) *HeatmapPrinter { + p.Legend = internal.WithBoolean(b) + return &p +} + +// WithCellSize returns a new HeatmapPrinter with a specific cell size. +// This only works if there is no header and OnlyColoredCells == true! +func (p HeatmapPrinter) WithCellSize(i int) *HeatmapPrinter { + p.CellSize = i + return &p +} + +// WithLegendLabel returns a new HeatmapPrinter with a specific legend tag. +// This sets the Legend to true. +func (p HeatmapPrinter) WithLegendLabel(s string) *HeatmapPrinter { + p.LegendLabel = s + p.Legend = true + return &p +} + +// WithRGBRange returns a new HeatmapPrinter with a specific RGBRange. +func (p HeatmapPrinter) WithRGBRange(rgb ...RGB) *HeatmapPrinter { + p.RGBRange = rgb + return &p +} + +// WithColors returns a new HeatmapPrinter with a specific Colors. +func (p HeatmapPrinter) WithColors(colors ...Color) *HeatmapPrinter { + p.Colors = colors + return &p +} + +// WithWriter sets the Writer. +func (p HeatmapPrinter) WithWriter(writer io.Writer) *HeatmapPrinter { + p.Writer = writer + return &p +} + +// Srender renders the HeatmapPrinter as a string. +func (p HeatmapPrinter) Srender() (string, error) { + if err := p.errCheck(); err != nil { + return "", err + } + + if p.SeparatorStyle == nil { + p.SeparatorStyle = DefaultHeatmap.SeparatorStyle + } + if p.AxisStyle == nil { + p.AxisStyle = DefaultHeatmap.AxisStyle + } + + if RawOutput { + p.Legend = false + } + + buffer := bytes.NewBufferString("") + xAmount := len(p.Data[0]) - 1 + yAmount := len(p.Data) - 1 + p.minValue, p.maxValue = minMaxFloat32(p.Data) + + var data string + for _, datum := range p.Data { + for _, f := range datum { + data += Sprintf("%v\n", f) + } + } + + if p.HasHeader { + data, xAmount, yAmount = p.computeAxisData(data, xAmount, yAmount) + } + + colWidth := internal.GetStringMaxWidth(data) + legendColWidth := colWidth + 2 + + if p.OnlyColoredCells && (p.CellSize > colWidth || !p.HasHeader) { + colWidth = p.CellSize + } + + if p.Boxed { + p.renderSeparatorRow(buffer, colWidth, xAmount, true) + } + + p.renderData(buffer, colWidth, xAmount, yAmount) + + if p.HasHeader { + p.renderHeader(buffer, colWidth, xAmount) + } + + if p.Boxed { + p.renderSeparatorRow(buffer, colWidth, xAmount, false) + } + + if p.Legend { + p.renderLegend(buffer, legendColWidth) + } + + buffer.WriteString("\n") + + return buffer.String(), nil +} + +func (p HeatmapPrinter) computeAxisData(data string, xAmount, yAmount int) (string, int, int) { + var header string + for _, h := range p.Axis.XAxis { + header += h + "\n" + } + for _, h := range p.Axis.YAxis { + header += h + "\n" + } + + if p.OnlyColoredCells { + data = header + } else { + data += header + } + xAmount++ + yAmount++ + + p.Axis.YAxis = append(p.Axis.YAxis, "") + + return data, xAmount, yAmount +} + +func (p HeatmapPrinter) renderSeparatorRow(buffer *bytes.Buffer, colWidth, xAmount int, top bool) { + tSep := p.TReverseSeparator + rightSep := p.TopRightCornerSeparator + leftSep := p.TopLeftCornerSeparator + + if top { + tSep = p.TSeparator + rightSep = p.BottomRightCornerSeparator + leftSep = p.BottomLeftCornerSeparator + } else { + buffer.WriteString("\n") + } + buffer.WriteString(p.SeparatorStyle.Sprint(rightSep)) + for i := 0; i < xAmount+1; i++ { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth)) + if i < xAmount { + buffer.WriteString(p.SeparatorStyle.Sprint(tSep)) + } + } + buffer.WriteString(p.SeparatorStyle.Sprint(leftSep)) + + if top { + buffer.WriteString("\n") + } +} + +func (p HeatmapPrinter) renderLegend(buffer *bytes.Buffer, legendColWidth int) { + buffer.WriteString("\n") + buffer.WriteString("\n") + if p.Boxed { + p.boxLegend(buffer, p.LegendLabel, legendColWidth) + } else { + p.generateLegend(buffer, p.LegendLabel, legendColWidth) + } +} + +func (p HeatmapPrinter) renderHeader(buffer *bytes.Buffer, colWidth int, xAmount int) { + buffer.WriteString("\n") + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.LSeparator)) + } + if p.Grid { + for i := 0; i < xAmount+1; i++ { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth)) + if i < xAmount { + buffer.WriteString(p.SeparatorStyle.Sprint(p.TCrossSeparator)) + } + } + } + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.LReverseSeparator)) + } + if p.Grid { + buffer.WriteString("\n") + } + for j, f := range p.Axis.XAxis { + if j == 0 { + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + ct := internal.CenterText(" ", colWidth) + if len(ct) < colWidth { + ct += strings.Repeat(" ", colWidth-len(ct)) + } + buffer.WriteString(p.AxisStyle.Sprint(ct)) + if p.Grid { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + } + var ct string + ct = internal.CenterText(Sprintf("%v", f), colWidth) + if len(ct) < colWidth { + ct += strings.Repeat(" ", colWidth-len(ct)) + } + buffer.WriteString(p.AxisStyle.Sprint(ct)) + + if j < xAmount { + if !p.Boxed && j == xAmount-1 { + continue + } + if p.Grid { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + } + } +} + +func (p HeatmapPrinter) renderData(buffer *bytes.Buffer, colWidth int, xAmount int, yAmount int) { + for i, datum := range p.Data { + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + for j, f := range datum { + if j == 0 && p.HasHeader { + ct := internal.CenterText(p.Axis.YAxis[i], colWidth) + if len(ct) < colWidth { + ct += strings.Repeat(" ", colWidth-len(ct)) + } + buffer.WriteString(p.AxisStyle.Sprint(ct)) + if p.Grid { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + } + var ct string + if p.OnlyColoredCells { + ct = internal.CenterText(" ", colWidth) + } else { + ct = internal.CenterText(Sprintf("%v", f), colWidth) + } + if len(ct) < colWidth { + if len(Sprintf("%v", f)) == 1 { + ct += strings.Repeat(" ", colWidth-len(ct)) + } else { + ct = strings.Repeat(" ", colWidth-len(ct)) + ct + } + } + if p.EnableRGB { + rgb := p.RGBRange[0].Fade(p.minValue, p.maxValue, f, p.RGBRange[1:]...) + rgbStyle := NewRGBStyle(p.TextRGB, rgb) + if p.EnableComplementaryColor { + complimentary := NewRGB(internal.Complementary(rgb.R, rgb.G, rgb.B)) + rgbStyle = NewRGBStyle(complimentary, rgb) + } + buffer.WriteString(rgbStyle.Sprint(ct)) + } else { + color := getColor(p.minValue, p.maxValue, f, p.Colors...) + fgColor := p.TextColor + if p.EnableComplementaryColor { + fgColor = complementaryColors[color] + } + buffer.WriteString(fgColor.Sprint(color.Sprintf(ct))) + } + if j < xAmount { + if !p.Boxed && p.HasHeader && j == xAmount-1 { + continue + } + if p.Grid { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + } + if p.Boxed && !p.HasHeader && j == xAmount { + buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator)) + } + } + + if i < yAmount { + if p.HasHeader && i == yAmount-1 { + continue + } + buffer.WriteString("\n") + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.LSeparator)) + } + if p.Grid { + for i := 0; i < xAmount+1; i++ { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth)) + if i < xAmount { + buffer.WriteString(p.SeparatorStyle.Sprint(p.TCrossSeparator)) + } + } + } + if p.Boxed { + buffer.WriteString(p.SeparatorStyle.Sprint(p.LReverseSeparator)) + } + if p.Grid { + buffer.WriteString("\n") + } + } + } +} + +func (p HeatmapPrinter) generateLegend(buffer *bytes.Buffer, legend string, legendColWidth int) { + buffer.WriteString(p.AxisStyle.Sprint(legend)) + if p.Grid { + buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator)) + } else { + buffer.WriteString(" ") + } + if p.EnableRGB { + p.generateRGBLegend(buffer, legendColWidth) + } else { + p.generateColorLegend(buffer, legendColWidth) + } +} + +func (p HeatmapPrinter) generateColorLegend(buffer *bytes.Buffer, legendColWidth int) { + for i, color := range p.Colors { + // the first color is the min value and the last color is the max value + var f float32 + if i == 0 { + f = p.minValue + } else if i == len(p.Colors)-1 { + f = p.maxValue + } else { + f = p.minValue + (p.maxValue-p.minValue)*float32(i)/float32(len(p.Colors)-1) + } + fgColor := p.TextColor + if p.EnableComplementaryColor { + fgColor = complementaryColors[color] + } + buffer.WriteString(fgColor.Sprint(color.Sprint(centerAndShorten(f, legendColWidth, p.LegendOnlyColoredCells)))) + if p.Grid && i < len(p.Colors)-1 && !p.LegendOnlyColoredCells { + buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator)) + } + } +} + +func (p HeatmapPrinter) generateRGBLegend(buffer *bytes.Buffer, legendColWidth int) { + p.rgbLegendValue = 10 + steps := len(p.RGBRange) + if steps < p.rgbLegendValue { + steps = p.rgbLegendValue + } + if p.LegendOnlyColoredCells { + steps *= 3 + } + for i := 0; i < steps; i++ { + // the first color is the min value and the last color is the max value + var f float32 + if i == 0 { + f = p.minValue + } else if i == steps-1 { + f = p.maxValue + } else { + f = p.minValue + (p.maxValue-p.minValue)*float32(i)/float32(steps-1) + } + rgb := p.RGBRange[0].Fade(p.minValue, p.maxValue, f, p.RGBRange[1:]...) + rgbStyle := NewRGBStyle(p.TextRGB, rgb) + if p.EnableComplementaryColor { + complimentary := NewRGB(internal.Complementary(rgb.R, rgb.G, rgb.B)) + rgbStyle = NewRGBStyle(complimentary, rgb) + } + if p.LegendOnlyColoredCells { + buffer.WriteString(rgbStyle.Sprint(centerAndShorten(f, 1, p.LegendOnlyColoredCells))) + } else { + buffer.WriteString(rgbStyle.Sprint(centerAndShorten(f, legendColWidth, p.LegendOnlyColoredCells))) + } + if p.Grid && i < steps-1 && !p.LegendOnlyColoredCells { + buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator)) + } + } +} + +func (p HeatmapPrinter) boxLegend(buffer *bytes.Buffer, legend string, legendColWidth int) { + buffer.WriteString(p.SeparatorStyle.Sprint(p.BottomRightCornerSeparator)) + + p.generateSeparatorRow(buffer, legend, legendColWidth, true) + + buffer.WriteString(p.SeparatorStyle.Sprint(p.BottomLeftCornerSeparator)) + buffer.WriteString("\n") + buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator)) + + p.generateLegend(buffer, legend, legendColWidth) + + buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator)) + buffer.WriteString("\n") + + buffer.WriteString(p.SeparatorStyle.Sprint(p.TopRightCornerSeparator)) + + p.generateSeparatorRow(buffer, legend, legendColWidth, false) + + buffer.WriteString(p.SeparatorStyle.Sprint(p.TopLeftCornerSeparator)) +} + +func (p HeatmapPrinter) generateSeparatorRow(buffer *bytes.Buffer, legend string, legendColWidth int, top bool) { + p.rgbLegendValue = 10 + steps := len(p.RGBRange) + if steps < p.rgbLegendValue { + steps = p.rgbLegendValue + } + if p.LegendOnlyColoredCells { + steps *= 3 + } + + var xValue int + if p.EnableRGB { + xValue = len(p.RGBRange) + if xValue < p.rgbLegendValue { + xValue = p.rgbLegendValue + } + } else { + xValue = len(p.Colors) + } + + for i := 0; i < xValue+1; i++ { + if i == 0 { + firstLength := len(legend) + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), firstLength)) + } else { + if p.LegendOnlyColoredCells { + if p.EnableRGB { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), steps/(xValue))) + } else { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), legendColWidth)) + } + } else { + buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), legendColWidth)) + } + } + if i < xValue && !p.LegendOnlyColoredCells || i == 0 { + if top { + buffer.WriteString(p.SeparatorStyle.Sprint(p.TSeparator)) + } else { + buffer.WriteString(p.SeparatorStyle.Sprint(p.TReverseSeparator)) + } + } + } +} + +func centerAndShorten(f float32, lineLength int, onlyColor bool) string { + value := "" + if !onlyColor { + value = Sprintf("%.2v", f) + } + if len(value) > lineLength { + value = value[:lineLength] + if strings.HasSuffix(value, ".") { + value = Sprintf("%.1v", f) + lineLength = len(value) + } + } + ct := internal.CenterText(value, lineLength) + if len(ct) < lineLength { + if len(Sprintf("%v", f)) == 1 { + ct += strings.Repeat(" ", lineLength-len(ct)) + } else { + ct = strings.Repeat(" ", lineLength-len(ct)) + ct + } + } + + return ct +} + +func getColor(min float32, max float32, current float32, colors ...Color) Color { + // split the range into equal parts + // and assign a color to each part + // the last color is assigned to the max value + // and the first color to the min value + // the rest of the colors are assigned to the + // middle values + step := (max - min) / float32(len(colors)) + for i := range colors { + if current >= min+float32(i)*step && current < min+float32(i+1)*step { + return colors[i] + } + } + return colors[len(colors)-1] +} + +// Render prints the HeatmapPrinter to the terminal. +func (p HeatmapPrinter) Render() error { + s, err := p.Srender() + if err != nil { + return err + } + Fprintln(p.Writer, s) + + return nil +} + +func (p HeatmapPrinter) errCheck() error { + if p.HasHeader { + if p.Axis.XAxis == nil { + return errors.New("x axis is nil") + } + if p.Axis.YAxis == nil { + return errors.New("y axis is nil") + } + + if len(p.Axis.XAxis) == 0 { + return errors.New("x axis is empty") + } + if len(p.Axis.YAxis) == 0 { + return errors.New("y axis is empty") + } + + for i := 1; i < len(p.Data); i++ { + if len(p.Data[i]) != len(p.Axis.XAxis) { + return errors.New("x axis length does not match data") + } + } + if len(p.Axis.YAxis) != len(p.Data) { + return errors.New("y axis length does not match data") + } + } + + if p.Data == nil { + return errors.New("data is nil") + } + + if len(p.Data) == 0 { + return errors.New("data is empty") + } + + // check if p.Data[n] has the same length + for i := 1; i < len(p.Data); i++ { + if len(p.Data[i]) != len(p.Data[0]) { + return errors.New("data is not rectangular") + } + } + + return nil +} + +// return min and max value of a slice +func minMaxFloat32(s [][]float32) (float32, float32) { + var min, max float32 + min = math.MaxFloat32 + max = -math.MaxFloat32 + + for _, r := range s { + for _, c := range r { + if c < min { + min = c + } + if c > max { + max = c + } + } + } + return min, max +} diff --git a/vendor/github.com/pterm/pterm/interactive_textinput_printer.go b/vendor/github.com/pterm/pterm/interactive_textinput_printer.go index 2514ab12..88535c51 100644 --- a/vendor/github.com/pterm/pterm/interactive_textinput_printer.go +++ b/vendor/github.com/pterm/pterm/interactive_textinput_printer.go @@ -22,6 +22,7 @@ var DefaultInteractiveTextInput = InteractiveTextInputPrinter{ type InteractiveTextInputPrinter struct { TextStyle *Style DefaultText string + DefaultValue string Delimiter string MultiLine bool Mask string @@ -39,6 +40,12 @@ func (p InteractiveTextInputPrinter) WithDefaultText(text string) *InteractiveTe return &p } +// WithDefaultValue sets the default value. +func (p InteractiveTextInputPrinter) WithDefaultValue(value string) *InteractiveTextInputPrinter { + p.DefaultValue = value + return &p +} + // WithTextStyle sets the text style. func (p InteractiveTextInputPrinter) WithTextStyle(style *Style) *InteractiveTextInputPrinter { p.TextStyle = style @@ -87,6 +94,7 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { } else { areaText = p.TextStyle.Sprintf("%s%s", text[0], p.Delimiter) } + p.text = areaText area := cursor.NewArea() area.Update(areaText) @@ -96,6 +104,11 @@ func (p InteractiveTextInputPrinter) Show(text ...string) (string, error) { cursor.Right(len(RemoveColorFromString(areaText))) } + if p.DefaultValue != "" { + p.input = append(p.input, p.DefaultValue) + p.updateArea(&area) + } + err := keyboard.Listen(func(key keys.Key) (stop bool, err error) { if !p.MultiLine { p.cursorYPos = 0 diff --git a/vendor/github.com/pterm/pterm/internal/rgb_complementary.go b/vendor/github.com/pterm/pterm/internal/rgb_complementary.go new file mode 100644 index 00000000..71520b33 --- /dev/null +++ b/vendor/github.com/pterm/pterm/internal/rgb_complementary.go @@ -0,0 +1,5 @@ +package internal + +func Complementary(r, g, b uint8) (uint8, uint8, uint8) { + return 255 - r, 255 - g, 255 - b +} diff --git a/vendor/github.com/pterm/pterm/slog_handler.go b/vendor/github.com/pterm/pterm/slog_handler.go index db35617e..d0854d04 100644 --- a/vendor/github.com/pterm/pterm/slog_handler.go +++ b/vendor/github.com/pterm/pterm/slog_handler.go @@ -2,6 +2,7 @@ package pterm import ( "context" + "log/slog" ) diff --git a/vendor/github.com/pterm/pterm/theme.go b/vendor/github.com/pterm/pterm/theme.go index 22466ea7..91594c8c 100644 --- a/vendor/github.com/pterm/pterm/theme.go +++ b/vendor/github.com/pterm/pterm/theme.go @@ -30,6 +30,9 @@ var ( TableStyle: Style{FgDefault}, TableHeaderStyle: Style{FgLightCyan}, TableSeparatorStyle: Style{FgGray}, + HeatmapStyle: Style{FgDefault}, + HeatmapHeaderStyle: Style{FgLightCyan}, + HeatmapSeparatorStyle: Style{FgDefault}, SectionStyle: Style{Bold, FgYellow}, BulletListTextStyle: Style{FgDefault}, BulletListBulletStyle: Style{FgGray}, @@ -81,6 +84,9 @@ type Theme struct { TableStyle Style TableHeaderStyle Style TableSeparatorStyle Style + HeatmapStyle Style + HeatmapHeaderStyle Style + HeatmapSeparatorStyle Style SectionStyle Style BulletListTextStyle Style BulletListBulletStyle Style diff --git a/vendor/modules.txt b/vendor/modules.txt index 1ac9d957..0e93c367 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -240,7 +240,7 @@ github.com/pjbgf/sha1cd/ubc # github.com/pkg/errors v0.9.1 ## explicit github.com/pkg/errors -# github.com/pterm/pterm v0.12.69 +# github.com/pterm/pterm v0.12.71 ## explicit; go 1.21 github.com/pterm/pterm github.com/pterm/pterm/internal