view.Cellbuffer: put code for testing if x,y is inside the view into a method
1 files changed, 61 insertions(+), 53 deletions(-)

M cell.go
M cell.go +61 -53
@@ 40,32 40,35 @@ type CellBuffer struct {
 	cells []cell
 }
 
+func (cb *CellBuffer) isOutside(x int, y int) bool {
+	return x < 0 || y < 0 || x >= cb.w || y >= cb.h
+}
+
 // SetContent sets the contents (primary rune, combining runes,
 // and style) for a cell at a given location.
 func (cb *CellBuffer) SetContent(x int, y int,
 	mainc rune, combc []rune, style Style) {
-
-	if x >= 0 && y >= 0 && x < cb.w && y < cb.h {
-		c := &cb.cells[(y*cb.w)+x]
+	if cb.isOutside(x, y) {
+		// todo: error?
+		return
+	}
+	c := &cb.cells[y*cb.w+x]
 
-		i := 0
-		for i < len(combc) {
-			r := combc[i]
-			if runewidth.RuneWidth(r) != 0 {
-				// not a combining character, yank it
-				combc = append(combc[:i-1], combc[i+1:]...)
-				continue
-			}
-			i++
+	for i := 0; i < len(combc); i++ {
+		r := combc[i]
+		if runewidth.RuneWidth(r) != 0 {
+			// not a combining character, yank it
+			combc = append(combc[:i-1], combc[i+1:]...)
+			continue
 		}
+	}
 
-		if c.currMain != mainc {
-			c.width = runewidth.RuneWidth(mainc)
-		}
-		c.currMain = mainc
-		c.currComb = combc
-		c.currStyle = style
+	if c.currMain != mainc {
+		c.width = runewidth.RuneWidth(mainc)
 	}
+	c.currMain = mainc
+	c.currComb = combc
+	c.currStyle = style
 }
 
 // GetContent returns the contents of a character cell, including the

          
@@ 73,13 76,15 @@ func (cb *CellBuffer) SetContent(x int, 
 // nil), the style, and the display width in cells.  (The width can be
 // either 1, normally, or 2 for East Asian full-width characters.)
 func (cb *CellBuffer) GetContent(x, y int) (mainc rune, combc []rune, style Style, width int) {
-	if x >= 0 && y >= 0 && x < cb.w && y < cb.h {
-		c := &cb.cells[(y*cb.w)+x]
-		mainc, combc, style = c.currMain, c.currComb, c.currStyle
-		if width = c.width; width == 0 || mainc < ' ' {
-			width = 1
-			mainc = ' '
-		}
+	if cb.isOutside(x, y) {
+		// todo: error?
+		return mainc, combc, style, width
+	}
+	c := &cb.cells[(y*cb.w)+x]
+	mainc, combc, style = c.currMain, c.currComb, c.currStyle
+	if width = c.width; width == 0 || mainc < ' ' {
+		width = 1
+		mainc = ' '
 	}
 	return mainc, combc, style, width
 }

          
@@ 101,25 106,27 @@ func (cb *CellBuffer) Invalidate() {
 // if the cell content is different since the last time it was
 // marked clean.
 func (cb *CellBuffer) Dirty(x, y int) bool {
-	if x >= 0 && y >= 0 && x < cb.w && y < cb.h {
-		c := &cb.cells[(y*cb.w)+x]
-		if c.lastMain == rune(0) {
-			return true
-		}
-		if c.lastMain != c.currMain {
+	if cb.isOutside(x, y) {
+		// todo: error?
+		return false
+	}
+	c := &cb.cells[(y*cb.w)+x]
+	if c.lastMain == rune(0) {
+		return true
+	}
+	if c.lastMain != c.currMain {
+		return true
+	}
+	if c.lastStyle != c.currStyle {
+		return true
+	}
+	if len(c.lastComb) != len(c.currComb) {
+		return true
+	}
+	for i := range c.lastComb {
+		if c.lastComb[i] != c.currComb[i] {
 			return true
 		}
-		if c.lastStyle != c.currStyle {
-			return true
-		}
-		if len(c.lastComb) != len(c.currComb) {
-			return true
-		}
-		for i := range c.lastComb {
-			if c.lastComb[i] != c.currComb[i] {
-				return true
-			}
-		}
 	}
 	return false
 }

          
@@ 128,18 135,19 @@ func (cb *CellBuffer) Dirty(x, y int) bo
 // been displayed (in which case dirty is false), or to manually
 // force a cell to be marked dirty.
 func (cb *CellBuffer) SetDirty(x, y int, dirty bool) {
-	if x >= 0 && y >= 0 && x < cb.w && y < cb.h {
-		c := &cb.cells[(y*cb.w)+x]
-		if dirty {
-			c.lastMain = rune(0)
-		} else {
-			if c.currMain == rune(0) {
-				c.currMain = ' '
-			}
-			c.lastMain = c.currMain
-			c.lastComb = c.currComb
-			c.lastStyle = c.currStyle
+	if cb.isOutside(x, y) {
+		// todo: error?
+		return
+	}
+	c := &cb.cells[(y*cb.w)+x]
+	c.lastMain = rune(0)
+	if !dirty {
+		if c.currMain == rune(0) {
+			c.currMain = ' '
 		}
+		c.lastMain = c.currMain
+		c.lastComb = c.currComb
+		c.lastStyle = c.currStyle
 	}
 }