Skip to content

Commit e4ba199

Browse files
authored
Merge pull request #830 from noborus/improve-package-usage
Improve package usage
2 parents 7fe1026 + bb43d6d commit e4ba199

File tree

8 files changed

+368
-87
lines changed

8 files changed

+368
-87
lines changed

examples/header/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"github.com/noborus/ov/oviewer"
5+
)
6+
7+
func main() {
8+
ov, err := oviewer.Open("main.go")
9+
if err != nil {
10+
panic(err)
11+
}
12+
// Set header lines before Run().
13+
// Options must be set before Run(); do not use Config directly.
14+
ov.Config.General.SetHeader(1)
15+
ov.Config.General.SetStatusLine(false)
16+
ov.SetConfig(ov.Config)
17+
18+
if err := ov.Run(); err != nil {
19+
panic(err)
20+
}
21+
}

examples/search/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@ func main() {
1414
if err != nil {
1515
log.Fatal(err)
1616
}
17-
17+
// All Documents.
18+
/*
19+
ov.Config.General.Style = oviewer.StyleConfig{
20+
SearchHighlight: &oviewer.OVStyle{
21+
Foreground: "gold",
22+
Reverse: true,
23+
Blink: true,
24+
},
25+
}
26+
ov.SetConfig(ov.Config)
27+
*/
28+
// Only this Document.
29+
ov.Doc.General.Style = oviewer.StyleConfig{
30+
SearchHighlight: &oviewer.OVStyle{
31+
Foreground: "gold",
32+
Reverse: true,
33+
Blink: true,
34+
},
35+
}
1836
var wg sync.WaitGroup
1937

2038
wg.Add(1)
@@ -25,11 +43,6 @@ func main() {
2543
}
2644
}()
2745
time.Sleep(time.Second * 1)
28-
ov.Doc.Style.SearchHighlight = oviewer.OVStyle{
29-
Foreground: "gold",
30-
Reverse: true,
31-
Blink: true,
32-
}
3346
ov.MoveBottom()
3447
ov.BackSearch("main")
3548

