Skip to content

Commit

Permalink
Merge pull request #442 from noborus/section-header
Browse files Browse the repository at this point in the history
Added section-header
  • Loading branch information
noborus authored Sep 21, 2023
2 parents db9e8d2 + 252d510 commit 9051636
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ func init() {
rootCmd.PersistentFlags().IntP("section-start", "", 0, "section start position")
_ = viper.BindPFlag("general.SectionStartPosition", rootCmd.PersistentFlags().Lookup("section-start"))

rootCmd.PersistentFlags().BoolP("section-header", "", false, "enable section-delimiter line as Header")
_ = viper.BindPFlag("general.SectionHeader", rootCmd.PersistentFlags().Lookup("section-header"))

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

Expand Down
18 changes: 11 additions & 7 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ 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 @@ -170,14 +172,12 @@ func NewDocument() (*Document, error) {
ColumnDelimiter: "",
TabWidth: 8,
MarkStyleWidth: 1,
PlainMode: false,
},
ctlCh: make(chan controlSpecifier),
memoryLimit: 100,
seekable: true,
reopenable: true,
preventReload: false,
store: NewStore(),
ctlCh: make(chan controlSpecifier),
memoryLimit: 100,
seekable: true,
reopenable: true,
store: NewStore(),
}
if err := m.NewCache(); err != nil {
return nil, err
Expand Down Expand Up @@ -458,6 +458,10 @@ 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
38 changes: 38 additions & 0 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (root *Root) draw() {
}

lN = m.topLN + lN

// Section header
lN = root.sectionHeader(lN)

// Body
lX, lN = root.drawBody(lX, lN)

Expand Down Expand Up @@ -102,6 +106,40 @@ func (root *Root) drawHeader() int {
return lN
}

// sectionHeader draws section header.
func (root *Root) sectionHeader(lN int) int {
m := root.Doc
if !m.SectionHeader || m.SectionDelimiter == "" {
return lN
}

sectionLN, err := root.Doc.prevSection(lN)
if err != nil {
log.Println(err)
return lN
}

if m.Header > sectionLN {
return lN
}

if lN > sectionLN {
sx, sn := 0, sectionLN
line, _ := m.getLineC(sn, m.TabWidth)
nextY := sn
for y := m.headerLen; sn < sectionLN+m.sectionHeaderNum; y++ {
sx, nextY = root.drawLine(y, sx, sn, line.lc)
root.sectionLineHighlight(y, line.str)
m.headerLen += 1
if nextY != sn {
sn = nextY
line, _ = m.getLineC(sn, m.TabWidth)
}
}
}
return lN
}

// drawBody draws body.
func (root *Root) drawBody(lX int, lN int) (int, int) {
m := root.Doc
Expand Down
2 changes: 1 addition & 1 deletion oviewer/input_savebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/gdamore/tcell/v2"
// setSaveBuffer is a wrapper to move to setSaveBufferMode.
func (root *Root) setSaveBuffer() {
if root.Doc.seekable {
root.setMessage("Saving regular files is not supported")
root.setMessage("Does not support saving regular files")
return
}
root.setSaveBufferMode()
Expand Down
4 changes: 4 additions & 0 deletions oviewer/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ func (m *Document) searchGoSection(lN int, x int) {
if err != nil {
sN = 0
}
if m.SectionHeader {
sN = (sN - m.firstLine() + m.sectionHeaderNum) + m.SectionStartPosition
sN = max(sN, m.BufStartNum())
}
y := 0
if sN < m.firstLine() {
// topLN is negative if the section is less than header + skip.
Expand Down
22 changes: 7 additions & 15 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.SectionStartPosition)
m.moveLine((lN - m.firstLine() + m.sectionHeaderNum) + m.SectionStartPosition)
return nil
}

Expand All @@ -266,11 +266,7 @@ func (m *Document) nextSection(n int) (int, error) {
searcher := NewSearcher(m.SectionDelimiter, m.SectionDelimiterReg, true, true)
ctx := context.Background()
defer ctx.Done()
n, err := m.SearchLine(ctx, searcher, lN)
if err != nil {
return n, err
}
return n, nil
return m.SearchLine(ctx, searcher, lN)
}

// movePrevSection moves to the previous section.
Expand All @@ -281,12 +277,14 @@ func (m *Document) movePrevSection() error {
return nil
}

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

Expand All @@ -296,13 +294,7 @@ func (m *Document) prevSection(n int) (int, error) {
searcher := NewSearcher(m.SectionDelimiter, m.SectionDelimiterReg, true, true)
ctx := context.Background()
defer ctx.Done()
n, err := m.BackSearchLine(ctx, searcher, lN)
if err != nil {
return 0, err
}
n = (n - m.firstLine()) + m.SectionStartPosition
n = max(n, m.BufStartNum())
return n, nil
return m.BackSearchLine(ctx, searcher, lN)
}

// moveLastSection moves to the last section.
Expand Down
2 changes: 2 additions & 0 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type general struct {
FollowName bool
// PlainMode is whether to enable the original character decoration.
PlainMode bool
// SectionHeader is whether to display the section header.
SectionHeader bool
}

// OVPromptConfigNormal is the normal prompt setting.
Expand Down
3 changes: 3 additions & 0 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (root *Root) setSearcher(word string, caseSensitive bool) Searcher {
// searchMove searches forward/backward and moves to the nearest matching line.
func (root *Root) searchMove(ctx context.Context, forward bool, lN int, searcher Searcher) {
if searcher == nil {
if root.Doc.jumpTargetSection {
root.Doc.jumpTargetNum = 0
}
return
}
word := root.searcher.String()
Expand Down

0 comments on commit 9051636

Please sign in to comment.