forked from oqapps/Qlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
138 lines (127 loc) · 2.87 KB
/
helpers.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"strings"
"time"
"github.com/asaskevich/govalidator"
valid "github.com/asaskevich/govalidator"
"github.com/iancoleman/strcase"
)
type Value struct {
real interface{}
display string
}
type Display struct {
value Value
typeText string
}
type Manager struct {
Displays map[string]Display
}
/*func (manager Manager) Length() int {
switch plistType {
case "Array":
return len(arrayPlist)
case "Dictionary":
return len(plistData.Keys)
default:
return 0
}
}
func (manager Manager) Keys() []string {
children := []string{}
switch plistType {
case "Array":
for i := 0; i < len(arrayPlist); i++ {
children = append(children, strconv.Itoa(i))
}
return children
case "Dictionary":
return plistData.Keys
default:
return children
}
}*/
func (manager Manager) Display(entry Entry) Display {
if manager.Displays == nil {
manager.Displays = make(map[string]Display)
}
display := manager.Displays[entry.path]
if display.typeText == "" {
typeText, value := manager.GetType(entry)
display = Display{typeText: typeText, value: value}
manager.Displays[entry.path] = display
}
return display
}
func GetFileName(path string) string {
unix := govalidator.IsUnixFilePath(path)
windows := govalidator.IsWinFilePath(path)
var sp []string
if windows {
sp = strings.Split(path, "\\")
} else if unix {
sp = strings.Split(path, "/")
} else {
return "Untitled.plist"
}
return sp[len(sp)-1]
}
func dataString(data []byte) string {
var builder strings.Builder
builder.WriteString("<")
for i, b := range data {
builder.WriteString(fmt.Sprintf("%02X", b))
if (i+1)%4 == 0 && i != len(data)-1 {
builder.WriteString(" ")
}
}
builder.WriteString(">")
return builder.String()
}
func (manager Manager) GetType(entry Entry) (string, Value) {
var t string
var value Value
if entry.value == nil {
if entry.isParent {
if entry.array {
t = "Array"
value = Value{display: fmt.Sprintf("%v children", len(entry.children))}
if len(entry.children) == 1 {
value.display = "1 child"
}
} else {
t = "Dictionary"
value = Value{display: fmt.Sprintf("%v key/value entries", len(entry.children))}
if len(entry.children) == 1 {
value.display = "1 key/value entry"
}
}
} else {
t = "String"
value = Value{display: fmt.Sprintf("%v", entry.value)}
}
} else {
value = Value{display: fmt.Sprintf("%v", entry.value), real: entry.value}
_, isBool := entry.value.(bool)
_, isString := entry.value.(string)
ti, isDate := entry.value.(time.Time)
if isBool {
t = "Boolean"
value.display = strcase.ToCamel(value.display)
} else if isString {
t = "String"
} else if isDate {
value.display = ti.String()
t = "Date"
} else {
if valid.IsInt(value.display) {
t = "Number"
} else {
value.display = dataString(entry.value.([]uint8))
t = "Data"
}
}
}
return t, value
}