Skip to content

Commit

Permalink
Unified variable names (refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Dec 8, 2020
1 parent 92b1c8e commit 4043251
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 109 deletions.
16 changes: 8 additions & 8 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func (m *Document) ReadFile(fileName string) error {
}

// GetLine returns one line from buffer.
func (m *Document) GetLine(lineNum int) string {
func (m *Document) GetLine(lN int) string {
m.mu.Lock()
defer m.mu.Unlock()
if lineNum < 0 || lineNum >= len(m.lines) {
if lN < 0 || lN >= len(m.lines) {
return ""
}
return m.lines[lineNum]
return m.lines[lN]
}

// BufEndNum return last line number.
Expand Down Expand Up @@ -132,12 +132,12 @@ func (m *Document) ClearCache() {
}

// lineToContents returns contents from line number.
func (m *Document) lineToContents(lineNum int, tabWidth int) (lineContents, error) {
if lineNum < 0 || lineNum >= m.BufEndNum() {
func (m *Document) lineToContents(lN int, tabWidth int) (lineContents, error) {
if lN < 0 || lN >= m.BufEndNum() {
return nil, ErrOutOfRange
}

value, found := m.cache.Get(lineNum)
value, found := m.cache.Get(lN)
if found {
lc, ok := value.(lineContents)
if !ok {
Expand All @@ -146,8 +146,8 @@ func (m *Document) lineToContents(lineNum int, tabWidth int) (lineContents, erro
return lc, nil
}

lc := parseString(m.GetLine(lineNum), tabWidth)
lc := parseString(m.GetLine(lN), tabWidth)

m.cache.Set(lineNum, lc, 1)
m.cache.Set(lN, lc, 1)
return lc, nil
}
35 changes: 18 additions & 17 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func (root *Root) draw() {

// Calculate the bottom line when it is possible to reach EOF.
if m.topLN+root.vHight >= m.endNum {
l, x := root.bottomLineNum(m.endNum)
if m.topLN > l || (m.topLN == l && m.topLX > x) {
tx, tn := root.bottomLineNum(m.endNum)
if m.topLN > tn || (m.topLN == tn && m.topLX > tx) {
if m.BufEOF() {
root.message = "EOF"
}
m.topLN = l
m.topLX = x
m.topLN = tn
m.topLX = tx
}
}

Expand All @@ -37,6 +37,7 @@ func (root *Root) draw() {
if m.WrapMode {
lX = m.topLX
}

if m.topLN < 0 {
m.topLN = 0
}
Expand Down Expand Up @@ -141,11 +142,11 @@ func (root *Root) drawBody(lX int, lY int) (int, int) {

// line number mode
if m.LineNumMode {
lineNum := strToContents(fmt.Sprintf("%*d", root.startX-1, m.topLN+lY-m.Header+1), m.TabWidth)
for i := 0; i < len(lineNum); i++ {
lineNum[i].style = tcell.StyleDefault.Bold(true)
lc := strToContents(fmt.Sprintf("%*d", root.startX-1, m.topLN+lY-m.Header+1), m.TabWidth)
for i := 0; i < len(lc); i++ {
lc[i].style = tcell.StyleDefault.Bold(true)
}
root.setContentString(0, y, lineNum)
root.setContentString(0, y, lc)
}

root.lnumber[y] = lineNumber{
Expand Down Expand Up @@ -178,16 +179,16 @@ func (root *Root) drawBody(lX int, lY int) (int, int) {
return lX, lY
}

func (root *Root) getContentsStr(lineNum int, lc lineContents) (string, map[int]int) {
if root.Doc.lastContentsNum != lineNum {
func (root *Root) getContentsStr(lN int, lc lineContents) (string, map[int]int) {
if root.Doc.lastContentsNum != lN {
root.Doc.lastContentsStr, root.Doc.lastContentsMap = contentsToStr(lc)
root.Doc.lastContentsNum = lineNum
root.Doc.lastContentsNum = lN
}
return root.Doc.lastContentsStr, root.Doc.lastContentsMap
}

func (root *Root) getLineContents(lineNum int, tabWidth int) lineContents {
lc, err := root.Doc.lineToContents(lineNum, tabWidth)
func (root *Root) getLineContents(lN int, tabWidth int) lineContents {
lc, err := root.Doc.lineToContents(lN, tabWidth)
if err == nil {
for n := range lc {
lc[n].style = lc[n].style.Reverse(false)
Expand Down Expand Up @@ -232,8 +233,8 @@ func (root *Root) drawEOL(eol int, y int) {

// reverses the specified range.
func reverseContents(lc lineContents, start int, end int) {
for n := start; n < end; n++ {
lc[n].style = lc[n].style.Reverse(true)
for x := start; x < end; x++ {
lc[x].style = lc[x].style.Reverse(true)
}
}

Expand Down Expand Up @@ -287,8 +288,8 @@ func (root *Root) noWrapContents(y int, lX int, lY int, lc lineContents) (int, i

// headerStyle applies the style of the header.
func (root *Root) headerStyle(lc lineContents) {
for i := 0; i < len(lc); i++ {
lc[i].style = applyStyle(lc[i].style, root.StyleHeader)
for x := 0; x < len(lc); x++ {
lc[x].style = applyStyle(lc[x].style, root.StyleHeader)
}
}

Expand Down
39 changes: 4 additions & 35 deletions oviewer/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (root *Root) moveBottom() {
}

// Move to the specified line.
func (root *Root) moveLine(num int) {
func (root *Root) moveLine(lN int) {
root.resetSelect()
root.Doc.topLN = num
root.Doc.topLN = lN
root.Doc.topLX = 0
}

Expand Down Expand Up @@ -75,39 +75,8 @@ func (root *Root) moveNumUp(moveY int) {

// WrapMode
num := root.Doc.topLN + root.Doc.Header
x := root.Doc.topLX

listX, err := root.leftMostX(num)
if err != nil {
log.Println(err, num)
return
}
n := numOfSlice(listX, x)

for y := moveY; y > 0; y-- {
if n <= 0 {
num--
if num < root.Doc.Header {
num = 0
x = 0
break
}
listX, err = root.leftMostX(num)
if err != nil {
log.Println(err, num)
return
}
n = len(listX)
}
if n > 0 {
x = listX[n-1]
} else {
x = 0
}
n--
}
root.Doc.topLX, num = root.findNumUp(root.Doc.topLX, num, moveY)
root.Doc.topLN = num - root.Doc.Header
root.Doc.topLX = x
}

// Moves down by the specified number of y.
Expand Down Expand Up @@ -160,8 +129,8 @@ func (root *Root) moveUp() {
}

if !root.Doc.WrapMode {
root.Doc.topLX = 0
root.Doc.topLN--
root.Doc.topLX = 0
return
}

Expand Down
100 changes: 54 additions & 46 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,66 +599,74 @@ func (root *Root) setWrapHeaderLen() {

// bottomLineNum returns the display start line
// when the last line number as an argument.
func (root *Root) bottomLineNum(num int) (int, int) {
func (root *Root) bottomLineNum(lN int) (int, int) {
hight := (root.vHight - root.headerLen()) - 2
if num < root.headerLen() {
if lN < root.headerLen() {
return 0, 0
}

if !root.Doc.WrapMode {
return num - (hight + root.headerLen()), 0
return 0, lN - (hight + root.headerLen())
}

// WrapMode
var listX []int
var err error
x := 0
lX, lN := root.findNumUp(0, lN, hight)
return lX, lN - root.Doc.Header
}

// leftMostX returns a list of left - most x positions when wrapping.
func (root *Root) leftMostX(lN int) ([]int, error) {
lc, err := root.Doc.lineToContents(lN, root.Doc.TabWidth)
if err != nil {
return nil, err
}

listX := make([]int, 0, (len(lc)/root.vWidth)+1)
width := (root.vWidth - root.startX)

listX = append(listX, 0)
for n := width; n < len(lc); n += width {
if lc[n-1].width == 2 {
n--
}
listX = append(listX, n)
}
return listX, nil
}

// findNumUp finds lX, lN when the number of lines is moved up from lX, lN.
func (root *Root) findNumUp(lX int, lN int, upY int) (int, int) {
listX, err := root.leftMostX(lN)
n := 0
for y := hight; y > 0; y-- {
if err != nil {
log.Println(err, lN)
} else {
n = numOfSlice(listX, lX)
}

for y := upY; y > 0; y-- {
if n <= 0 {
num--
if num < root.Doc.Header {
num = 0
x = 0
lN--
if lN < root.Doc.Header {
lN = 0
lX = 0
break
}
listX, err = root.leftMostX(num)
listX, err = root.leftMostX(lN)
if err != nil {
log.Println(err, num)
log.Println(err, lN)
return 0, 0
}
n = len(listX)
}
if n > 0 {
x = listX[n-1]
lX = listX[n-1]
} else {
x = 0
lX = 0
}
n--
}

return num - root.Doc.Header, x
}

// leftMostX returns a list of left - most x positions when wrapping.
func (root *Root) leftMostX(num int) ([]int, error) {
lc, err := root.Doc.lineToContents(num, root.Doc.TabWidth)
if err != nil {
return nil, err
}

listX := make([]int, 0, root.vHight)
lineLength := len(lc)
width := (root.vWidth - root.startX)
for n := 0; n < lineLength; n += width {
if n > 0 && n < lineLength {
if lc[n-1].width == 2 {
n--
}
}
listX = append(listX, n)
}
return listX, nil
return lX, lN
}

// toggleWrapMode toggles wrapMode each time it is called.
Expand Down Expand Up @@ -718,14 +726,14 @@ func (root *Root) updateEndNum() {

// goLine will move to the specified line.
func (root *Root) goLine(input string) {
lineNum, err := strconv.Atoi(input)
lN, err := strconv.Atoi(input)
if err != nil {
root.setMessage(ErrInvalidNumber.Error())
return
}

root.moveLine(lineNum - root.Doc.Header - 1)
root.setMessage(fmt.Sprintf("Moved to line %d", lineNum))
root.moveLine(lN - root.Doc.Header - 1)
root.setMessage(fmt.Sprintf("Moved to line %d", lN))
}

// markLineNum stores the specified number of lines.
Expand All @@ -738,21 +746,21 @@ func (root *Root) markLineNum() {

// setHeader sets the number of lines in the header.
func (root *Root) setHeader(input string) {
lineNum, err := strconv.Atoi(input)
num, err := strconv.Atoi(input)
if err != nil {
root.setMessage(ErrInvalidNumber.Error())
return
}
if lineNum < 0 || lineNum > root.vHight-1 {
if num < 0 || num > root.vHight-1 {
root.setMessage(ErrOutOfRange.Error())
return
}
if root.Doc.Header == lineNum {
if root.Doc.Header == num {
return
}

root.Doc.Header = lineNum
root.setMessage(fmt.Sprintf("Set Header %d", lineNum))
root.Doc.Header = num
root.setMessage(fmt.Sprintf("Set Header %d", num))
root.setWrapHeaderLen()
root.Doc.ClearCache()
}
Expand Down
6 changes: 3 additions & 3 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (root *Root) backSearch(ctx context.Context, input string) {
}

// search searches forward or backward.
func (root *Root) search(ctx context.Context, num int, searchFunc func(context.Context, int) (int, error)) {
func (root *Root) search(ctx context.Context, lN int, searchFunc func(context.Context, int) (int, error)) {
root.setMessage(fmt.Sprintf("search:%v (%v)Cancel", root.input.value, strings.Join(root.cancelKeys, ",")))

eg, ctx := errgroup.WithContext(ctx)
Expand All @@ -52,11 +52,11 @@ func (root *Root) search(ctx context.Context, num int, searchFunc func(context.C
})

eg.Go(func() error {
lineNum, err := searchFunc(ctx, num)
lN, err := searchFunc(ctx, lN)
if err != nil {
return err
}
root.moveLine(lineNum - root.Doc.Header)
root.moveLine(lN - root.Doc.Header)
return nil
})

Expand Down

0 comments on commit 4043251

Please sign in to comment.