-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
79 lines (69 loc) · 1.92 KB
/
util.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package library
import (
"image/color"
"path/filepath"
"strings"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/container"
"github.com/jvatic/audible-downloader/audible"
)
func BookStatusText(b *audible.Book) string {
if b.LocalPath == "" {
return "Status: Not Downloaded"
}
return "Status: Downloaded"
}
func PathFromFyneURI(uri fyne.ListableURI) string {
if uri == nil {
return ""
}
return filepath.Join(strings.SplitAfter(strings.TrimPrefix(uri.String(), "file://"), "/")...)
}
// FormatFilePath tries to fit the given path within the given width
func FormatFilePath(p string, width int) string {
textSize := fyne.CurrentApp().Settings().Theme().TextSize()
textStyle := fyne.TextStyle{Bold: true}
textWidth := fyne.MeasureText(p, textSize, textStyle).Width
if textWidth <= width {
// text will fit without modification
return p
}
parts := strings.SplitAfter(p, string(filepath.Separator))
ellipsis := "..."
ellipsisWidth := fyne.MeasureText(ellipsis, textSize, textStyle).Width
delta := textWidth - width
for i := 1; i < len(parts)-1; i++ {
if fyne.MeasureText(parts[i], textSize, textStyle).Width-ellipsisWidth >= delta {
parts[i] = ellipsis
return filepath.Join(parts...)
}
}
a := parts[:2]
var b []string
c := parts[len(parts)-1:]
parts = parts[2 : len(parts)-1]
for i := 0; i < len(parts); i++ {
if textWidth <= width {
b = append(b, parts[i:]...)
break
}
textWidth -= fyne.MeasureText(parts[i], textSize, textStyle).Width
}
if textWidth > width {
textWidth -= fyne.MeasureText(a[0], textSize, textStyle).Width
a = []string{string(filepath.Separator)}
}
b = append(b, c...)
return filepath.Join(filepath.Join(a...), ellipsis, filepath.Join(b...))
}
func Indent(items ...fyne.CanvasObject) fyne.CanvasObject {
indentStr := ""
for i := 0; i < 4; i++ {
indentStr += " "
}
return container.NewHBox(
canvas.NewText(indentStr, color.Black),
container.NewVBox(items...),
)
}