-
Notifications
You must be signed in to change notification settings - Fork 55
/
sortState.go
104 lines (91 loc) · 1.85 KB
/
sortState.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
package serverHandler
type dirSort int
const (
dirSortFirst dirSort = -1
dirSortMixed dirSort = 0
dirSortLast dirSort = 1
)
const (
nameAsc byte = 'n'
nameDesc byte = 'N'
typeAsc byte = 'e'
typeDesc byte = 'E'
sizeAsc byte = 's'
sizeDesc byte = 'S'
timeAsc byte = 't'
timeDesc byte = 'T'
)
type SortState struct {
dirSort dirSort
key byte
}
func (info SortState) DirSort() dirSort {
return info.dirSort
}
func (info SortState) Key() string {
return string(info.key)
}
func (info SortState) mergeDirWithKey(key byte) string {
switch info.dirSort {
case dirSortFirst:
return "/" + string(key)
case dirSortLast:
return string(key) + "/"
default:
return string(key)
}
}
func (info SortState) CurrentSort() string {
return info.mergeDirWithKey(info.key)
}
func (info SortState) NextDirSort() string {
switch info.dirSort {
case dirSortFirst: // next is dirSortLast
return string(info.key) + "/"
case dirSortLast: // next is dirSortMixed
return string(info.key)
case dirSortMixed: // next is dirSortFirst
return "/" + string(info.key)
}
return "/" + string(info.key)
}
func (info SortState) NextNameSort() string {
var nextKey byte
switch info.key {
case nameAsc:
nextKey = nameDesc
default:
nextKey = nameAsc
}
return info.mergeDirWithKey(nextKey)
}
func (info SortState) NextTypeSort() string {
var nextKey byte
switch info.key {
case typeAsc:
nextKey = typeDesc
default:
nextKey = typeAsc
}
return info.mergeDirWithKey(nextKey)
}
func (info SortState) NextSizeSort() string {
var nextKey byte
switch info.key {
case sizeDesc:
nextKey = sizeAsc
default:
nextKey = sizeDesc
}
return info.mergeDirWithKey(nextKey)
}
func (info SortState) NextTimeSort() string {
var nextKey byte
switch info.key {
case timeDesc:
nextKey = timeAsc
default:
nextKey = timeDesc
}
return info.mergeDirWithKey(nextKey)
}