Skip to content

Commit

Permalink
Merge pull request #413 from noborus/minor-fix
Browse files Browse the repository at this point in the history
Minor fix
  • Loading branch information
noborus authored Jul 14, 2023
2 parents 807763c + 9021af3 commit b86cb62
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 60 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ func flagUsage(f *pflag.FlagSet) string {
return buf.String()
}

// UsageFunc returns a function that returns the usage.
// This is for overwriting cobra usage.
func UsageFunc() (cmd func(*cobra.Command) error) {
return func(c *cobra.Command) error {
fmt.Fprintf(os.Stdout, "Usage:\n")
Expand Down
52 changes: 52 additions & 0 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,55 @@ func (root *Root) updateEndNum() {
root.drawStatus()
root.Screen.Sync()
}

// follow updates the document in follow mode.
func (root *Root) follow() {
if root.General.FollowAll {
root.followAll()
}
num := root.Doc.BufEndNum()
if root.Doc.latestNum == num {
return
}

root.skipDraw = false
if root.Doc.FollowSection {
root.tailSection()
} else {
root.TailSync()
}
root.Doc.latestNum = num
}

// followAll monitors and switches all document updates
// in follow all mode.
func (root *Root) followAll() {
if root.screenMode != Docs {
return
}

current := root.CurrentDoc
root.mu.RLock()
for n, doc := range root.DocList {
if doc.latestNum != doc.BufEndNum() {
current = n
}
}
root.mu.RUnlock()

if root.CurrentDoc != current {
root.switchDocument(current)
}
}

// Cancel follow mode and follow all mode.
func (root *Root) Cancel() {
root.General.FollowAll = false
root.Doc.FollowMode = false
}

// WriteQuit sets the write flag and executes a quit event.
func (root *Root) WriteQuit() {
root.IsWriteOriginal = true
root.Quit()
}
2 changes: 1 addition & 1 deletion oviewer/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (m *Document) ControlReader(r io.Reader, reload func() *bufio.Reader) error
reader := bufio.NewReader(r)

go func() {
var err error
for sc := range m.ctlCh {
var err error
reader, err = m.controlReader(sc, reader, reload)
if err != nil {
log.Println(sc.request, err)
Expand Down
2 changes: 1 addition & 1 deletion oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type store struct {
// chunks is the content of the file to be stored in chunks.
chunks []*chunk
// mu controls the mutex.
mu sync.Mutex
mu sync.RWMutex

// startNum is the number of the first line that can be moved.
startNum int32
Expand Down
52 changes: 0 additions & 52 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,58 +181,6 @@ func (root *Root) sendSuspend() {
root.postEvent(ev)
}

// Cancel follow mode and follow all mode.
func (root *Root) Cancel() {
root.General.FollowAll = false
root.Doc.FollowMode = false
}

// WriteQuit sets the write flag and executes a quit event.
func (root *Root) WriteQuit() {
root.IsWriteOriginal = true
root.Quit()
}

// follow updates the document in follow mode.
func (root *Root) follow() {
if root.General.FollowAll {
root.followAll()
}
num := root.Doc.BufEndNum()
if root.Doc.latestNum == num {
return
}

root.skipDraw = false
if root.Doc.FollowSection {
root.tailSection()
} else {
root.TailSync()
}
root.Doc.latestNum = num
}

// followAll monitors and switches all document updates
// in follow all mode.
func (root *Root) followAll() {
if root.screenMode != Docs {
return
}

current := root.CurrentDoc
root.mu.RLock()
for n, doc := range root.DocList {
if doc.latestNum != doc.BufEndNum() {
current = n
}
}
root.mu.RUnlock()

if root.CurrentDoc != current {
root.switchDocument(current)
}
}

// updateInterval calls eventUpdate at regular intervals.
func (root *Root) updateInterval(ctx context.Context) {
timer := time.NewTicker(UpdateInterval)
Expand Down
2 changes: 1 addition & 1 deletion oviewer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ func (m *Document) loadChunk(reader *bufio.Reader, chunkNum int) (*bufio.Reader,

start, end := m.store.chunkRange(chunkNum)
if err := m.store.readLines(chunk, reader, start, end, false); err != nil {
log.Printf("Failed to read the expected number of lines(%d:%d): %s", start, end, err)
if errors.Is(err, io.EOF) {
return m.afterEOF(reader), nil
}
log.Printf("Failed to read the expected number of lines(%d:%d): %s", start, end, err)
return nil, err
}
return reader, nil
Expand Down
5 changes: 2 additions & 3 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,8 @@ func (m *Document) SearchLine(ctx context.Context, searcher Searcher, lN int) (i
default:
}

// endChunk may be updated by Search.
endChunk := (m.BufEndNum() - 1) / ChunkSize
if cn >= endChunk {
// lastChunkNum may be updated by Search.
if cn >= m.store.lastChunkNum() {
break
}
sn = 0
Expand Down
1 change: 1 addition & 0 deletions oviewer/sigtstp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
)

// registerSIGTSTP registers SIGTSTP signal.
func registerSIGTSTP() chan os.Signal {
sigSuspend := make(chan os.Signal, 1)
signal.Notify(sigSuspend, syscall.SIGTSTP)
Expand Down
4 changes: 2 additions & 2 deletions oviewer/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (s *store) unloadChunk(chunkNum int) {

// lastChunkNum returns the last chunk number.
func (s *store) lastChunkNum() int {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.RLock()
defer s.mu.RUnlock()

return len(s.chunks) - 1
}
Expand Down

0 comments on commit b86cb62

Please sign in to comment.