Skip to content

Commit

Permalink
Added section-header
Browse files Browse the repository at this point in the history
Fixed display of the line specified by section-delimiter.
This lets users know which section they are scrolling through.
  • Loading branch information
noborus committed Sep 19, 2023
1 parent db9e8d2 commit 12148cc
Show file tree
Hide file tree
Showing 8 changed files with 64 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
33 changes: 33 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,35 @@ 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
}
log.Println(m.SectionHeader)
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)
for y := m.headerLen; sn <= sectionLN; y++ {
sx, sn = root.drawLine(y, sx, sn, line.lc)
root.sectionLineHighlight(y, line.str)
m.headerLen += 1
}
}
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 12148cc

Please sign in to comment.