Skip to content

Commit

Permalink
Separate search_move.go by refactoring
Browse files Browse the repository at this point in the history
Moved search movement to search_move.go.
Delete unnecessary logs.
  • Loading branch information
noborus committed Nov 5, 2023
1 parent a6d2792 commit 6a1895d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 126 deletions.
11 changes: 5 additions & 6 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ func (root *Root) watchControl() {
m.tickerDone <- struct{}{}
return
case <-m.ticker.C:
ev := &eventReload{}
ev.SetEventNow()
ev.m = m
root.postEvent(ev)
root.sendReload(m)
}
}
}()
Expand Down Expand Up @@ -549,6 +546,10 @@ func jumpPosition(height int, str string) (int, bool) {
// numbers (1), returns dot.number for percentages (.5) = 50%,
// and returns the % after the number for percentages (50%). return.
func calculatePosition(length int, str string) float64 {
if len(str) == 0 || str == "0" {
return 0
}

var p float64 = 0
if strings.HasPrefix(str, ".") {
str = strings.TrimLeft(str, ".")
Expand All @@ -573,10 +574,8 @@ func calculatePosition(length int, str string) float64 {

num, err := strconv.ParseFloat(str, 64)
if err != nil {
log.Println(err)
return 0
}

return num
}

Expand Down
33 changes: 30 additions & 3 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oviewer
import (
"context"
"log"
"strconv"
"sync/atomic"
"time"

Expand Down Expand Up @@ -98,6 +99,32 @@ func (root *Root) eventLoop(ctx context.Context, quitChan chan<- struct{}) {
}
}

// sendGoto fires an eventGoto event that moves to the specified line.
func (root *Root) sendGoto(num int) {
ev := &eventGoto{}
ev.value = strconv.Itoa(num)
ev.SetEventNow()
root.postEvent(ev)
}

// MoveLine fires an eventGoto event that moves to the specified line.
func (root *Root) MoveLine(num int) {
if !root.checkScreen() {
return
}
root.sendGoto(num)
}

// MoveTop fires the event of moving to top.
func (root *Root) MoveTop() {
root.MoveLine(root.Doc.BufStartNum())
}

// MoveBottom fires the event of moving to bottom.
func (root *Root) MoveBottom() {
root.MoveLine(root.Doc.BufEndNum())
}

// everyUpdate is called every time before running the event.
func (root *Root) everyUpdate() {
// If tmpLN is set, set top position to position from bottom.
Expand Down Expand Up @@ -294,15 +321,15 @@ type eventReload struct {

// Reload fires the eventReload event.
func (root *Root) Reload() {
root.sendReload()
root.sendReload(root.Doc)
}

func (root *Root) sendReload() {
func (root *Root) sendReload(m *Document) {
if !root.checkScreen() {
return
}
ev := &eventReload{}
ev.m = root.Doc
ev.m = m
ev.SetEventNow()
root.postEvent(ev)
}
Expand Down
7 changes: 5 additions & 2 deletions oviewer/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/mattn/go-runewidth"
)

// WheelScrollNum is the number of lines to scroll with the mouse wheel.
var WheelScrollNum = 2

// mouseEvent handles mouse events.
func (root *Root) mouseEvent(ev *tcell.EventMouse) {
button := ev.Buttons()
Expand Down Expand Up @@ -60,13 +63,13 @@ func (root *Root) mouseEvent(ev *tcell.EventMouse) {
// wheelUp moves the mouse wheel up.
func (root *Root) wheelUp() {
root.setMessage("")
root.moveUp(2)
root.moveUp(WheelScrollNum)
}

// wheelDown moves the mouse wheel down.
func (root *Root) wheelDown() {
root.setMessage("")
root.moveDown(2)
root.moveDown(WheelScrollNum)
}

func (root *Root) wheelRight() {
Expand Down
115 changes: 2 additions & 113 deletions oviewer/move.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
package oviewer

import (
"strconv"
)
// Called by event key to change document position.

// bottomMargin is the margin of the bottom line when specifying section
// bottomMargin is the margin of the bottom line when specifying section.
const bottomMargin = 2

// MoveLine fires an eventGoto event that moves to the specified line.
func (root *Root) MoveLine(num int) {
if !root.checkScreen() {
return
}
root.sendGoto(num)
}

// sendGoto fires an eventGoto event that moves to the specified line.
func (root *Root) sendGoto(num int) {
ev := &eventGoto{}
ev.value = strconv.Itoa(num)
ev.SetEventNow()
root.postEvent(ev)
}

// MoveTop fires the event of moving to top.
func (root *Root) MoveTop() {
root.MoveLine(root.Doc.BufStartNum())
}

// MoveBottom fires the event of moving to bottom.
func (root *Root) MoveBottom() {
root.MoveLine(root.Doc.BufEndNum())
}

// Go to the top line.
// Called from a EventKey.
func (root *Root) moveTop() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -43,7 +14,6 @@ func (root *Root) moveTop() {
}

// Go to the bottom line.
// Called from a EventKey.
func (root *Root) moveBottom() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -52,7 +22,6 @@ func (root *Root) moveBottom() {
}

// Move up one screen.
// Called from a EventKey.
func (root *Root) movePgUp() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -61,7 +30,6 @@ func (root *Root) movePgUp() {
}

// Moves down one screen.
// Called from a EventKey.
func (root *Root) movePgDn() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -70,7 +38,6 @@ func (root *Root) movePgDn() {
}

// Moves up half a screen.
// Called from a EventKey.
func (root *Root) moveHfUp() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -79,7 +46,6 @@ func (root *Root) moveHfUp() {
}

// Moves down half a screen.
// Called from a EventKey.
func (root *Root) moveHfDn() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -88,13 +54,11 @@ func (root *Root) moveHfDn() {
}

// Move up one line.
// Called from a EventKey.
func (root *Root) moveUpOne() {
root.moveUp(1)
}

// Move down one line.
// Called from a EventKey.
func (root *Root) moveDownOne() {
root.moveDown(1)
}
Expand Down Expand Up @@ -146,25 +110,21 @@ func (root *Root) lastSection() {
}

// Move to the left.
// Called from a EventKey.
func (root *Root) moveLeftOne() {
root.moveLeft(1)
}

// Move to the right.
// Called from a EventKey.
func (root *Root) moveRightOne() {
root.moveRight(1)
}

// Move to the width of the screen to the left.
// Called from a EventKey.
func (root *Root) moveWidthLeft() {
root.moveLeft(root.Doc.HScrollWidthNum)
}

// Move to the width of the screen to the right.
// Called from a EventKey.
func (root *Root) moveWidthRight() {
root.moveRight(root.Doc.HScrollWidthNum)
}
Expand Down Expand Up @@ -194,7 +154,6 @@ func (root *Root) moveRight(n int) {
}

// Move to the left by half a screen.
// Called from a EventKey.
func (root *Root) moveHfLeft() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand All @@ -204,7 +163,6 @@ func (root *Root) moveHfLeft() {
}

// Move to the right by half a screen.
// Called from a EventKey.
func (root *Root) moveHfRight() {
root.resetSelect()
defer root.releaseEventBuffer()
Expand Down Expand Up @@ -247,72 +205,3 @@ func (root *Root) moveColumnRight(n int) {
root.debugMessage(err.Error())
}
}

// searchGoTo moves to the specified line and position after searching.
// Go to the specified line +root.Doc.JumpTarget Go to.
// 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()
m.moveYUp(m.jumpTargetNum)
}

// Bottom line of jumpTarget when specifying a section
func (m *Document) bottomJumpTarget() int {
return m.statusPos - bottomMargin
}

// searchGoSection will go to the section with the matching term after searching.
// Move the JumpTarget so that it can be seen from the beginning of the section.
func (m *Document) searchGoSection(lN int, x int) {
m.searchGoX(x)

sN, err := m.prevSection(lN)
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.
m.topLN = sN - m.firstLine()
} else {
m.topLN = sN
sN += m.firstLine()
}

if !m.WrapMode {
y = lN - sN
} else {
for n := sN; n < lN; n++ {
listX := m.leftMostX(n)
y += len(listX)
}
}

if m.bottomJumpTarget() > y+m.headerLen {
m.jumpTargetNum = y
return
}

m.jumpTargetNum = m.bottomJumpTarget() - (m.headerLen + 1)
m.moveYDown(y - m.jumpTargetNum)
}

// searchGoX moves to the specified x position.
func (m *Document) searchGoX(x int) {
m.topLX = 0
// If the search term is outside the height of the screen when in WrapMode.
if nTh := x / m.width; nTh > m.height {
m.topLX = nTh * m.width
}

// If the search term is outside the width of the screen when in NoWrapMode.
if x < m.x || x > m.x+m.width-1 {
m.x = x
}
}
7 changes: 5 additions & 2 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,13 @@ func (root *Root) prepareView() {
// Do not allow size 0.
root.scr.vWidth = max(root.scr.vWidth, 1)
root.scr.vHeight = max(root.scr.vHeight, 1)
root.Doc.statusPos = root.scr.vHeight - statusLine

num := int(math.Round(calculatePosition(root.scr.vWidth, root.Doc.HScrollWidth)))
root.Doc.HScrollWidthNum = max(num, 1)
root.scr.numbers = make([]LineNumber, root.scr.vHeight+1)
root.Doc.statusPos = root.scr.vHeight - statusLine
if len(root.scr.numbers) != root.scr.vHeight+1 {
root.scr.numbers = make([]LineNumber, root.scr.vHeight+1)
}
}

// docSmall returns with bool whether the file to display fits on the screen.
Expand Down
Loading

0 comments on commit 6a1895d

Please sign in to comment.