Skip to content

Commit

Permalink
Merge pull request #600 from noborus/insert-filter
Browse files Browse the repository at this point in the history
Change the order of inserting filter documents
  • Loading branch information
noborus authored Jul 19, 2024
2 parents 3e33eb2 + ad86d37 commit 97f8a5f
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 89 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ require (
github.com/jwalton/gchalk v1.3.0
github.com/klauspost/compress v1.17.9
github.com/mattn/go-runewidth v0.0.15
github.com/noborus/guesswidth v0.3.4
github.com/noborus/guesswidth v0.4.0
github.com/pierrec/lz4/v4 v4.1.21
github.com/rivo/uniseg v0.4.7
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/ulikunitz/xz v0.5.12
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7
golang.org/x/sync v0.7.0
golang.org/x/term v0.21.0
golang.org/x/term v0.22.0
)

require (
Expand All @@ -40,7 +40,7 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/noborus/guesswidth v0.3.4 h1:+iKmbm0iFTS3pksIOKQQvLVZVOKNZHavqJoFK2mPoTQ=
github.com/noborus/guesswidth v0.3.4/go.mod h1:2F1sqiazKIwuSRjQTweQHPFJcjV5375jYUrTik9/V5k=
github.com/noborus/guesswidth v0.4.0 h1:+PPh+Z+GM4mKmVrhYR4lpjeyBuLMSVo2arM+VErdHIc=
github.com/noborus/guesswidth v0.4.0/go.mod h1:ghA6uh9RcK+uSmaDDmBMj/tRZ3BSpspDP6DMF5Xk3bc=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
Expand Down Expand Up @@ -92,8 +92,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7 h1:wDLEX9a7YQoKdKNQt88rtydkqDxeGaBUTnIYc3iG/mA=
golang.org/x/exp v0.0.0-20240716175740-e3f259677ff7/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -115,16 +115,16 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
12 changes: 0 additions & 12 deletions oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,6 @@ func TestRoot_toggleColumnMode(t *testing.T) {
}

func TestRoot_toggleMouse(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type fields struct {
disableMouse bool
}
Expand Down Expand Up @@ -1229,10 +1225,6 @@ func TestRoot_setWriteBA(t *testing.T) {
}

func TestRoot_modeConfig(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type args struct {
modeName string
}
Expand Down Expand Up @@ -1290,10 +1282,6 @@ func TestRoot_modeConfig(t *testing.T) {
}

func TestRoot_suspend(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
root := rootHelper(t)
shell := os.Getenv("SHELL")
if got := root.shellSuspendResume(shell); got != nil {
Expand Down
74 changes: 45 additions & 29 deletions oviewer/doclist.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ func (root *Root) hasDocChanged() bool {
}

// addDocument adds a document and displays it.
func (root *Root) addDocument(ctx context.Context, m *Document) {
root.setMessageLogf("add %s", m.FileName)
m.general = root.Config.General
m.regexpCompile()
func (root *Root) addDocument(ctx context.Context, addDoc *Document) {
root.mu.Lock()
root.DocList = append(root.DocList, addDoc)
root.mu.Unlock()

root.setDocumentNum(ctx, len(root.DocList)-1)
root.setMessageLogf("add %s%s", addDoc.FileName, addDoc.Caption)
}

// insertDocument inserts a document after the specified number and displays it.
func (root *Root) insertDocument(ctx context.Context, num int, m *Document) {
root.mu.Lock()
defer root.mu.Unlock()
root.DocList = append(root.DocList, m)
root.CurrentDoc = len(root.DocList) - 1
root.showDocNum = true
num = max(0, num)
num = min(len(root.DocList)-1, num)
root.DocList = append(root.DocList[:num+1], append([]*Document{m}, root.DocList[num+1:]...)...)
root.mu.Unlock()

root.setDocument(ctx, m)
root.setDocumentNum(ctx, num+1)
root.setMessageLogf("insert %s%s", m.FileName, m.Caption)
}

// closeDocument closes the document.
Expand All @@ -58,37 +65,43 @@ func (root *Root) closeDocument(ctx context.Context) {
return
}

root.setMessageLogf("close [%d]%s", root.CurrentDoc, root.Doc.FileName)
root.setMessageLogf("close [%d]%s%s", root.CurrentDoc, root.Doc.FileName, root.Doc.Caption)

root.mu.Lock()
defer root.mu.Unlock()
root.DocList[root.CurrentDoc].requestClose()
root.DocList = append(root.DocList[:root.CurrentDoc], root.DocList[root.CurrentDoc+1:]...)
if root.CurrentDoc > 0 {
root.CurrentDoc--
num := root.CurrentDoc
root.DocList[num].requestClose()
root.DocList = append(root.DocList[:num], root.DocList[num+1:]...)
if num > 0 {
num--
}
doc := root.DocList[root.CurrentDoc]
root.setDocument(ctx, doc)
root.mu.Unlock()

root.setDocumentNum(ctx, num)
}

// closeAllDocument closes all documents of the specified type.
func (root *Root) closeAllDocument(ctx context.Context, dType documentType) {
// closeAllDocumentsOfType closes all documents of the specified type.
func (root *Root) closeAllDocumentsOfType(dType documentType) (int, []string) {
root.mu.Lock()
for i := len(root.DocList) - 1; i >= 0; i-- {
defer root.mu.Unlock()

docLen := len(root.DocList)
docNum := root.CurrentDoc
closed := make([]string, 0, docLen)
for i := docLen - 1; i >= 0; i-- {
if len(root.DocList) <= 1 {
break
}
doc := root.DocList[i]
if doc.documentType == dType {
doc.requestClose()
root.DocList = append(root.DocList[:i], root.DocList[i+1:]...)
root.setMessageLogf("close %s", doc.FileName)
closed = append(closed, doc.FileName+doc.Caption)
if docNum == i {
docNum--
}
}
}
if root.CurrentDoc >= len(root.DocList) {
root.CurrentDoc = len(root.DocList) - 1
}
doc := root.DocList[root.CurrentDoc]
root.mu.Unlock()
root.setDocument(ctx, doc)
return docNum, closed
}

// nextDoc displays the next document.
Expand All @@ -112,6 +125,11 @@ func (root *Root) previousDoc(ctx context.Context) {
if fromDoc.parent != toDoc {
return
}
root.linkLineNum(fromDoc, toDoc)
}

// linkLineNum links the line number of the parent document.
func (root *Root) linkLineNum(fromDoc, toDoc *Document) {
if fromDoc.lineNumMap == nil {
return
}
Expand All @@ -135,7 +153,6 @@ func (root *Root) setDocumentNum(ctx context.Context, docNum int) {

root.mu.Lock()
defer root.mu.Unlock()

root.CurrentDoc = docNum
m := root.DocList[root.CurrentDoc]
root.setDocument(ctx, m)
Expand Down Expand Up @@ -169,7 +186,6 @@ func (root *Root) logDisplay(ctx context.Context) {
func (root *Root) toNormal(ctx context.Context) {
root.mu.RLock()
defer root.mu.RUnlock()

m := root.DocList[root.CurrentDoc]
root.setDocument(ctx, m)
}
149 changes: 149 additions & 0 deletions oviewer/doclist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oviewer
import (
"context"
"path/filepath"
"reflect"
"testing"

"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -119,3 +120,151 @@ func TestRoot_SwitchingDoc(t *testing.T) {
t.Errorf("Root.switchDocument() = %v, want %v", root.Doc.FileName, fileName1)
}
}

func TestRoot_addDocument(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type fields struct {
fileNames []string
}
type args struct {
fileName string
}
tests := []struct {
name string
fields fields
args args
want int
}{
{
name: "addDocument",
fields: fields{
fileNames: []string{filepath.Join(testdata, "test.txt")},
},
args: args{
fileName: filepath.Join(testdata, "test2.txt"),
},
want: 2,
},
}
{
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootFileReadHelper(t, tt.fields.fileNames...)
ctx := context.Background()
addDoc, err := OpenDocument(tt.args.fileName)
if err != nil {
t.Fatal(err)
}
root.addDocument(ctx, addDoc)
if got := root.DocumentLen(); got != tt.want {
t.Errorf("Root.addDocument() = %v, want %v", got, tt.want)
}
root.closeDocument(ctx)
if got := root.DocumentLen(); got != tt.want-1 {
t.Errorf("Root.closeDocument() = %v, want %v", got, tt.want-1)
}
})
}
}
}

func TestRoot_insertDocument(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type fields struct {
fileNames []string
}
type args struct {
num int
}
tests := []struct {
name string
fields fields
args args
want int
}{
{
name: "insertDocument",
fields: fields{
fileNames: []string{filepath.Join(testdata, "test.txt")},
},
args: args{
num: 0,
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootFileReadHelper(t, tt.fields.fileNames...)
ctx := context.Background()
m, err := NewDocument()
if err != nil {
t.Fatal(err)
}
m.documentType = DocFilter
root.insertDocument(ctx, tt.args.num, m)
if got := root.DocumentLen(); got != tt.want {
t.Errorf("Root.insertDocument() = %v, want %v", got, tt.want)
}
})
}
}

func TestRoot_closeAllDocumentsOfType(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type fields struct {
fileNames []string
count int
}
tests := []struct {
name string
fields fields
want int
want1 []string
}{
{
name: "closeAllDocumentsOfTypeNon",
fields: fields{
fileNames: []string{filepath.Join(testdata, "test.txt")},
count: 0,
},
want: 0,
want1: []string{},
},
{
name: "closeAllDocumentsOfType1",
fields: fields{
fileNames: []string{filepath.Join(testdata, "test.txt")},
count: 1,
},
want: 0,
want1: []string{"filter:test"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootFileReadHelper(t, tt.fields.fileNames...)
searcher := NewSearcher("test", nil, false, false)
ctx := context.Background()
for i := 0; i < tt.fields.count; i++ {
root.filterDocument(ctx, searcher)
}
got, got1 := root.closeAllDocumentsOfType(DocFilter)
if got != tt.want {
t.Errorf("Root.closeAllDocumentsOfType() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("Root.closeAllDocumentsOfType() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
Loading

0 comments on commit 97f8a5f

Please sign in to comment.