File: path.go

package info (click to toggle)
gdu 5.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: makefile: 122
file content (26 lines) | stat: -rw-r--r-- 509 bytes parent folder | download | duplicates (2)
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
}