examples/tail/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ func main() {
2727
if err != nil {
2828
log.Fatal(err)
2929
}
30-
// Set in general as individual modes will be overwritten.
31-
ov.Doc.FollowMode = true
32-
ov.Doc.MultiColorWords = []string{"error:", "info:", "warn:", "debug:"}
30+
ov.Doc.General.SetMultiColorWords([]string{"error:", "info:", "warn:", "debug:"})
31+
ov.Doc.General.SetFollowMode(true)
3332
if err := ov.Run(); err != nil {
3433
log.Fatal(err)
3534
}

oviewer/config.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -88,84 +88,6 @@ type Config struct {
8888
deprecatedStyleConfig `yaml:",inline" mapstructure:",squash"`
8989
}
9090

91-
// General is the general configuration.
92-
type General struct {
93-
// Converter is the converter name.
94-
Converter *string
95-
// Align is the alignment.
96-
Align *bool
97-
// Raw is the raw setting.
98-
Raw *bool
99-
// Caption is an additional caption to display after the file name.
100-
Caption *string
101-
// ColumnDelimiter is a column delimiter.
102-
ColumnDelimiter *string
103-
// SectionDelimiter is a section delimiter.
104-
SectionDelimiter *string
105-
// Specified string for jumpTarget.
106-
JumpTarget *string
107-
// MultiColorWords specifies words to color separated by spaces.
108-
MultiColorWords *[]string
109-
110-
// TabWidth is tab stop num.
111-
TabWidth *int
112-
// Header is number of header lines to be fixed.
113-
Header *int
114-
// VerticalHeader is the number of vertical header lines.
115-
VerticalHeader *int
116-
// HeaderColumn is the number of columns from the left to be fixed.
117-
// If 0 is specified, no columns are fixed.
118-
HeaderColumn *int
119-
// SkipLines is the rows to skip.
120-
SkipLines *int
121-
// WatchInterval is the watch interval (seconds).
122-
WatchInterval *int
123-
// MarkStyleWidth is width to apply the style of the marked line.
124-
MarkStyleWidth *int
125-
// SectionStartPosition is a section start position.
126-
SectionStartPosition *int
127-
// SectionHeaderNum is the number of lines in the section header.
128-
SectionHeaderNum *int
129-
// HScrollWidth is the horizontal scroll width.
130-
HScrollWidth *string
131-
// HScrollWidthNum is the horizontal scroll width.
132-
HScrollWidthNum *int
133-
// RulerType is the ruler type (0: none, 1: relative, 2: absolute).
134-
RulerType *RulerType
135-
// AlternateRows alternately style rows.
136-
AlternateRows *bool
137-
// ColumnMode is column mode.
138-
ColumnMode *bool
139-
// ColumnWidth is column width mode.
140-
ColumnWidth *bool
141-
// ColumnRainbow is column rainbow.
142-
ColumnRainbow *bool
143-
// LineNumMode displays line numbers.
144-
LineNumMode *bool
145-
// Wrap is Wrap mode.
146-
WrapMode *bool
147-
// FollowMode is the follow mode.
148-
FollowMode *bool
149-
// FollowAll is a follow mode for all documents.
150-
FollowAll *bool
151-
// FollowSection is a follow mode that uses section instead of line.
152-
FollowSection *bool
153-
// FollowName is the mode to follow files by name.
154-
FollowName *bool
155-
// PlainMode is whether to enable the original character decoration.
156-
PlainMode *bool
157-
// SectionHeader is whether to display the section header.
158-
SectionHeader *bool
159-
// HideOtherSection is whether to hide other sections.
160-
HideOtherSection *bool
161-
// StatusLine indicates whether to hide the status line.
162-
StatusLine *bool
163-
// Prompt is the prompt configuration.
164-
Prompt PromptConfig
165-
// Style is the style setting.
166-
Style StyleConfig
167-
}
168-
16991
// PromptConfigNormal is the normal prompt setting.
17092
type PromptConfigNormal struct {
17193
// ShowFilename controls whether to display filename.

oviewer/document.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ type Document struct {
175175
reopenable bool
176176
// nonMatch indicates if non-matching lines are searched.
177177
nonMatch bool
178+
// General is the General settings.
179+
General General
178180
}
179181

180182
// store represents store management.

oviewer/example_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@ package oviewer_test
33
import (
44
"bytes"
55
"os/exec"
6+
"path/filepath"
67
"strings"
8+
"testing"
79

10+
"github.com/gdamore/tcell/v2"
811
"github.com/noborus/ov/oviewer"
912
)
1013

14+
const cwd = ".."
15+
16+
var testdata = filepath.Join(cwd, "testdata")
17+
18+
// This is a function of tcell.NewScreen but can be replaced with mock.
19+
var tcellNewScreen = tcell.NewScreen
20+
21+
// fakeScreen returns a fake screen.
22+
func fakeScreen() (tcell.Screen, error) {
23+
// width, height := 80, 25
24+
return tcell.NewSimulationScreen(""), nil
25+
}
26+
1127
func ExampleOpen() {
1228
ov, err := oviewer.Open("example_test.go")
1329
if err != nil {
@@ -78,3 +94,52 @@ func ExampleSearch() {
7894
panic(err)
7995
}
8096
}
97+
98+
func TestExample_HeaderOption(t *testing.T) {
99+
oviewer.SetTcellNewScreen(fakeScreen)
100+
defer func() {
101+
oviewer.SetTcellNewScreen(tcell.NewScreen)
102+
}()
103+
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
104+
if err != nil {
105+
t.Fatalf("Open failed: %v", err)
106+
}
107+
ov.Config.General.SetHeader(2)
108+
if ov.Config.General.Header == nil || *ov.Config.General.Header != 2 {
109+
t.Errorf("Config.General.SetHeader did not set header correctly: got %v", ov.Config.General.Header)
110+
}
111+
}
112+
113+
func TestExample_DocumentGeneral(t *testing.T) {
114+
oviewer.SetTcellNewScreen(fakeScreen)
115+
defer func() {
116+
oviewer.SetTcellNewScreen(tcell.NewScreen)
117+
}()
118+
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
119+
if err != nil {
120+
t.Fatalf("Open failed: %v", err)
121+
}
122+
ov.Doc.General.SetHeader(3)
123+
if ov.Doc.General.Header == nil || *ov.Doc.General.Header != 3 {
124+
t.Errorf("Doc.General.SetHeader did not set header correctly: got %v", ov.Doc.General.Header)
125+
}
126+
}
127+
128+
func TestExample_FollowMode(t *testing.T) {
129+
oviewer.SetTcellNewScreen(fakeScreen)
130+
defer func() {
131+
oviewer.SetTcellNewScreen(tcell.NewScreen)
132+
}()
133+
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
134+
if err != nil {
135+
t.Fatalf("Open failed: %v", err)
136+
}
137+
ov.Config.General.SetFollowMode(true)
138+
if ov.Config.General.FollowMode == nil || *ov.Config.General.FollowMode != true {
139+
t.Errorf("Options.SetFollowMode did not set follow mode correctly: got %v", ov.Config.General.FollowMode)
140+
}
141+
ov.Doc.General.SetFollowMode(false)
142+
if ov.Doc.General.FollowMode == nil || *ov.Doc.General.FollowMode != false {
143+
t.Errorf("Doc.General.SetFollowMode did not set follow mode correctly: got %v", ov.Doc.General.FollowMode)
144+
}
145+
}

0 commit comments

Comments
 (0)