# HG changeset patch # User telesto # Date 1513829944 -3600 # Thu Dec 21 05:19:04 2017 +0100 # Node ID ab2f73aac14baa8439dee5abfe2ab5bfb1821f79 # Parent 52b3d1bdd2b3b3743e85009ca41173449ecfd4ab view.Cellbuffer: put code for testing if x,y is inside the view into a method diff --git a/cell.go b/cell.go --- a/cell.go +++ b/cell.go @@ -40,32 +40,35 @@ 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 @@ // 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 @@ // 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 @@ // 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 } }