Skip to content

Commit

Permalink
Correctly handle grid_line repeat parameter
Browse files Browse the repository at this point in the history
fixes #44
  • Loading branch information
hismailbulut committed Oct 22, 2023
1 parent 468a3f3 commit b357fe4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
15 changes: 8 additions & 7 deletions cmd/neoray/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,18 @@ func (grid *Grid) CellAt(row, col int) Cell {

// Safe alternative to CellAt. Returns empty cell if out of bounds.
func (grid *Grid) SafeCellAt(row, col int) Cell {
if row >= 0 && row < grid.rows && col >= 0 && col < grid.cols {
return grid.cells[row][col]
if !grid.IsInBounds(row, col) {
return Cell{}
}
return Cell{}
return grid.cells[row][col]
}

func (grid *Grid) IsInBounds(row, col int) bool {
return row >= 0 && row < grid.rows && col >= 0 && col < grid.cols
}

// Sets the cell in grid
// Sets the cell in grid, doesn't checks for bounds
func (grid *Grid) SetCell(row, col int, char rune, attribID int) {
if row < 0 || row >= grid.rows || col < 0 || col >= grid.cols {
return
}
grid.cells[row][col].char = char
grid.cells[row][col].attribID = attribID
grid.cells[row][col].needsDraw = true
Expand Down
2 changes: 1 addition & 1 deletion cmd/neoray/grid_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (manager *GridManager) grid_line(args []interface{}) {
hl_id = to_int(cell[1])
}
// third one is repeat count -optional
repeat := 0
repeat := 1
if len(cell) == 3 {
repeat = to_int(cell[2])
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/neoray/grid_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (manager *GridManager) CheckGridSize(grid *Grid, prevSize common.Vector2[in
// You can access the sorted array via gridManager.sortedGrids
// and don't call this function directly.
func (manager *GridManager) SortGrids() {
bench.Begin()()

This comment has been minimized.

Copy link
@hismailbulut

hismailbulut Oct 22, 2023

Author Owner

Doesn't have any relotions with this pr, unnecessary. I forget to delete it

// Resize sorted slice to length of the grids slice
if len(manager.grids) == 0 {
return
Expand Down Expand Up @@ -354,14 +355,14 @@ func (manager *GridManager) ClearGrid(id int) {
}
}

// Sets cells with the given parameters, and advances y to the next. If
// `repeat` is present, the cell should be repeated `repeat` times (including
// the first time). This function will not check the end of the row. And
// currently only used by neovim.
// Sets the cells at the (x, [y, y+repeat]) to the char and attr, advances y to the end
func (manager *GridManager) SetCell(id, x int, y *int, char rune, attribId, repeat int) {
grid, ok := manager.grids[id]
if ok {
for i := 0; i < common.Max(repeat, 1); i++ {
if !ok {
return
}
for i := 0; i < repeat; i++ {
if grid.IsInBounds(x, *y) {
grid.SetCell(x, *y, char, attribId)
*y++
}
Expand Down

0 comments on commit b357fe4

Please sign in to comment.