From 02b50c2219cd172785845056cbe429bd66e343b6 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Tue, 23 Jun 2020 08:08:45 +0900 Subject: [PATCH] Added a comment --- internal/oviewer/content.go | 6 ++- internal/oviewer/input.go | 2 + internal/oviewer/model.go | 34 ++++++++---- internal/oviewer/oviewer.go | 104 ++++++++++++++++++------------------ 4 files changed, 82 insertions(+), 64 deletions(-) diff --git a/internal/oviewer/content.go b/internal/oviewer/content.go index f8b3bc1e..5fd4a650 100644 --- a/internal/oviewer/content.go +++ b/internal/oviewer/content.go @@ -55,7 +55,7 @@ func parseString(line string, tabWidth int) lineContents { style := tcell.StyleDefault x := 0 n := 0 - bsFlag := false + bsFlag := false // backspace(^H) flag var bsContent Content for _, runeValue := range line { c := DefaultContent @@ -181,6 +181,7 @@ func parseString(line string, tabWidth int) lineContents { return lc } +// overstrike returns an overstrike tcell.Style. func overstrike(p, m rune, style tcell.Style) tcell.Style { if p == m { style = OverStrikeStyle @@ -190,6 +191,7 @@ func overstrike(p, m rune, style tcell.Style) tcell.Style { return style } +// lastContent returns the last character of Contents. func lastContent(contents []Content) Content { n := len(contents) if n == 0 { @@ -201,6 +203,7 @@ func lastContent(contents []Content) Content { return contents[n-1] } +// csToStyle returns tcell.Style from the control sequence. func csToStyle(style tcell.Style, csiParameter *bytes.Buffer) tcell.Style { fields := strings.Split(csiParameter.String(), ";") if len(fields) == 0 || len(fields) == 1 && fields[0] == "0" { @@ -275,6 +278,7 @@ FieldLoop: return style } +// lookupColor returns the color name from the color number. func lookupColor(colorNumber int) string { if colorNumber < 0 || colorNumber > 15 { return "black" diff --git a/internal/oviewer/input.go b/internal/oviewer/input.go index c3e35c0a..0b3ed979 100644 --- a/internal/oviewer/input.go +++ b/internal/oviewer/input.go @@ -131,6 +131,7 @@ func (root *Root) inputKeyEvent(ev *tcell.EventKey) bool { return false } +// stringWidth returns the number of characters in the input. func stringWidth(str string, cursor int) int { width := 0 i := 0 @@ -147,6 +148,7 @@ func stringWidth(str string, cursor int) int { return i } +// runeWidth returns the number of widths of the input. func runeWidth(str string) int { width := 0 for _, r := range str { diff --git a/internal/oviewer/model.go b/internal/oviewer/model.go index 4eebf636..5b550c83 100644 --- a/internal/oviewer/model.go +++ b/internal/oviewer/model.go @@ -9,20 +9,31 @@ import ( // The Model structure contains the values // for the logical screen. type Model struct { - // updated by reader goroutine + // buffer stores the contents of the file in slices of strings. + // buffer,endNum and eof is updated by reader goroutine. buffer []string + // endNum is the number of the last line read. endNum int - eof bool - - x int - lineNum int - yy int - header []string + // eof is or has reached EOF. + eof bool + + // x is the starting position of the current x. + x int + // lineNum is the starting position of the current y. + lineNum int + // yy represents the number of wrapped lines. + yy int + // header represents the header line. + header []string + // beforeSize represents the number of lines to read first. beforeSize int - vWidth int - vHight int - cache *ristretto.Cache - + // vWidth represents the screen width. + vWidth int + // vHight represents the screen height. + vHight int + // cache represents a cache of contents. + cache *ristretto.Cache + // mu controls the mutex. mu sync.Mutex } @@ -94,6 +105,7 @@ func (m *Model) GetContents(lineNum int, tabWidth int) []Content { return lc.contents } +// lineToContents returns contents from line number. func (m *Model) lineToContents(lineNum int, tabWidth int) (lineContents, error) { var lc lineContents diff --git a/internal/oviewer/oviewer.go b/internal/oviewer/oviewer.go index b9952629..c517de79 100644 --- a/internal/oviewer/oviewer.go +++ b/internal/oviewer/oviewer.go @@ -117,15 +117,6 @@ func New() *Root { return root } -// PrepareView prepares when the screen size is changed. -func (root *Root) PrepareView() { - m := root.Model - screen := root.Screen - m.vWidth, m.vHight = screen.Size() - root.setWrapHeaderLen() - root.statusPos = m.vHight - 1 -} - // Run is reads the file(or stdin) and starts // the terminal pager. func (root *Root) Run(args []string) error { @@ -203,6 +194,45 @@ func (root *Root) Run(args []string) error { return nil } +// main is manages and executes events in the main routine. +func (root *Root) main() { + screen := root.Screen + go root.countTimer() + +loop: + for { + root.Draw() + ev := screen.PollEvent() + switch ev := ev.(type) { + case *eventTimer: + root.updateEndNum() + case *eventAppQuit: + break loop + case *SearchInput: + root.Search(root.Input.value) + case *BackSearchInput: + root.BackSearch(root.Input.value) + case *GotoInput: + root.GoLine(root.Input.value) + case *HeaderInput: + root.SetHeader(root.Input.value) + case *DelimiterInput: + root.SetDelimiter(root.Input.value) + case *TABWidthInput: + root.SetTabWidth(root.Input.value) + case *tcell.EventKey: + root.message = "" + if root.Input.mode == normal { + root.DefaultKeyEvent(ev) + } else { + root.InputKeyEvent(ev) + } + case *tcell.EventResize: + root.Resize() + } + } +} + // setGlobalStyle sets some styles that are determined by the settings. func (root *Root) setGlobalStyle() { if root.ColorAlternate != "" { @@ -219,6 +249,15 @@ func (root *Root) setGlobalStyle() { } } +// PrepareView prepares when the screen size is changed. +func (root *Root) PrepareView() { + m := root.Model + screen := root.Screen + m.vWidth, m.vHight = screen.Size() + root.setWrapHeaderLen() + root.statusPos = m.vHight - 1 +} + // contentsSmall returns with bool whether the file to display fits on the screen. func (root *Root) contentsSmall() bool { root.PrepareView() @@ -368,53 +407,14 @@ func (root *Root) updateEndNum() { root.statusDraw() } -// main is manages and executes events in the main routine. -func (root *Root) main() { - screen := root.Screen - go root.countTimer() - -loop: - for { - root.Draw() - ev := screen.PollEvent() - switch ev := ev.(type) { - case *eventTimer: - root.updateEndNum() - case *eventAppQuit: - break loop - case *SearchInput: - root.Search(root.Input.value) - case *BackSearchInput: - root.BackSearch(root.Input.value) - case *GotoInput: - root.GoLine(root.Input.value) - case *HeaderInput: - root.SetHeader(root.Input.value) - case *DelimiterInput: - root.SetDelimiter(root.Input.value) - case *TABWidthInput: - root.SetTabWidth(root.Input.value) - case *tcell.EventKey: - root.message = "" - if root.Input.mode == normal { - root.DefaultKeyEvent(ev) - } else { - root.InputKeyEvent(ev) - } - case *tcell.EventResize: - root.Resize() - } - } -} - // GoLine will move to the specified line. func (root *Root) GoLine(input string) { lineNum, err := strconv.Atoi(input) - root.Input.value = "" if err != nil { root.message = ErrInvalidNumber.Error() return } + root.moveNum(lineNum - root.Header - 1) root.message = fmt.Sprintf("Moved to line %d", lineNum) } @@ -430,7 +430,6 @@ func (root *Root) MarkLineNum(lineNum int) { // SetHeader sets the number of lines in the header. func (root *Root) SetHeader(input string) { lineNum, err := strconv.Atoi(input) - root.Input.value = "" if err != nil { root.message = ErrInvalidNumber.Error() return @@ -442,6 +441,7 @@ func (root *Root) SetHeader(input string) { if root.Header == lineNum { return } + root.Header = lineNum root.message = fmt.Sprintf("Set Header %d", lineNum) root.setWrapHeaderLen() @@ -451,13 +451,12 @@ func (root *Root) SetHeader(input string) { // SetDelimiter sets the delimiter string. func (root *Root) SetDelimiter(input string) { root.ColumnDelimiter = input - root.Input.value = "" + root.message = fmt.Sprintf("Set delimiter %s", input) } // SetTabWidth sets the tab width. func (root *Root) SetTabWidth(input string) { width, err := strconv.Atoi(input) - root.Input.value = "" if err != nil { root.message = ErrInvalidNumber.Error() return @@ -465,6 +464,7 @@ func (root *Root) SetTabWidth(input string) { if root.TabWidth == width { return } + root.TabWidth = width root.message = fmt.Sprintf("Set tab width %d", width) root.Model.ClearCache()