pancap/common/common.go

36 lines
802 B
Go
Raw Normal View History

package common
2019-11-27 19:24:12 +00:00
import "fmt"
// Appends the appendee to the array if it does not contain appendee yet
func AppendIfUnique(appendee string, array []string) []string {
2019-11-27 19:24:12 +00:00
// Iterate over all elements and check values
for _, elem := range array {
if elem == appendee {
// ... found. Stop here
return array
}
}
// None found, append
return append(array, appendee)
}
2019-12-03 22:51:03 +00:00
// Generates a small ASCII tree for the given string array
func GenerateTree(strarr []string) string {
tmpstr := ""
2019-11-27 19:24:12 +00:00
// iterate over each element
for iter, elem := range strarr {
// check if we got the last element
2023-09-02 21:49:02 +00:00
if iter < len(strarr)-1 {
tmpstr = fmt.Sprintf("%s├ %s\n", tmpstr, elem)
2019-11-27 19:24:12 +00:00
} else {
2023-09-02 21:49:02 +00:00
tmpstr = fmt.Sprintf("%s╰ %s\n", tmpstr, elem)
2019-11-27 19:24:12 +00:00
}
}
2019-12-03 22:51:03 +00:00
// Return constructed (grown?) tree
return tmpstr
2019-11-27 19:24:12 +00:00
}