Skip to content

Commit

Permalink
Fixed sectionLine style not applying to invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Jul 12, 2024
1 parent 97d7f8e commit fdd0f89
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
11 changes: 9 additions & 2 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,16 @@ func (root *Root) yRangeStyle(y int, s OVStyle, start int, end int) {

// sectionLineHighlight applies the style of the section line highlight.
func (root *Root) sectionLineHighlight(y int, line LineC) {
if line.section > 0 && line.sectionNm > 0 && line.sectionNm <= root.Doc.SectionHeaderNum {
root.yStyle(y, root.StyleSectionLine)
if !line.valid {
return
}
if line.section <= 0 {
return
}
if line.sectionNm <= 0 || line.sectionNm > root.Doc.SectionHeaderNum {
return
}
root.yStyle(y, root.StyleSectionLine)
}

// hideOtherSection hides other sections.
Expand Down
93 changes: 93 additions & 0 deletions oviewer/draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,96 @@ func TestRoot_section2(t *testing.T) {
})
}
}

func TestRoot_sectionLineHighlight(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
type fields struct {
fileNames []string
sectionDelimiter string
sectionHeaderNum int
}
type args struct {
y int
}
tests := []struct {
name string
fields fields
args args
wantStr rune
wantStyle bool
}{
{
name: "test-sectionLineHighlight1",
fields: fields{
fileNames: []string{filepath.Join(testdata, "section.txt")},
sectionDelimiter: "^#",
sectionHeaderNum: 1,
},
args: args{
y: 2,
},
wantStr: '#',
wantStyle: true,
},
{
name: "test-sectionLineHighlight2",
fields: fields{
fileNames: []string{filepath.Join(testdata, "section.txt")},
sectionDelimiter: "^#",
sectionHeaderNum: 1,
},
args: args{
y: 3,
},
wantStr: '2',
wantStyle: false,
},
{
name: "test-sectionLineHighlightInvalid",
fields: fields{
fileNames: []string{filepath.Join(testdata, "section.txt")},
sectionDelimiter: "^#",
sectionHeaderNum: 2,
},
args: args{
y: 20,
},
wantStr: '~',
wantStyle: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := rootFileReadHelper(t, tt.fields.fileNames...)
root.Doc.SectionHeader = true
root.setSectionDelimiter(tt.fields.sectionDelimiter)
root.Doc.SectionHeaderNum = tt.fields.sectionHeaderNum
root.prepareScreen()
ctx := context.Background()
root.ViewSync(ctx)
root.prepareDraw(ctx)
root.draw(ctx)
line, ok := root.scr.lines[tt.args.y]
if !ok {
t.Fatalf("line is not found %d", tt.args.y)
}
root.sectionLineHighlight(tt.args.y, line)
p, _, style, _ := root.Screen.GetContent(0, tt.args.y)
if p != tt.wantStr {
t.Errorf("Root.sectionLineHighlight() = %v, want %v", p, tt.wantStr)
}
if tt.wantStyle {
if style != applyStyle(tcell.StyleDefault, root.StyleSectionLine) {
t.Errorf("Root.sectionLineHighlight() = %v, want %v", style, root.StyleSectionLine)
}
} else {
if style == applyStyle(tcell.StyleDefault, root.StyleSectionLine) {
t.Errorf("Root.sectionLineHighlight() = %v, want %v", style, tcell.StyleDefault)
}
}
})
}
}

0 comments on commit fdd0f89

Please sign in to comment.