-
Notifications
You must be signed in to change notification settings - Fork 1
/
navigation.go
330 lines (279 loc) · 6.69 KB
/
navigation.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2013-2021 Utkan Güngördü <[email protected]>
* Copyright (c) 2021-2024 Piotr Grabowski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gomicsv
import (
"errors"
"log"
"math/rand"
"os"
"path/filepath"
"github.com/fauu/gomicsv/archive"
"github.com/fauu/gomicsv/imgdiff"
)
func (app *App) randomPage() {
if !app.archiveIsLoaded() {
return
}
if app.S.Archive.Len() == nil {
return
}
app.setPage(rand.Int() % *app.S.Archive.Len())
}
func (app *App) previousPage() {
if !app.archiveIsLoaded() {
if app.Config.Seamless {
app.previousArchive()
}
return
}
if app.Config.Random && app.S.Archive.Len() != nil {
app.randomPage()
return
}
n := 1
if app.Config.DoublePage && app.S.ArchivePos > 1 {
n = 2
}
if app.Config.Seamless && app.S.ArchivePos+1 <= n {
app.previousArchive()
return
}
app.setPage(app.S.ArchivePos - n)
if app.Config.DoublePage &&
app.shouldForceSinglePage() &&
app.S.Archive.Len() != nil &&
*app.S.Archive.Len()-app.S.ArchivePos > 1 {
app.nextPage()
}
}
func (app *App) nextPage() {
if !app.archiveIsLoaded() {
if app.Config.Seamless {
app.nextArchive()
}
return
}
if app.Config.Random && app.S.Archive.Len() != nil {
app.randomPage()
return
}
n := 1
if app.Config.DoublePage &&
!app.shouldForceSinglePage() &&
app.S.Archive.Len() != nil &&
*app.S.Archive.Len() > app.S.ArchivePos+2 {
n = 2
}
if app.Config.Seamless &&
app.S.Archive.Len() != nil &&
*app.S.Archive.Len()-app.S.ArchivePos <= n {
app.nextArchive()
return
}
app.setPage(app.S.ArchivePos + n)
}
func (app *App) firstPage() {
if !app.archiveIsLoaded() {
return
}
app.setPage(0)
}
func (app *App) lastPage() {
if !app.archiveIsLoaded() {
return
}
if app.S.Archive.Len() == nil {
return
}
offset := -1
if app.Config.DoublePage && *app.S.Archive.Len() >= 2 {
offset = -2
}
app.setPage(*app.S.Archive.Len() + offset)
}
func (app *App) imageHash(n int) (imgdiff.Hash, bool) {
if hash, ok := app.S.ImageHashes[n]; ok {
return hash, true
}
pixbuf, err := app.S.Archive.Load(n, app.Config.EmbeddedOrientation, 0)
if err != nil {
app.showError(err.Error())
return 0, false
}
return imgdiff.DHash(pixbuf), true
}
func (app *App) skipForward() {
app.setPage(app.S.ArchivePos + app.Config.NSkip)
}
func (app *App) skipBackward() {
app.setPage(app.S.ArchivePos - app.Config.NSkip)
}
func (app *App) nextScene() {
if !app.archiveIsLoaded() {
return
}
if app.S.Archive.Len() == nil {
return
}
if app.S.PixbufL == nil {
return
}
hash := imgdiff.DHash(app.S.PixbufL)
dn := app.Config.SceneScanSkip
if *app.S.Archive.Len()-1-app.S.ArchivePos <= dn {
dn = 1
}
for n := app.S.ArchivePos + 1; n < *app.S.Archive.Len(); n += dn {
h, ok := app.imageHash(n)
if !ok {
return
}
distance := float32(imgdiff.Distance(hash, h)) / 64
if distance > app.Config.ImageDiffThres {
if dn == 1 || n == app.S.ArchivePos+1 {
app.doSetPage(n)
return
}
// Did we go too fast?
for l := n - 1; l >= app.S.ArchivePos+1; l-- {
h, ok := app.imageHash(l)
if !ok {
return
}
d := float32(imgdiff.Distance(hash, h)) / 64
if d <= app.Config.ImageDiffThres {
app.doSetPage(l + 1)
return
}
}
return
}
}
}
func (app *App) previousScene() {
if !app.archiveIsLoaded() {
return
}
if app.S.PixbufL == nil {
return
}
hash := imgdiff.DHash(app.S.PixbufL)
dn := app.Config.SceneScanSkip
if app.S.ArchivePos <= dn {
dn = 1
}
for n := app.S.ArchivePos - 1; n >= 0; n -= dn {
h, ok := app.imageHash(n)
if !ok {
return
}
distance := float32(imgdiff.Distance(hash, h)) / 64
if distance > app.Config.ImageDiffThres {
if dn == 1 || n == app.S.ArchivePos-1 {
app.doSetPage(n)
return
}
// Did we go too fast?
for l := n + 1; l <= app.S.ArchivePos-1; l++ {
h, ok := app.imageHash(l)
if !ok {
return
}
d := float32(imgdiff.Distance(hash, h)) / 64
if d <= app.Config.ImageDiffThres {
app.doSetPage(l - 1)
return
}
}
return
}
}
}
// TODO(fau): Distinguish a failiure from the "no next archive" condition and inform the user accordingly
func (app *App) nextArchive() bool {
newName, err := app.archiveNameRelativeToCurrent(1)
if err != nil {
log.Printf("Error getting next archive: %v", err)
return false
}
app.loadArchiveFromPath(newName)
return true
}
// TODO(fau): Distinguish a failiure from the "no previous archive" condition and inform the user accordingly
func (app *App) previousArchive() bool {
newName, err := app.archiveNameRelativeToCurrent(-1)
if err != nil {
log.Printf("Error getting previous archive: %v", err)
return false
}
app.loadArchiveFromPath(newName)
app.lastPage()
return true
}
// currentArchiveIdx determines the index of the current archive in the directory. We need to do
// this every time, since the filesystem is mutable
func (app *App) currentArchiveIdx() (idx int, err error) {
dir, name := filepath.Split(app.S.ArchivePath)
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return
}
}
arNames, err := archive.ListInDirectory(dir)
if err != nil {
return
}
idx = -1
for i := 0; i < len(arNames); i++ {
if arNames[i] == name {
idx = i
}
}
if idx == -1 {
return 0, errors.New("could not find the current archive in the current directory. Deleted, perhaps?")
}
return
}
// archiveNameRelativeToCurrent gets the name of the archive in the current directory whose
// relative position with regards to the current archive is equal to relIdx
// TODO(utkan): Use inotify to avoid obtaining list from the scratch all the time
func (app *App) archiveNameRelativeToCurrent(relIdx int) (newName string, err error) {
dir, _ := filepath.Split(app.S.ArchivePath)
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return
}
}
arNames, err := archive.ListInDirectory(dir)
if err != nil {
return
}
currIdx, err := app.currentArchiveIdx()
if err != nil {
return "", nil
}
idx := currIdx + relIdx
if idx < 0 || idx >= len(arNames) {
err = errors.New("no more archives in the directory")
return
}
newName = filepath.Join(dir, arNames[idx])
return
}