1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
package path
import "strings"
// ShortenPath removes the last but one path components to fit into maxLen
func ShortenPath(path string, maxLen int) string {
if len(path) <= maxLen {
return path
}
res := ""
parts := strings.SplitAfter(path, "/")
curLen := len(parts[len(parts)-1]) // count lenght of last part for start
for _, part := range parts[:len(parts)-1] {
curLen += len(part)
if curLen > maxLen {
res += ".../"
break
}
res += part
}
res += parts[len(parts)-1]
return res
}
|