Skip to content

Commit

Permalink
Added input mode for section header number
Browse files Browse the repository at this point in the history
Added section-header-num to help-key and help options.
README.md has also been updated, as it has been added to the help.
Improved comments.
  • Loading branch information
noborus committed Dec 26, 2023
1 parent 6ab515e commit a7967bc
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,14 @@ ov --section-delimiter "^#" --section-header README.md

It is also useful as a pager for `git``.

The number of lines in section-header can be changed.

You can specify the number of lines using the `--section-header-num` option or key input(default key `F7`).

```gitconfig
[pager]
diff = "ov -F --section-delimiter '^diff' --section-header"
log = "ov -F --section-delimiter '^commit' --section-header"
log = "ov -F --section-delimiter '^commit' --section-header --section-header-num 3"
```

### 3.10. <a name='follow-mode'></a>Follow mode
Expand Down Expand Up @@ -721,6 +725,7 @@ MemoryLimit: 1000
| -H, | --header int | number of header lines to be displayed constantly |
| -h, | --help | help for ov |
| | --help-key | display key bind information |
| | --hscroll-width [int\|int%\|.int] | width to scroll horizontally [int\|int%\|.int] (default "10%") |
| | --incsearch[=true\|false] | incremental search (default true) |
| -j, | --jump-target [int\|int%\|.int\|'section'] | jump target [int\|int%\|.int\|'section'] |
| -n, | --line-number | line number mode |
Expand All @@ -733,6 +738,7 @@ MemoryLimit: 1000
| | --regexp-search | regular expression search |
| | --section-delimiter regexp | regexp for section delimiter .e.g. "^#" |
| | --section-header | enable section-delimiter line as Header |
| | --section-header-num int | number of header lines (default 1) |
| | --section-start int | section start position |
| | --skip-extract | skip extracting compressed files |
| | --skip-lines int | skip the number of lines |
Expand Down Expand Up @@ -774,6 +780,8 @@ It can also be changed after startup.
| [right] | * scroll to right |
| [ctrl+left] | * scroll left half screen |
| [ctrl+right] | * scroll right half screen |
| [ctrl+shift+left] | * scroll left specified width |
| [ctrl+shift+right] | * scroll right specified width |
| [shift+Home] | * go to beginning of line |
| [shift+End] | * go to end of line |
| [g] | * go to line(input number or `.n` or `n%` allowed) |
Expand Down Expand Up @@ -815,6 +823,7 @@ It can also be changed after startup.
| [^] | * previous section |
| [9] | * last section |
| [F2] | * follow section mode toggle |
| [F7] | * section header number |
| **Close and reload** | |
| [ctrl+F9], [ctrl+alt+s] | * close file |
| [ctrl+alt+l], [F5] | * reload file |
Expand Down
18 changes: 18 additions & 0 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ func (root *Root) setSkipLines(input string) {
root.setMessagef("Set skip lines %d", num)
}

func (root *Root) setSectionNum(input string) {
num, err := strconv.Atoi(input)
if err != nil {
root.setMessagef("Set section header num: %s", ErrInvalidNumber.Error())
return
}
if num < 0 {
root.setMessagef("Set section header num: %s", ErrOutOfRange.Error())
return
}
if root.Doc.SectionHeaderNum == num {
return
}

root.Doc.SectionHeaderNum = num
root.setMessagef("Set section header num %d", num)
}

// suspend suspends the current screen display and runs the shell.
// It will return when you exit the shell.
func (root *Root) suspend() {
Expand Down
2 changes: 1 addition & 1 deletion oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (root *Root) drawSectionHeader(lN int) int {
}

pn := lN
// If the line number is 0, it is the first line.
// prevSection searches for the section above the specified line.
if pn == 0 {
pn = 1
}
Expand Down
2 changes: 2 additions & 0 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func (root *Root) eventLoop(ctx context.Context, quitChan chan<- struct{}) {
root.setJumpTarget(ev.value)
case *eventSaveBuffer:
root.saveBuffer(ev.value)
case *eventSectionNum:
root.setSectionNum(ev.value)

// tcell events
case *tcell.EventResize:
Expand Down
1 change: 1 addition & 0 deletions oviewer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
MultiColor // MultiColor is multi-word coloring.
JumpTarget // JumpTarget is the position to display the search results.
SaveBuffer // SaveBuffer is the save buffer.
SectionNum // SectionNum is the section number.
)

// Input represents the status of various inputs.
Expand Down
61 changes: 61 additions & 0 deletions oviewer/input_section_num.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package oviewer

import (
"strconv"

"github.com/gdamore/tcell/v2"
)

// setSectionNumMode sets the inputMode to SectionNum.
func (root *Root) setSectionNumMode() {
input := root.input
input.value = ""
input.cursorX = 0
input.Event = newSectionNumEvent()
}

// eventSectionNum represents the section num input mode.
type eventSectionNum struct {
tcell.EventTime
value string
}

// newSectionNumEvent returns Event.
func newSectionNumEvent() *eventSectionNum {
return &eventSectionNum{}
}

// Mode returns InputMode.
func (e *eventSectionNum) Mode() InputMode {
return SectionNum
}

// Prompt returns the prompt string in the input field.
func (e *eventSectionNum) Prompt() string {
return "Section Num:"
}

// Confirm returns the event when the input is confirmed.
func (e *eventSectionNum) Confirm(str string) tcell.Event {
e.value = str
e.SetEventNow()
return e
}

// Up returns strings when the up key is pressed during input.
func (e *eventSectionNum) Up(str string) string {
n, err := strconv.Atoi(str)
if err != nil {
return "0"
}
return strconv.Itoa(n + 1)
}

// Down returns strings when the down key is pressed during input.
func (e *eventSectionNum) Down(str string) string {
n, err := strconv.Atoi(str)
if err != nil || n <= 0 {
return "0"
}
return strconv.Itoa(n - 1)
}
2 changes: 1 addition & 1 deletion oviewer/input_skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (root *Root) setSkipLinesMode() {
input.Event = newSkipLinesEvent()
}

// eventSkipLines represents the goto input mode.
// eventSkipLines represents the skip lines input mode.
type eventSkipLines struct {
tcell.EventTime
value string
Expand Down
2 changes: 1 addition & 1 deletion oviewer/input_viewmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func viewModeCandidate() *candidate {
}
}

// eventViewMode represents the mode input mode.
// eventViewMode represents the view mode input mode.
type eventViewMode struct {
tcell.EventTime
clist *candidate
Expand Down
4 changes: 4 additions & 0 deletions oviewer/keybind.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
actionSkipLines = "skip_lines"
actionTabWidth = "tabwidth"
actionGoLine = "goto"
actionSectionNum = "section_num"
actionNextSearch = "next_search"
actionNextBackSearch = "next_backsearch"
actionNextDoc = "next_doc"
Expand Down Expand Up @@ -147,6 +148,7 @@ func (root *Root) handlers() map[string]func() {
actionSkipLines: root.setSkipLinesMode,
actionTabWidth: root.setTabWidthMode,
actionGoLine: root.setGoLineMode,
actionSectionNum: root.setSectionNumMode,
actionNextSearch: root.sendNextSearch,
actionNextBackSearch: root.sendNextBackSearch,
actionNextDoc: root.nextDoc,
Expand Down Expand Up @@ -229,6 +231,7 @@ func defaultKeyBinds() KeyBind {
actionSkipLines: {"ctrl+s"},
actionTabWidth: {"t"},
actionGoLine: {"g"},
actionSectionNum: {"F7"},
actionNextSearch: {"n"},
actionNextBackSearch: {"N"},
actionNextDoc: {"]"},
Expand Down Expand Up @@ -338,6 +341,7 @@ func (k KeyBind) String() string {
k.writeKeyBind(&b, actionPrevSection, "previous section")
k.writeKeyBind(&b, actionLastSection, "last section")
k.writeKeyBind(&b, actionFollowSection, "follow section mode toggle")
k.writeKeyBind(&b, actionSectionNum, "section header number")

fmt.Fprint(&b, "\n\tClose and reload\n")
fmt.Fprint(&b, "\n")
Expand Down

0 comments on commit a7967bc

Please sign in to comment.