/
oviewer.go
941 lines (849 loc) 路 24 KB
/
oviewer.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
package oviewer
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"regexp"
"sync"
"syscall"
"code.rocketnine.space/tslocum/cbind"
"github.com/fsnotify/fsnotify"
"github.com/gdamore/tcell/v2"
)
// Root structure contains information about the drawing.
type Root struct {
// tcell.Screen is the root screen.
tcell.Screen
// Doc contains the model of ov
Doc *Document
// help
helpDoc *Document
// log
logDoc *Document
// input contains the input mode.
input *Input
// cancelFunc saves the cancel function, which is a time-consuming process.
cancelFunc context.CancelFunc
// searchReg for on-screen highlighting.
searchReg *regexp.Regexp
// keyConfig contains the binding settings for the key.
keyConfig *cbind.Configuration
// inputKeyConfig contains the binding settings for the key.
inputKeyConfig *cbind.Configuration
// searchWord for on-screen highlighting.
searchWord string
// Original string.
OriginStr string
// message is the message to display.
message string
// DocList
DocList []*Document
// cancelKeys represents the cancellation key string.
cancelKeys []string
// Config contains settings that determine the behavior of ov.
Config
// screenMode represents the mode of screen.
screenMode ScreenMode
// mu controls the RWMutex.
mu sync.RWMutex
// Original position at the start of search.
OriginPos int
// CurrentDoc is the index of the current document.
CurrentDoc int
scr SCR
// x1, y1, x2, y2 are the coordinates selected by the mouse.
x1 int
y1 int
x2 int
y2 int
// headerLen is the actual header length when wrapped.
headerLen int
// statusPos is the position of the status line.
statusPos int
// minStartX is the minimum start position of x.
minStartX int
// skipDraw skips draw once when true.
// skipDraw is set to true when the mouse cursor just moves (no event occurs).
skipDraw bool
// mousePressed is a flag when the mouse selection button is pressed.
mousePressed bool
// mouseSelect is a flag with mouse selection.
mouseSelect bool
// mouseRectangle is a flag for rectangle selection.
mouseRectangle bool
}
// SCR contains the screen information.
type SCR struct {
// numbers is the line information of the currently displayed screen.
// numbers (number of logical numbers and number of wrapping numbers) from y on the screen.
numbers []LineNumber
// vWidth represents the screen width.
vWidth int
// vHeight represents the screen height.
vHeight int
// startX is the start position of x.
startX int
}
// LineNumber is Number of logical lines and number of wrapping lines on the screen.
type LineNumber struct {
number int
wrap int
}
// general structure contains the general of the display.
// general contains values that determine the behavior of each document.
type general struct {
// ColumnDelimiterReg is a compiled regular expression of ColumnDelimiter.
ColumnDelimiterReg *regexp.Regexp
// ColumnDelimiter is a column delimiter.
ColumnDelimiter string
// SectionDelimiterReg is a section delimiter.
SectionDelimiterReg *regexp.Regexp
// SectionDelimiter is a section delimiter.
SectionDelimiter string
// Specified string for jumpTarget.
JumpTargetString string
// MultiColorWords specifies words to color separated by spaces.
MultiColorWords []string
// TabWidth is tab stop num.
TabWidth int
// HeaderLen is number of header rows to be fixed.
Header int
// SkipLines is the rows to skip.
SkipLines int
// WatchInterval is the watch interval (seconds).
WatchInterval int
// MarkStyleWidth is width to apply the style of the marked line.
MarkStyleWidth int
// SectionStartPosition is a section start position.
SectionStartPosition int
// JumpTarget is the display position of search results.
JumpTarget int
// AlternateRows alternately style rows.
AlternateRows bool
// ColumnMode is column mode.
ColumnMode bool
// ColumnWidth is column width mode.
ColumnWidth bool
// ColumnRainbow is column rainbow.
ColumnRainbow bool
// LineNumMode displays line numbers.
LineNumMode bool
// Wrap is Wrap mode.
WrapMode bool
// FollowMode is the follow mode.
FollowMode bool
// FollowAll is a follow mode for all documents.
FollowAll bool
// FollowSection is a follow mode that uses section instead of line.
FollowSection bool
// FollowName is the mode to follow files by name.
FollowName bool
// PlainMode is whether to enable the original character decoration.
PlainMode bool
}
// Config represents the settings of ov.
type Config struct {
// KeyBinding
Keybind map[string][]string
// Mode represents the operation of the customized mode.
Mode map[string]general
// StyleAlternate is a style that applies line by line.
StyleAlternate OVStyle
// StyleHeader is the style that applies to the header.
StyleHeader OVStyle
// StyleBody is the style that applies to the body.
StyleBody OVStyle
// StyleOverStrike is a style that applies to overstrike.
StyleOverStrike OVStyle
// StyleOverLine is a style that applies to overstrike underlines.
StyleOverLine OVStyle
// StyleLineNumber is a style that applies line number.
StyleLineNumber OVStyle
// StyleSearchHighlight is the style that applies to the search highlight.
StyleSearchHighlight OVStyle
// StyleColumnHighlight is the style that applies to the column highlight.
StyleColumnHighlight OVStyle
// StyleMarkLine is a style that marked line.
StyleMarkLine OVStyle
// StyleSectionLine is a style that section delimiter line.
StyleSectionLine OVStyle
// StyleMultiColorHighlight is the style that applies to the multi color highlight.
StyleMultiColorHighlight []OVStyle
// StyleColumnRainbow is the style that applies to the column rainbow color highlight.
StyleColumnRainbow []OVStyle
// StyleJumpTargetLine is the line that displays the search results.
StyleJumpTargetLine OVStyle
// General represents the general behavior.
General general
// MemoryLimit is a number that limits chunk loading.
MemoryLimit int
// MemoryLimitFile is a number that limits the chunks loading a file into memory.
MemoryLimitFile int
// BeforeWriteOriginal specifies the number of lines before the current position.
// 0 is the top of the current screen
BeforeWriteOriginal int
// AfterWriteOriginal specifies the number of lines after the current position.
// 0 specifies the bottom of the screen.
AfterWriteOriginal int
// Default keybindings. Disabled if the default keybinding is "disable".
DefaultKeyBind string
// ViewMode represents the view mode.
// ViewMode sets several settings together and can be easily switched.
ViewMode string
// Mouse support disable.
DisableMouse bool
// IsWriteOriginal is true, write the current screen on quit.
IsWriteOriginal bool
// QuitSmall Quit if the output fits on one screen.
QuitSmall bool
// CaseSensitive is case-sensitive if true.
CaseSensitive bool
// RegexpSearch is Regular expression search if true.
RegexpSearch bool
// Incsearch is incremental search if true.
Incsearch bool
// Debug represents whether to enable the debug output.
Debug bool
}
// OVStyle represents a style in addition to the original style.
type OVStyle struct {
// Background is a color name string.
Background string
// Foreground is a color name string.
Foreground string
// If true, add blink.
Blink bool
// If true, add bold.
Bold bool
// If true, add dim.
Dim bool
// If true, add italic.
Italic bool
// If true, add reverse.
Reverse bool
// If true, add underline.
Underline bool
// If true, add strike through.
StrikeThrough bool
// If true, add overline (not yet supported).
OverLine bool
// If true, sub blink.
UnBlink bool
// If true, sub bold.
UnBold bool
// If true, sub dim.
UnDim bool
// If true, sub italic.
UnItalic bool
// If true, sub reverse.
UnReverse bool
// If true, sub underline.
UnUnderline bool
// If true, sub strike through.
UnStrikeThrough bool
// if true, sub underline (not yet supported).
UnOverLine bool
}
var (
// MemoryLimit is a number that limits the chunks to load into memory.
MemoryLimit int
// MemoryLimitFile is a number that limits the chunks loading a file into memory.
MemoryLimitFile int
// OverStrikeStyle represents the overstrike style.
OverStrikeStyle tcell.Style
// OverLineStyle represents the overline underline style.
OverLineStyle tcell.Style
)
// ov output destination.
var (
// Redirect to standard output.
// echo "t" | ov> out
STDOUTPIPE *os.File
// Redirects the error output of ov --exec.
// ov --exec -- command 2> out
STDERRPIPE *os.File
)
// ScreenMode represents the state of the screen.
type ScreenMode int
const (
// Docs is a normal document screen mode.
Docs ScreenMode = iota
// Help is Help screen mode.
Help
// LogDoc is Error screen mode.
LogDoc
)
// MouseFlags represents which events of the mouse should be captured.
// Set the mode to MouseDragEvents when the mouse is enabled in oviewer.
// Does not track mouse movements except when dragging.
const MouseFlags = tcell.MouseDragEvents
// MaxWriteLog is the maximum number of lines to output to the log
// when the debug flag is enabled.
const MaxWriteLog int = 10
var (
// ErrOutOfRange indicates that value is out of range.
ErrOutOfRange = errors.New("out of range")
// ErrNotInMemory indicates that value is not in memory.
ErrNotInMemory = errors.New("not in memory")
// ErrFatalCache indicates that the cache value had a fatal error.
ErrFatalCache = errors.New("fatal error in cache value")
// ErrMissingFile indicates that the file does not exist.
ErrMissingFile = errors.New("missing filename")
// ErrIsDirectory indicates that specify a directory instead of a file.
ErrIsDirectory = errors.New("is a directory")
// ErrNotFound indicates not found.
ErrNotFound = errors.New("not found")
// ErrCancel indicates cancel.
ErrCancel = errors.New("cancel")
// ErrInvalidNumber indicates an invalid number.
ErrInvalidNumber = errors.New("invalid number")
// ErrFailedKeyBind indicates keybinding failed.
ErrFailedKeyBind = errors.New("failed to set keybind")
// ErrSignalCatch indicates that the signal has been caught.
ErrSignalCatch = errors.New("signal catch")
// ErrAlreadyClose indicates that it is already closed.
ErrAlreadyClose = errors.New("already closed")
// ErrNoColumn indicates that cursor specified a nonexistent column.
ErrNoColumn = errors.New("no column")
// ErrNoDelimiter indicates that the line containing the delimiter could not be found.
ErrNoDelimiter = errors.New("no delimiter")
// ErrOutOfChunk indicates that the specified Chunk is out of range.
ErrOutOfChunk = errors.New("out of chunk")
// ErrNotLoaded indicates that it cannot be loaded.
ErrNotLoaded = errors.New("not loaded")
// ErrEOFreached indicates that EOF has been reached.
ErrEOFreached = errors.New("EOF reached")
// ErrPreventReload indicates that reload is prevented.
ErrPreventReload = errors.New("prevent reload")
// ErrOverChunkLimit indicates that the chunk limit has been exceeded.
ErrOverChunkLimit = errors.New("over chunk limit")
// ErrAlreadyLoaded indicates that the chunk already loaded.
ErrAlreadyLoaded = errors.New("chunk already loaded")
// ErrEvictedMemory indicates that it has been evicted from memory.
ErrEvictedMemory = errors.New("evicted memory")
)
// This is a function of tcell.NewScreen but can be replaced with mock.
var tcellNewScreen = tcell.NewScreen
// NewOviewer return the structure of oviewer.
// NewOviewer requires one or more documents.
func NewOviewer(docs ...*Document) (*Root, error) {
if len(docs) == 0 {
return nil, ErrNotFound
}
root := &Root{
minStartX: -10,
}
root.Config = NewConfig()
root.keyConfig = cbind.NewConfiguration()
root.inputKeyConfig = cbind.NewConfiguration()
root.DocList = append(root.DocList, docs...)
root.Doc = root.DocList[0]
root.input = NewInput()
root.screenMode = Docs
screen, err := tcellNewScreen()
if err != nil {
return nil, err
}
if err := screen.Init(); err != nil {
return nil, fmt.Errorf("Screen.Init(): %w", err)
}
root.Screen = screen
logDoc, err := NewLogDoc()
if err != nil {
return nil, err
}
root.logDoc = logDoc
return root, nil
}
// NewRoot returns the structure of the oviewer.
// NewRoot is a simplified version that can be used externally.
func NewRoot(r io.Reader) (*Root, error) {
m, err := NewDocument()
if err != nil {
return nil, err
}
if err := m.ControlReader(r, nil); err != nil {
return nil, err
}
return NewOviewer(m)
}
// Open reads the file named of the argument and return the structure of oviewer.
// If there is no file name, create Root from standard input.
// If there is only one file name, create Root from that file,
// but return an error if the open is an error.
// If there is more than one file name, create Root from multiple files.
func Open(fileNames ...string) (*Root, error) {
switch len(fileNames) {
case 0:
return openSTDIN()
case 1:
return openFile(fileNames[0])
default:
return openFiles(fileNames)
}
}
// openSTDIN creates root with standard input.
func openSTDIN() (*Root, error) {
m, err := STDINDocument()
if err != nil {
return nil, err
}
return NewOviewer(m)
}
// openFile creates root in one file.
// If there is only one file, an error will occur if the file fails to open.
func openFile(fileName string) (*Root, error) {
m, err := OpenDocument(fileName)
if err != nil {
return nil, err
}
return NewOviewer(m)
}
// openFiles opens multiple files and creates root.
// It will continue even if there are files that fail to open.
func openFiles(fileNames []string) (*Root, error) {
errors := make([]string, 0)
docList := make([]*Document, 0)
for _, fileName := range fileNames {
m, err := OpenDocument(fileName)
if err != nil {
errors = append(errors, fmt.Sprintf("open error: %s", err))
continue
}
docList = append(docList, m)
}
if len(docList) == 0 {
return nil, fmt.Errorf("%w: %s", ErrMissingFile, fileNames[0])
}
root, err := NewOviewer(docList...)
if err != nil {
return nil, err
}
for _, e := range errors {
log.Println(e)
}
return root, err
}
// SetConfig sets config.
func (root *Root) SetConfig(config Config) {
viewMode, overwrite := config.Mode[config.ViewMode]
if overwrite {
config.General = mergeGeneral(config.General, viewMode)
}
root.Config = config
}
// SetWatcher sets file monitoring.
func (root *Root) SetWatcher(watcher *fsnotify.Watcher) {
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
root.mu.Lock()
for _, doc := range root.DocList {
if doc.filepath == event.Name {
switch event.Op {
case fsnotify.Write:
select {
case doc.ctlCh <- controlSpecifier{request: requestFollow}:
root.debugMessage(fmt.Sprintf("notify send %v", event))
default:
root.debugMessage(fmt.Sprintf("notify send fail %s", requestFollow))
}
case fsnotify.Remove, fsnotify.Create:
if !doc.FollowName {
continue
}
select {
case doc.ctlCh <- controlSpecifier{request: requestReload}:
root.debugMessage(fmt.Sprintf("notify send %v", event))
default:
root.debugMessage(fmt.Sprintf("notify send fail %s", requestReload))
}
}
}
}
root.mu.Unlock()
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
for _, doc := range root.DocList {
fileName, err := filepath.Abs(doc.FileName)
if err != nil {
log.Println(err)
continue
}
doc.filepath = fileName
path := filepath.Dir(fileName)
if err := watcher.Add(path); err != nil {
root.debugMessage(fmt.Sprintf("watcher %s:%s", doc.FileName, err))
}
}
}
// setKeyConfig sets key bindings.
func (root *Root) setKeyConfig() (map[string][]string, error) {
keyBind := GetKeyBinds(root.Config)
if err := root.setHandlers(keyBind); err != nil {
return nil, err
}
keys, ok := keyBind[actionCancel]
if !ok {
log.Printf("no cancel key")
} else {
root.cancelKeys = keys
}
return keyBind, nil
}
// SetKeyHandler assigns a new key handler.
func (root *Root) SetKeyHandler(name string, keys []string, handler func()) error {
return setHandler(root.keyConfig, name, keys, handler)
}
// Run starts the terminal pager.
func (root *Root) Run() error {
defer root.Close()
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to create watcher: %w", err)
}
defer watcher.Close()
root.SetWatcher(watcher)
// Do not set the key bindings in NewOviewer
// because it is done after loading the config.
keyBind, err := root.setKeyConfig()
if err != nil {
return err
}
help, err := NewHelp(keyBind)
if err != nil {
return err
}
root.helpDoc = help
if !root.Config.DisableMouse {
root.Screen.EnableMouse(MouseFlags)
}
root.optimizedMan()
root.setModeConfig()
for n, doc := range root.DocList {
doc.general = root.Config.General
doc.regexpCompile()
if doc.FollowName {
doc.FollowMode = true
}
if doc.ColumnWidth {
doc.ColumnMode = true
}
w := ""
if doc.general.WatchInterval > 0 {
doc.watchMode()
w = "(watch)"
}
log.Printf("open [%d]%s%s", n, doc.FileName, w)
}
root.ViewSync()
// Exit if fits on screen
if root.QuitSmall && root.docSmall() {
root.IsWriteOriginal = true
return nil
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT)
sigSuspend := registerSIGTSTP()
quitChan := make(chan struct{})
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
// Undo screen when goroutine panic.
defer func() {
root.Close()
}()
root.eventLoop(ctx, quitChan)
}()
for {
select {
case <-quitChan:
return nil
case <-sigSuspend:
root.Suspend()
case sig := <-sigs:
return fmt.Errorf("%w [%s]", ErrSignalCatch, sig)
}
}
}
// optimizedMan optimizes execution with the Man command.
func (root *Root) optimizedMan() {
// Call from man command.
manPN := os.Getenv("MAN_PN")
if len(manPN) == 0 {
return
}
root.Doc.Caption = manPN
}
// setModeConfig sets mode config.
func (root *Root) setModeConfig() {
list := make([]string, 0, len(root.Config.Mode)+1)
list = append(list, "general")
for name := range root.Config.Mode {
list = append(list, name)
}
root.input.ModeCandidate.list = list
}
// Close closes the oviewer.
func (root *Root) Close() {
root.Screen.Fini()
}
// setMessage displays a message in status.
func (root *Root) setMessage(msg string) {
if root.message == msg {
return
}
root.message = msg
root.debugMessage(msg)
root.drawStatus()
root.Show()
}
func (root *Root) setMessagef(format string, a ...any) {
msg := fmt.Sprintf(format, a...)
root.setMessage(msg)
}
func (root *Root) debugMessage(msg string) {
if !root.Debug {
return
}
if root.Doc == root.logDoc {
return
}
root.message = msg
if len(msg) == 0 {
return
}
log.Printf("%s:%s", root.Doc.FileName, msg)
}
// ToTcellStyle convert from ovStyle to tcell style.
func ToTcellStyle(s OVStyle) tcell.Style {
style := tcell.StyleDefault
style = style.Background(tcell.GetColor(s.Background))
style = style.Foreground(tcell.GetColor(s.Foreground))
style = style.Blink(s.Blink)
style = style.Bold(s.Bold)
style = style.Dim(s.Dim)
style = style.Italic(s.Italic)
style = style.Reverse(s.Reverse)
style = style.Underline(s.Underline)
style = style.StrikeThrough(s.StrikeThrough)
return style
}
func applyStyle(style tcell.Style, s OVStyle) tcell.Style {
if s.Background != "" {
style = style.Background(tcell.GetColor(s.Background))
}
if s.Foreground != "" {
style = style.Foreground(tcell.GetColor(s.Foreground))
}
if s.Blink {
style = style.Blink(true)
}
if s.Bold {
style = style.Bold(true)
}
if s.Dim {
style = style.Dim(true)
}
if s.Italic {
style = style.Italic(true)
}
if s.Reverse {
style = style.Reverse(true)
}
if s.Underline {
style = style.Underline(true)
}
if s.StrikeThrough {
style = style.StrikeThrough(true)
}
if s.UnBlink {
style = style.Blink(false)
}
if s.UnBold {
style = style.Bold(false)
}
if s.UnDim {
style = style.Dim(false)
}
if s.UnItalic {
style = style.Italic(false)
}
if s.UnReverse {
style = style.Reverse(false)
}
if s.UnUnderline {
style = style.Underline(false)
}
if s.UnStrikeThrough {
style = style.StrikeThrough(false)
}
return style
}
// mergeGeneral overwrites a general structure with a struct.
func mergeGeneral(src general, dst general) general {
if dst.TabWidth != 0 {
src.TabWidth = dst.TabWidth
}
if dst.Header != 0 {
src.Header = dst.Header
}
if dst.SkipLines != 0 {
src.SkipLines = dst.SkipLines
}
if dst.AlternateRows {
src.AlternateRows = dst.AlternateRows
}
if dst.ColumnMode {
src.ColumnMode = dst.ColumnMode
}
if dst.ColumnWidth {
src.ColumnWidth = dst.ColumnWidth
}
if dst.ColumnRainbow {
src.ColumnRainbow = dst.ColumnRainbow
}
if dst.LineNumMode {
src.LineNumMode = dst.LineNumMode
}
if !dst.WrapMode { // Because wrap mode defaults to true.
src.WrapMode = dst.WrapMode
}
if dst.FollowMode {
src.FollowMode = dst.FollowMode
}
if dst.FollowAll {
src.FollowAll = dst.FollowAll
}
if dst.FollowSection {
src.FollowSection = dst.FollowSection
}
if dst.FollowName {
src.FollowName = dst.FollowName
}
if dst.ColumnDelimiter != "" {
src.ColumnDelimiter = dst.ColumnDelimiter
}
if dst.WatchInterval != 0 {
src.WatchInterval = dst.WatchInterval
}
if dst.MarkStyleWidth != 0 {
src.MarkStyleWidth = dst.MarkStyleWidth
}
if dst.SectionDelimiter != "" {
src.SectionDelimiter = dst.SectionDelimiter
}
if dst.SectionStartPosition != 0 {
src.SectionStartPosition = dst.SectionStartPosition
}
if dst.JumpTargetString != "" {
src.JumpTargetString = dst.JumpTargetString
}
if dst.JumpTarget != 0 {
src.JumpTarget = dst.JumpTarget
}
if dst.MultiColorWords != nil {
src.MultiColorWords = dst.MultiColorWords
}
return src
}
// prepareView prepares when the screen size is changed.
func (root *Root) prepareView() {
screen := root.Screen
root.scr.vWidth, root.scr.vHeight = screen.Size()
// Do not allow size 0.
root.scr.vWidth = max(root.scr.vWidth, 1)
root.scr.vHeight = max(root.scr.vHeight, 1)
root.scr.numbers = make([]LineNumber, root.scr.vHeight+1)
root.statusPos = root.scr.vHeight - statusLine
}
// docSmall returns with bool whether the file to display fits on the screen.
func (root *Root) docSmall() bool {
if len(root.DocList) > 1 {
return false
}
root.prepareView()
m := root.Doc
if !m.BufEOF() {
return false
}
height := 0
for y := 0; y < m.BufEndNum(); y++ {
lc, err := m.contents(y, root.Doc.TabWidth)
if err != nil {
log.Printf("docSmall %d: %s", y, err)
continue
}
height += 1 + (len(lc) / root.scr.vWidth)
if height > root.scr.vHeight {
return false
}
}
return true
}
// WriteOriginal writes to the original terminal.
func (root *Root) WriteOriginal() {
m := root.Doc
if m.bottomLN == 0 {
m.bottomLN = m.BufEndNum()
}
start := max(0, m.topLN-root.BeforeWriteOriginal)
end := m.bottomLN - 1
if root.AfterWriteOriginal != 0 {
end = m.topLN + root.AfterWriteOriginal - 1
}
m.Export(os.Stdout, start, end)
}
// WriteLog write to the log terminal.
func (root *Root) WriteLog() {
m := root.logDoc
start := max(0, m.BufEndNum()-MaxWriteLog)
end := m.BufEndNum()
m.Export(os.Stdout, start, end)
}
// lineNumber returns the line information from y on the screen.
func (scr SCR) lineNumber(y int) LineNumber {
if y >= 0 && y <= len(scr.numbers) {
return scr.numbers[y]
}
return scr.numbers[0]
}
func (root *Root) debugNumOfChunk() {
if !root.Debug {
return
}
log.Println("MemoryLimit:", root.MemoryLimit)
log.Println("MemoryLimitFile:", root.MemoryLimitFile)
for _, doc := range root.DocList {
if !doc.seekable {
if MemoryLimit > 0 {
log.Printf("%s: The number of chunks is %d, of which %d(%v) are loaded", doc.FileName, len(doc.chunks), doc.loadedChunks.Len(), doc.loadedChunks.Keys())
}
continue
}
for n, chunk := range doc.chunks {
if n != 0 && len(chunk.lines) != 0 {
if !doc.loadedChunks.Contains(n) {
log.Printf("chunk %d is not under control %d", n, len(chunk.lines))
}
}
}
log.Printf("%s(seekable): The number of chunks is %d, of which %d(%v) are loaded", doc.FileName, len(doc.chunks), doc.loadedChunks.Len(), doc.loadedChunks.Keys())
}
}