From 69ebcc41e06f9bb6e8e62db9c9fc1b8c2995950e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 19 Jun 2023 15:06:16 -0400 Subject: [PATCH] feat: add Widest for returning the widest member of a string slice func Widest([]string) int --- size.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/size.go b/size.go index 439a5cb8..10a1c9f3 100644 --- a/size.go +++ b/size.go @@ -23,6 +23,20 @@ func Width(str string) (width int) { return width } +// Widest returns the string with the widest cell with in a slice of strings. +// ANSI sequences are ignored. If a string contains a newline it will be +// treated as two separate strings, returning only the widest. +func Widest(strs []string) (width int) { + for _, str := range strs { + w := Width(str) + if w > width { + width = w + } + } + + return width +} + // Height returns height of a string in cells. This is done simply by // counting \n characters. If your strings use \r\n for newlines you should // convert them to \n first, or simply write a separate function for measuring