Skip to content

Commit

Permalink
Added a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Jun 22, 2020
1 parent ceeb0f6 commit 02b50c2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 64 deletions.
6 changes: 5 additions & 1 deletion internal/oviewer/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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" {
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions internal/oviewer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
34 changes: 23 additions & 11 deletions internal/oviewer/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
104 changes: 52 additions & 52 deletions internal/oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -451,20 +451,20 @@ 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
}
if root.TabWidth == width {
return
}

root.TabWidth = width
root.message = fmt.Sprintf("Set tab width %d", width)
root.Model.ClearCache()
Expand Down

0 comments on commit 02b50c2

Please sign in to comment.