-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathviewer_view.go
More file actions
586 lines (519 loc) · 13.9 KB
/
viewer_view.go
File metadata and controls
586 lines (519 loc) · 13.9 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package main
import (
"time"
"context"
"fmt"
"unicode/utf8"
"path/filepath"
"github.com/unxed/f4/vfs"
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
"github.com/unxed/vtui/piecetable"
"github.com/mattn/go-runewidth"
)
// ViewerView is a high-performance file viewer component.
type ViewerView struct {
vtui.BaseFrame
topBar *TopBar
menuBar *vtui.MenuBar
backend *ViewerBackend
vfs vfs.VFS
path string
HexMode bool
WrapMode bool
TopOffset int64 // Current byte offset of the first visible line
// For Text mode: offsets of lines currently on screen
lineOffsets []int64
eofVisible bool
lastKnownSize int64
scrollBar *vtui.ScrollBar
}
func NewViewerView(ctx context.Context, v vfs.VFS, path string) (*ViewerView, error) {
backend, err := NewViewerBackend(ctx, v, path)
if err != nil {
return nil, err
}
vv := &ViewerView{
backend: backend,
vfs: v,
path: path,
WrapMode: true,
}
vv.scrollBar = vtui.NewScrollBar(0, 0, 0)
vv.scrollBar.SetOwner(vv)
vv.scrollBar.OnScroll = func(v int) {
newOff := int64(v)
if vv.HexMode {
newOff &= ^int64(0xF)
} else {
// Optimization: during fast drag, don't FindLineStart every pixel
// unless we are close to the target or moving slowly.
// For now, simple snap.
newOff = vv.backend.FindLineStart(newOff)
}
if newOff != vv.TopOffset {
vv.TopOffset = newOff
vtui.FrameManager.Redraw()
}
}
vv.scrollBar.OnStep = func(step int) {
// Used for arrows and track clicks: perform logical steps
switch step {
case -1: vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_UP})
case 1: vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_DOWN})
case -2: vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_PRIOR})
case 2: vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_NEXT})
}
vtui.FrameManager.Redraw()
}
vv.menuBar = vtui.NewMenuBar(nil)
vv.menuBar.Items = []vtui.MenuBarItem{
{Label: "&File", SubItems: []vtui.MenuItem{{Text: "E&xit", Command: vtui.CmClose}}},
{Label: "&View", SubItems: []vtui.MenuItem{{Text: "&Hex", Command: vtui.CmDefault}, {Text: "&Wrap"}}},
{Label: "&Options", SubItems: []vtui.MenuItem{{Text: "&Settings"}}},
}
vv.topBar = NewTopBar(func() string {
percent := 0
size := vv.backend.Size()
if size > 0 {
viewHeightBytes := int64(vv.Y2 - vv.Y1)
if vv.HexMode {
viewHeightBytes *= 16
} else {
viewHeightBytes *= 80
}
if size <= viewHeightBytes {
percent = 100
} else {
denominator := size - viewHeightBytes
percent = int((vv.TopOffset * 100) / denominator)
}
if percent < 0 { percent = 0 }
if percent > 100 { percent = 100 }
}
mode := Msg("Viewer.ModeText")
if vv.HexMode { mode = Msg("Viewer.ModeHex") }
base := ""
if vv.vfs != nil {
base = vv.vfs.Base(vv.path)
} else {
base = filepath.Base(vv.path)
}
return fmt.Sprintf(" %s │ %s │ %d%% ", base, mode, percent)
})
vv.topBar.SetVisible(true)
vv.SetCanFocus(true)
vv.SetFocus(true)
return vv, nil
}
func (vv *ViewerView) SetPosition(x1, y1, x2, y2 int) {
vv.ScreenObject.SetPosition(x1, y1, x2, y2)
if vv.topBar != nil {
vv.topBar.SetPosition(x1, y1, x2, y1)
}
if vv.menuBar != nil {
vv.menuBar.SetPosition(x1, 0, x2, 0)
}
if vv.scrollBar != nil {
vv.scrollBar.SetPosition(x2, y1+1, x2, y2)
}
}
func (vv *ViewerView) GetMenuBar() *vtui.MenuBar {
return vv.menuBar
}
func (vv *ViewerView) HandleCommand(cmd int, args any) bool {
if cmd == vtui.CmClose {
vv.Close()
return true
}
if cmd == CmSearch {
actionViewerSearch(vv)
return true
}
return vv.BaseFrame.HandleCommand(cmd, args)
}
func (vv *ViewerView) Show(scr *vtui.ScreenBuf) {
vv.ScreenObject.Show(scr)
if vv.topBar != nil {
vv.topBar.Show(scr)
}
vv.DisplayObject(scr)
}
func (vv *ViewerView) DisplayObject(scr *vtui.ScreenBuf) {
if !vv.IsVisible() {
return
}
// AUTO-SCROLL LOGIC (tail -f)
currentSize := vv.backend.Size()
if vv.eofVisible && currentSize > vv.lastKnownSize && !vv.Busy {
vv.lastKnownSize = currentSize
vv.jumpToEnd()
return
}
vv.lastKnownSize = currentSize
width := vv.X2 - vv.X1 + 1
if vv.scrollBar != nil {
width-- // Не рисуем текст поверх скроллбара
}
height := vv.Y2 - vv.Y1 + 1
contentHeight := height - 1
bgAttr := vtui.Palette[ColViewerText]
// 1. Draw Background
scr.FillRect(vv.X1, vv.Y1+1, vv.X2, vv.Y2, ' ', bgAttr)
if vv.Busy {
scr.Write(vv.X1, vv.Y1+1, vtui.StringToCharInfo(" [ Loading... ] ", bgAttr))
return
}
if contentHeight > 0 {
if vv.HexMode {
vv.renderHex(scr, width, contentHeight)
} else {
vv.renderText(scr, width, contentHeight)
}
}
if vv.scrollBar != nil && vv.backend.Size() > 0 {
vv.scrollBar.SetParams(int(vv.TopOffset), 0, int(vv.backend.Size()))
vv.scrollBar.Show(scr)
}
}
func (vv *ViewerView) renderHex(scr *vtui.ScreenBuf, width, contentHeight int) {
attr := vtui.Palette[ColViewerText]
offAttr := vtui.Palette[ColViewerArrows]
currOffset := vv.TopOffset &^ 0xF // Align to 16 bytes
//lastRowWasEOF := false
for y := 0; y < contentHeight; y++ {
if currOffset >= vv.backend.Size() {
//lastRowWasEOF = true
break
}
data, err := vv.backend.ReadAt(currOffset, 16)
if err == piecetable.ErrLoading {
scr.Write(vv.X1, vv.Y1+1+y, vtui.StringToCharInfo(" [ Loading... ] ", attr))
break
}
line := fmt.Sprintf("%010X: ", currOffset)
scr.Write(vv.X1, vv.Y1+1+y, vtui.StringToCharInfo(line, offAttr))
// Hex part
hexStr := ""
for i := 0; i < 16; i++ {
if i < len(data) {
hexStr += fmt.Sprintf("%02X ", data[i])
} else {
hexStr += " "
}
if i == 7 {
hexStr += " "
}
}
scr.Write(vv.X1+12, vv.Y1+1+y, vtui.StringToCharInfo(hexStr, attr))
// ASCII part
asciiStr := "│ "
for i := 0; i < len(data); i++ {
r := rune(data[i])
if r < 32 || r > 126 {
r = '.'
}
asciiStr += string(r)
}
scr.Write(vv.X1+12+50, vv.Y1+1+y, vtui.StringToCharInfo(asciiStr, attr))
currOffset += 16
}
vv.eofVisible = (currOffset >= vv.backend.Size())
}
func (vv *ViewerView) renderText(scr *vtui.ScreenBuf, width, contentHeight int) {
attr := vtui.Palette[ColViewerText]
currOffset := vv.TopOffset
vv.lineOffsets = vv.lineOffsets[:0]
//lastRowWasEOF := false
for y := 0; y < contentHeight; y++ {
vv.lineOffsets = append(vv.lineOffsets, currOffset)
if currOffset >= vv.backend.Size() {
//lastRowWasEOF = true
break
}
// Read a generous chunk to handle wrapping
data, err := vv.backend.ReadAt(currOffset, width*4)
if err == piecetable.ErrLoading {
scr.Write(vv.X1, vv.Y1+1+y, vtui.StringToCharInfo(" [ Loading... ] ", attr))
break
}
if len(data) == 0 {
break
}
lineLen := 0
textLen := 0
visualWidth := 0
foundNewline := false
for lineLen < len(data) {
r, size := utf8.DecodeRune(data[lineLen:])
if r == '\n' {
lineLen += size
foundNewline = true
break
}
if r == '\r' {
lineLen += size
continue
}
rw := runewidth.RuneWidth(r)
if vv.WrapMode && visualWidth+rw > width {
// Wrap occurred
break
}
visualWidth += rw
lineLen += size
textLen = lineLen
}
scr.Write(vv.X1, vv.Y1+1+y, vtui.StringToCharInfo(string(data[:textLen]), attr))
currOffset += int64(lineLen)
if !foundNewline && !vv.WrapMode {
// In no-wrap mode, we must consume until the actual newline
tempOff := currOffset
for {
b, err := vv.backend.ReadAt(tempOff, 1024)
if err != nil || len(b) == 0 { break }
found := false
for i, char := range b {
if char == '\n' {
tempOff += int64(i + 1)
found = true
break
}
}
if found { break }
tempOff += int64(len(b))
}
currOffset = tempOff
}
}
vv.eofVisible = (currOffset >= vv.backend.Size())
}
func (vv *ViewerView) ProcessKey(e *vtinput.InputEvent) bool {
if !e.KeyDown {
return false
}
ctrl := (e.ControlKeyState & (vtinput.LeftCtrlPressed | vtinput.RightCtrlPressed)) != 0
if e.VirtualKeyCode == vtinput.VK_TAB && ctrl {
return false
}
//height := int64(vv.Y2 - vv.Y1 + 1)
step := int64(1)
if vv.HexMode {
step = 16
}
contentHeight := int64(vv.Y2 - vv.Y1) // height - 1 (status line)
switch e.VirtualKeyCode {
case vtinput.VK_ESCAPE, vtinput.VK_F10, vtinput.VK_F3:
vv.Close()
return true
case vtinput.VK_F2:
vv.WrapMode = !vv.WrapMode
return true
case vtinput.VK_F4:
vv.HexMode = !vv.HexMode
if vv.HexMode {
vv.TopOffset &= ^0xF
}
return true
case vtinput.VK_DOWN:
if vv.eofVisible {
return true // Prevent scrolling past End of File
}
if vv.HexMode {
if vv.TopOffset+16 < vv.backend.Size() {
vv.TopOffset += 16
}
} else if len(vv.lineOffsets) > 1 {
vv.TopOffset = vv.lineOffsets[1]
} else {
// Fail-safe: if lineOffsets not populated (e.g. before first render),
// try to proactively find the next line start from current offset.
width := vv.X2 - vv.X1 + 1
if vv.scrollBar != nil {
width--
}
data, err := vv.backend.ReadAt(vv.TopOffset, width*4)
if err == nil && len(data) > 0 {
lineLen := 0
visualWidth := 0
for lineLen < len(data) {
r, size := utf8.DecodeRune(data[lineLen:])
if r == '\n' {
lineLen += size
break
}
rw := runewidth.RuneWidth(r)
if vv.WrapMode && visualWidth+rw > width {
break
}
visualWidth += rw
lineLen += size
}
if lineLen > 0 {
vv.TopOffset += int64(lineLen)
}
}
}
return true
case vtinput.VK_UP:
if vv.HexMode {
vv.TopOffset -= step
} else {
vv.TopOffset = vv.backend.FindLineStart(vv.TopOffset - 1)
}
if vv.TopOffset < 0 {
vv.TopOffset = 0
}
return true
case vtinput.VK_NEXT: // PgDn
if vv.HexMode {
vv.TopOffset += 16 * contentHeight
if vv.TopOffset >= vv.backend.Size() {
vv.TopOffset = (vv.backend.Size() - 1) &^ 0xF
if vv.TopOffset < 0 {
vv.TopOffset = 0
}
}
} else if len(vv.lineOffsets) > 0 {
vv.TopOffset = vv.lineOffsets[len(vv.lineOffsets)-1]
}
return true
case vtinput.VK_PRIOR: // PgUp
if vv.HexMode {
vv.TopOffset -= step * contentHeight
} else {
for i := 0; i < int(contentHeight); i++ {
vv.TopOffset = vv.backend.FindLineStart(vv.TopOffset - 1)
}
}
if vv.TopOffset < 0 {
vv.TopOffset = 0
}
return true
case vtinput.VK_HOME:
vv.TopOffset = 0
return true
case vtinput.VK_END:
vv.jumpToEnd()
return true
}
return false
}
func (vv *ViewerView) jumpToEnd() {
contentHeight := int64(vv.Y2 - vv.Y1)
if vv.HexMode {
if vv.backend.Size() == 0 {
vv.TopOffset = 0
} else {
lastLineOffset := (vv.backend.Size() - 1) &^ 0xF
vv.TopOffset = lastLineOffset - (contentHeight-1)*16
if vv.TopOffset < 0 { vv.TopOffset = 0 }
}
return
}
if vv.backend.Size() == 0 {
vv.TopOffset = 0
return
}
vv.Busy = true
vtui.RunAsync(func(ctx *vtui.TaskContext) {
defer ctx.RunOnUI(func() { vv.Busy = false })
width := vv.X2 - vv.X1 + 1
if vv.scrollBar != nil { width-- }
chunkSize := contentHeight * int64(width) * 4
if chunkSize < 16*1024 { chunkSize = 16*1024 }
startOff := vv.backend.Size() - chunkSize
if startOff < 0 { startOff = 0 }
if startOff < vv.backend.Size() - 1024*1024 {
startOff = vv.backend.Size() - 1024*1024
}
if startOff < 0 { startOff = 0 }
for {
if ctx.Err() != nil { return }
_, err := vv.backend.ReadAt(startOff, 1024)
if err != piecetable.ErrLoading { break }
time.Sleep(10 * time.Millisecond)
}
startOff = vv.backend.FindLineStart(startOff)
var offsets []int64
currOff := startOff
for currOff < vv.backend.Size() {
if ctx.Err() != nil { return }
data, err := vv.backend.ReadAt(currOff, 64*1024)
if err == piecetable.ErrLoading {
time.Sleep(20 * time.Millisecond)
continue
}
if err != nil || len(data) == 0 { break }
scanPos := 0
for scanPos < len(data) {
offsets = append(offsets, currOff + int64(scanPos))
lineLen := 0
visualWidth := 0
foundNewline := false
for scanPos+lineLen < len(data) {
r, size := utf8.DecodeRune(data[scanPos+lineLen:])
if r == '\n' { lineLen += size; foundNewline = true; break }
if r == '\r' { lineLen += size; continue }
rw := runewidth.RuneWidth(r)
if vv.WrapMode && visualWidth+rw > width { break }
visualWidth += rw
lineLen += size
}
scanPos += lineLen
if !foundNewline && !vv.WrapMode { break }
}
currOff += int64(scanPos)
}
ctx.RunOnUI(func() {
if int64(len(offsets)) <= contentHeight {
vv.TopOffset = startOff
} else {
vv.TopOffset = offsets[len(offsets)-int(contentHeight)]
}
vtui.FrameManager.Redraw()
})
})
}
func (vv *ViewerView) ProcessMouse(e *vtinput.InputEvent) bool {
if e.Type != vtinput.MouseEventType {
return false
}
if vv.scrollBar != nil && vv.scrollBar.ProcessMouse(e) {
return true
}
if e.WheelDirection != 0 {
if e.WheelDirection > 0 {
vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_UP})
} else {
vv.ProcessKey(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, VirtualKeyCode: vtinput.VK_DOWN})
}
return true
}
return false
}
func (vv *ViewerView) ResizeConsole(w, h int) { vv.SetPosition(0, 0, w-1, h-2) }
func (vv *ViewerView) Close() {
if GlobalFileState != nil && vv.path != "" {
GlobalFileState.SaveViewerState(vv.path, vv.TopOffset, vv.WrapMode, vv.HexMode)
}
if vv.backend != nil {
vv.backend.Close()
}
vv.BaseFrame.Close()
}
func (vv *ViewerView) GetKeyLabels() *vtui.KeySet {
return &vtui.KeySet{
Normal: vtui.KeyBarLabels{
Msg("KeyBar.ViewerF1"), Msg("KeyBar.ViewerF2"), Msg("KeyBar.ViewerF3"), Msg("KeyBar.ViewerF4"),
"", "", Msg("KeyBar.ViewerF7"), "", "", Msg("KeyBar.ViewerF10"),
},
}
}
func (vv *ViewerView) GetType() vtui.FrameType { return vtui.TypeUser + 3 }
func (vv *ViewerView) GetTitle() string {
if vv.path != "" {
return "View: " + filepath.Base(vv.path)
}
return "Viewer"
}