Skip to content

Commit

Permalink
Add section-header-num option
Browse files Browse the repository at this point in the history
If the section header number is greater than 1,
the lines after the section delimiter are also treated
as section headers.

This is an implementation of #461.
  • Loading branch information
noborus committed Nov 5, 2023
1 parent 2ed4ba3 commit 93099cc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ It supports various compressed files(gzip, bzip2, zstd, lz4, and xz).
config.General.ColumnDelimiter = "\t"
}

// SectionHeader is enabled if SectionHeaderNum is greater than 0.
if config.General.SectionHeaderNum > 0 {
config.General.SectionHeader = true
}

// Set a global variable to convert to a style before opening the file.
oviewer.OverStrikeStyle = oviewer.ToTcellStyle(config.StyleOverStrike)
oviewer.OverLineStyle = oviewer.ToTcellStyle(config.StyleOverLine)
Expand Down Expand Up @@ -365,6 +370,9 @@ func init() {
rootCmd.PersistentFlags().BoolP("section-header", "", false, "enable section-delimiter line as Header")
_ = viper.BindPFlag("general.SectionHeader", rootCmd.PersistentFlags().Lookup("section-header"))

rootCmd.PersistentFlags().IntP("section-header-num", "", 1, "number of header lines")
_ = viper.BindPFlag("general.SectionHeaderNum", rootCmd.PersistentFlags().Lookup("section-header-num"))

rootCmd.PersistentFlags().BoolP("follow-mode", "f", false, "follow mode")
_ = viper.BindPFlag("general.FollowMode", rootCmd.PersistentFlags().Lookup("follow-mode"))

Expand Down
6 changes: 0 additions & 6 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ type Document struct {
// columnCursor is the number of columns.
columnCursor int

// sectionHeaderNum is the number of lines in the section header.
sectionHeaderNum int
// jumpTargetNum is the display position of search results.
jumpTargetNum int
// jumpTargetSection is the display position of search results.
Expand Down Expand Up @@ -458,10 +456,6 @@ func (m *Document) setDelimiter(delm string) {
func (m *Document) setSectionDelimiter(delm string) {
m.SectionDelimiter = delm
m.SectionDelimiterReg = regexpCompile(delm, true)
m.sectionHeaderNum = 0
if m.SectionHeader && m.SectionDelimiter != "" {
m.sectionHeaderNum = 1
}
}

// setMultiColorWords set multiple strings to highlight with multiple colors.
Expand Down
19 changes: 15 additions & 4 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (root *Root) draw() {
n := root.drawSectionHeader(lN)
if lN != n {
lN = n
m.topLN = lN
if m.topLN == 0 {
m.topLN = 1
}
}

// Body
Expand Down Expand Up @@ -110,6 +112,8 @@ func (root *Root) drawHeader() int {
}

// drawSectionHeader draws section header.
// drawSectionHeader advances the line
// if the section header contains a line in the terminal.
func (root *Root) drawSectionHeader(lN int) int {
m := root.Doc
if !m.SectionHeader || m.SectionDelimiter == "" {
Expand All @@ -134,14 +138,17 @@ func (root *Root) drawSectionHeader(lN int) int {
if pn > sectionLN {
sn = sectionLN
line, valid := m.getLineC(sn, m.TabWidth)
for y := m.headerLen; sn < sectionLN+m.sectionHeaderNum; y++ {
for y := m.headerLen; sn < sectionLN+m.SectionHeaderNum; y++ {
if root.Doc.LineNumMode {
if valid {
root.drawLineNumber(sn, y)
} else {
root.blankLineNumber(y)
}
}
if valid {
root.styleContent(lN, line)
}
sx, nextN = root.drawLine(y, sx, sn, line.lc)
root.sectionLineHighlight(y, line.str)
m.headerLen += 1
Expand All @@ -151,7 +158,7 @@ func (root *Root) drawSectionHeader(lN int) int {
}
}
}
return pn
return pn + (m.SectionHeaderNum - 1)
}

// drawBody draws body.
Expand Down Expand Up @@ -646,8 +653,12 @@ func (root *Root) sectionLineHighlight(y int, str string) {
log.Printf("Regular expression is not set: %s", root.Doc.SectionDelimiter)
return
}

if root.scr.sectionHeaderLeft > 0 {
root.yStyle(y, root.StyleSectionLine)
root.scr.sectionHeaderLeft--
}
if root.Doc.SectionDelimiterReg.MatchString(str) {
root.yStyle(y, root.StyleSectionLine)
root.scr.sectionHeaderLeft = root.Doc.SectionHeaderNum - 1
}
}
6 changes: 3 additions & 3 deletions oviewer/move_vertical.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (m *Document) moveNextSection() error {
m.movePgDn()
return ErrNoDelimiter
}
m.moveLine((lN - m.firstLine() + m.sectionHeaderNum) + m.SectionStartPosition)
m.moveLine((lN - m.firstLine() + m.SectionHeaderNum) + m.SectionStartPosition)
return nil
}

Expand All @@ -277,14 +277,14 @@ func (m *Document) movePrevSection() error {
return nil
}

lN, err := m.prevSection(m.topLN + m.firstLine() - m.sectionHeaderNum)
lN, err := m.prevSection(m.topLN + m.firstLine() - m.SectionHeaderNum)
if err != nil {
m.moveTop()
return err
}
lN = (lN - m.firstLine()) + m.SectionStartPosition
lN = max(lN, m.BufStartNum())
m.moveLine(lN + m.sectionHeaderNum)
m.moveLine(lN + m.SectionHeaderNum)
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type SCR struct {
vHeight int
// startX is the start position of x.
startX int
// Process as a section header if the remaining value is 1 or more.
sectionHeaderLeft int
}

// LineNumber is Number of logical lines and number of wrapping lines on the screen.
Expand Down Expand Up @@ -130,6 +132,8 @@ type general struct {
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.
Expand Down Expand Up @@ -929,7 +933,7 @@ func (root *Root) WriteOriginal() {
m.bottomLN = m.BufEndNum()
}

start := max(0, m.topLN-(m.sectionHeaderNum+root.BeforeWriteOriginal))
start := max(0, m.topLN-(m.SectionHeaderNum+root.BeforeWriteOriginal))
end := m.bottomLN - 1
if root.AfterWriteOriginal != 0 {
end = m.topLN + root.AfterWriteOriginal - 1
Expand Down
9 changes: 6 additions & 3 deletions oviewer/search_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ package oviewer
// If the search term is off screen, move until the search term is visible.
func (m *Document) searchGoTo(lN int, x int) {
m.searchGoX(x)

m.topLN = lN - m.firstLine()
sn := 0
if lN > m.firstLine()+m.SectionHeaderNum-1 {
sn = (m.SectionHeaderNum - 1)
}
m.topLN = lN - (m.firstLine() + sn)
m.moveYUp(m.jumpTargetNum)
}

Expand All @@ -25,7 +28,7 @@ func (m *Document) searchGoSection(lN int, x int) {
sN = 0
}
if m.SectionHeader {
sN = (sN - m.firstLine() + m.sectionHeaderNum) + m.SectionStartPosition
sN = (sN - m.firstLine() + m.SectionHeaderNum) + m.SectionStartPosition
sN = max(sN, m.BufStartNum())
}
y := 0
Expand Down

0 comments on commit 93099cc

Please sign in to comment.