Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Widest for returning the widest member of a string slice #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions size.go
Expand Up @@ -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
Expand Down