Skip to content

Commit

Permalink
Merge pull request #467 from noborus/input-section-num
Browse files Browse the repository at this point in the history
Added input mode for section header number
  • Loading branch information
noborus authored Dec 26, 2023
2 parents 6ab515e + fdb88e4 commit 81f4de1
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 24 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
2 changes: 2 additions & 0 deletions ov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ KeyBind:
section_start:
- "ctrl+F3"
- "alt+s"
section_header_num:
- "F7"
next_section:
- "space"
last_section:
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
38 changes: 18 additions & 20 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 All @@ -254,8 +257,7 @@ func defaultKeyBinds() KeyBind {
// String returns keybind as a string for help.
func (k KeyBind) String() string {
var b strings.Builder
fmt.Fprint(&b, "\n\tKey binding\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Key binding")
k.writeKeyBind(&b, actionExit, "quit")
k.writeKeyBind(&b, actionCancel, "cancel")
k.writeKeyBind(&b, actionWriteExit, "output screen and quit")
Expand All @@ -269,8 +271,7 @@ func (k KeyBind) String() string {
k.writeKeyBind(&b, actionToggleMouse, "enable/disable mouse")
k.writeKeyBind(&b, actionSaveBuffer, "save buffer to file")

fmt.Fprint(&b, "\n\tMoving\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Moving")
k.writeKeyBind(&b, actionMoveDown, "forward by one line")
k.writeKeyBind(&b, actionMoveUp, "backward by one line")
k.writeKeyBind(&b, actionMoveTop, "go to top of document")
Expand All @@ -289,29 +290,25 @@ func (k KeyBind) String() string {
k.writeKeyBind(&b, actionMoveEndRight, "go to end of line")
k.writeKeyBind(&b, actionGoLine, "go to line(input number or `.n` or `n%` allowed)")

fmt.Fprint(&b, "\n\tMove document\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Move document")
k.writeKeyBind(&b, actionNextDoc, "next document")
k.writeKeyBind(&b, actionPreviousDoc, "previous document")
k.writeKeyBind(&b, actionCloseDoc, "close current document")

fmt.Fprint(&b, "\n\tMark position\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Mark position")
k.writeKeyBind(&b, actionMark, "mark current position")
k.writeKeyBind(&b, actionRemoveMark, "remove mark current position")
k.writeKeyBind(&b, actionRemoveAllMark, "remove all mark")
k.writeKeyBind(&b, actionMoveMark, "move to next marked position")
k.writeKeyBind(&b, actionMovePrevMark, "move to previous marked position")

fmt.Fprint(&b, "\n\tSearch\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Search")
k.writeKeyBind(&b, actionSearch, "forward search mode")
k.writeKeyBind(&b, actionBackSearch, "backward search mode")
k.writeKeyBind(&b, actionNextSearch, "repeat forward search")
k.writeKeyBind(&b, actionNextBackSearch, "repeat backward search")

fmt.Fprint(&b, "\n\tChange display\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Change display")
k.writeKeyBind(&b, actionWrap, "wrap/nowrap toggle")
k.writeKeyBind(&b, actionColumnMode, "column mode toggle")
k.writeKeyBind(&b, actionColumnWidth, "column width toggle")
Expand All @@ -320,8 +317,7 @@ func (k KeyBind) String() string {
k.writeKeyBind(&b, actionLineNumMode, "line number toggle")
k.writeKeyBind(&b, actionPlain, "original decoration toggle(plain)")

fmt.Fprint(&b, "\n\tChange Display with Input\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Change Display with Input")
k.writeKeyBind(&b, actionViewMode, "view mode selection")
k.writeKeyBind(&b, actionDelimiter, "column delimiter string")
k.writeKeyBind(&b, actionHeader, "number of header lines")
Expand All @@ -330,24 +326,22 @@ func (k KeyBind) String() string {
k.writeKeyBind(&b, actionMultiColor, "multi color highlight")
k.writeKeyBind(&b, actionJumpTarget, "jump target(`.n` or `n%` or `section` allowed)")

fmt.Fprint(&b, "\n\tSection\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Section")
k.writeKeyBind(&b, actionSection, "section delimiter regular expression")
k.writeKeyBind(&b, actionSectionStart, "section start position")
k.writeKeyBind(&b, actionNextSection, "next section")
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")
writeHeader(&b, "Close and reload")
k.writeKeyBind(&b, actionCloseFile, "close file")
k.writeKeyBind(&b, actionReload, "reload file")
k.writeKeyBind(&b, actionWatch, "watch mode")
k.writeKeyBind(&b, actionWatchInterval, "set watch interval")

fmt.Fprint(&b, "\n\tKey binding when typing\n")
fmt.Fprint(&b, "\n")
writeHeader(&b, "Key binding when typing")
k.writeKeyBind(&b, inputCaseSensitive, "case-sensitive toggle")
k.writeKeyBind(&b, inputSmartCaseSensitive, "smart case-sensitive toggle")
k.writeKeyBind(&b, inputRegexpSearch, "regular expression search toggle")
Expand All @@ -359,6 +353,10 @@ func (k KeyBind) String() string {
return b.String()
}

func writeHeader(w io.Writer, header string) {
fmt.Fprintf(w, "\n\t%s\n", header)
}

func (k KeyBind) writeKeyBind(w io.Writer, action string, detail string) {
fmt.Fprintf(w, " %-28s * %s\n", "["+strings.Join(k[action], "], [")+"]", detail)
}
Expand Down

0 comments on commit 81f4de1

Please sign in to comment.