Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/header/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/noborus/ov/oviewer"
)

func main() {
ov, err := oviewer.Open("main.go")
if err != nil {
panic(err)
}
// Set header lines before Run().
// Options must be set before Run(); do not use Config directly.
ov.Config.General.SetHeader(1)
ov.Config.General.SetStatusLine(false)
ov.SetConfig(ov.Config)

if err := ov.Run(); err != nil {
panic(err)
}
}
25 changes: 19 additions & 6 deletions examples/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ func main() {
if err != nil {
log.Fatal(err)
}

// All Documents.
/*
ov.Config.General.Style = oviewer.StyleConfig{
SearchHighlight: &oviewer.OVStyle{
Foreground: "gold",
Reverse: true,
Blink: true,
},
}
ov.SetConfig(ov.Config)
*/
// Only this Document.
ov.Doc.General.Style = oviewer.StyleConfig{
SearchHighlight: &oviewer.OVStyle{
Foreground: "gold",
Reverse: true,
Blink: true,
},
}
var wg sync.WaitGroup

wg.Add(1)
Expand All @@ -25,11 +43,6 @@ func main() {
}
}()
time.Sleep(time.Second * 1)
ov.Doc.Style.SearchHighlight = oviewer.OVStyle{
Foreground: "gold",
Reverse: true,
Blink: true,
}
ov.MoveBottom()
ov.BackSearch("main")

Expand Down
5 changes: 2 additions & 3 deletions examples/tail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ func main() {
if err != nil {
log.Fatal(err)
}
// Set in general as individual modes will be overwritten.
ov.Doc.FollowMode = true
ov.Doc.MultiColorWords = []string{"error:", "info:", "warn:", "debug:"}
ov.Doc.General.SetMultiColorWords([]string{"error:", "info:", "warn:", "debug:"})
ov.Doc.General.SetFollowMode(true)
if err := ov.Run(); err != nil {
log.Fatal(err)
}
Expand Down
78 changes: 0 additions & 78 deletions oviewer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,84 +88,6 @@ type Config struct {
deprecatedStyleConfig `yaml:",inline" mapstructure:",squash"`
}

// General is the general configuration.
type General struct {
// Converter is the converter name.
Converter *string
// Align is the alignment.
Align *bool
// Raw is the raw setting.
Raw *bool
// Caption is an additional caption to display after the file name.
Caption *string
// ColumnDelimiter is a column delimiter.
ColumnDelimiter *string
// SectionDelimiter is a section delimiter.
SectionDelimiter *string
// Specified string for jumpTarget.
JumpTarget *string
// MultiColorWords specifies words to color separated by spaces.
MultiColorWords *[]string

// TabWidth is tab stop num.
TabWidth *int
// Header is number of header lines to be fixed.
Header *int
// VerticalHeader is the number of vertical header lines.
VerticalHeader *int
// HeaderColumn is the number of columns from the left to be fixed.
// If 0 is specified, no columns are fixed.
HeaderColumn *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
// SectionHeaderNum is the number of lines in the section header.
SectionHeaderNum *int
// HScrollWidth is the horizontal scroll width.
HScrollWidth *string
// HScrollWidthNum is the horizontal scroll width.
HScrollWidthNum *int
// RulerType is the ruler type (0: none, 1: relative, 2: absolute).
RulerType *RulerType
// 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
// SectionHeader is whether to display the section header.
SectionHeader *bool
// HideOtherSection is whether to hide other sections.
HideOtherSection *bool
// StatusLine indicates whether to hide the status line.
StatusLine *bool
// Prompt is the prompt configuration.
Prompt PromptConfig
// Style is the style setting.
Style StyleConfig
}

// PromptConfigNormal is the normal prompt setting.
type PromptConfigNormal struct {
// ShowFilename controls whether to display filename.
Expand Down
2 changes: 2 additions & 0 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type Document struct {
reopenable bool
// nonMatch indicates if non-matching lines are searched.
nonMatch bool
// General is the General settings.
General General
}

// store represents store management.
Expand Down
65 changes: 65 additions & 0 deletions oviewer/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@ package oviewer_test
import (
"bytes"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/gdamore/tcell/v2"
"github.com/noborus/ov/oviewer"
)

const cwd = ".."

var testdata = filepath.Join(cwd, "testdata")

// This is a function of tcell.NewScreen but can be replaced with mock.
var tcellNewScreen = tcell.NewScreen

// fakeScreen returns a fake screen.
func fakeScreen() (tcell.Screen, error) {
// width, height := 80, 25
return tcell.NewSimulationScreen(""), nil
}

func ExampleOpen() {
ov, err := oviewer.Open("example_test.go")
if err != nil {
Expand Down Expand Up @@ -78,3 +94,52 @@ func ExampleSearch() {
panic(err)
}
}

func TestExample_HeaderOption(t *testing.T) {
oviewer.SetTcellNewScreen(fakeScreen)
defer func() {
oviewer.SetTcellNewScreen(tcell.NewScreen)
}()
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
if err != nil {
t.Fatalf("Open failed: %v", err)
}
ov.Config.General.SetHeader(2)
if ov.Config.General.Header == nil || *ov.Config.General.Header != 2 {
t.Errorf("Config.General.SetHeader did not set header correctly: got %v", ov.Config.General.Header)
}
}

func TestExample_DocumentGeneral(t *testing.T) {
oviewer.SetTcellNewScreen(fakeScreen)
defer func() {
oviewer.SetTcellNewScreen(tcell.NewScreen)
}()
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
if err != nil {
t.Fatalf("Open failed: %v", err)
}
ov.Doc.General.SetHeader(3)
if ov.Doc.General.Header == nil || *ov.Doc.General.Header != 3 {
t.Errorf("Doc.General.SetHeader did not set header correctly: got %v", ov.Doc.General.Header)
}
}

func TestExample_FollowMode(t *testing.T) {
oviewer.SetTcellNewScreen(fakeScreen)
defer func() {
oviewer.SetTcellNewScreen(tcell.NewScreen)
}()
ov, err := oviewer.Open(filepath.Join(testdata, "test.txt"))
if err != nil {
t.Fatalf("Open failed: %v", err)
}
ov.Config.General.SetFollowMode(true)
if ov.Config.General.FollowMode == nil || *ov.Config.General.FollowMode != true {
t.Errorf("Options.SetFollowMode did not set follow mode correctly: got %v", ov.Config.General.FollowMode)
}
ov.Doc.General.SetFollowMode(false)
if ov.Doc.General.FollowMode == nil || *ov.Doc.General.FollowMode != false {
t.Errorf("Doc.General.SetFollowMode did not set follow mode correctly: got %v", ov.Doc.General.FollowMode)
}
}
Loading
Loading