From 6a1895d4c1afdd71dcdb671860057b7c15c2a5cb Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Sun, 5 Nov 2023 11:19:14 +0900 Subject: [PATCH] Separate search_move.go by refactoring Moved search movement to search_move.go. Delete unnecessary logs. --- oviewer/action.go | 11 ++-- oviewer/event.go | 33 ++++++++++-- oviewer/mouse.go | 7 ++- oviewer/move.go | 115 +---------------------------------------- oviewer/oviewer.go | 7 ++- oviewer/search_move.go | 70 +++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 126 deletions(-) create mode 100644 oviewer/search_move.go diff --git a/oviewer/action.go b/oviewer/action.go index feb8888e..43ba58ca 100644 --- a/oviewer/action.go +++ b/oviewer/action.go @@ -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) } } }() @@ -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, ".") @@ -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 } diff --git a/oviewer/event.go b/oviewer/event.go index f00ca2ff..c7669a76 100644 --- a/oviewer/event.go +++ b/oviewer/event.go @@ -3,6 +3,7 @@ package oviewer import ( "context" "log" + "strconv" "sync/atomic" "time" @@ -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. @@ -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) } diff --git a/oviewer/mouse.go b/oviewer/mouse.go index 87934e6e..d48abe7a 100644 --- a/oviewer/mouse.go +++ b/oviewer/mouse.go @@ -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() @@ -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() { diff --git a/oviewer/move.go b/oviewer/move.go index b58690e1..4e395646 100644 --- a/oviewer/move.go +++ b/oviewer/move.go @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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) } @@ -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) } @@ -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() @@ -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() @@ -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 - } -} diff --git a/oviewer/oviewer.go b/oviewer/oviewer.go index 44e4617e..78c088f8 100644 --- a/oviewer/oviewer.go +++ b/oviewer/oviewer.go @@ -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. diff --git a/oviewer/search_move.go b/oviewer/search_move.go new file mode 100644 index 00000000..36965052 --- /dev/null +++ b/oviewer/search_move.go @@ -0,0 +1,70 @@ +package oviewer + +// 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 + } +}