       self.size.height - 1,
       max(0, Int(floor(Double(flattenedIndex) / Double(self.size.width))))
     )
-    let col = min(
-      self.size.width - 1,
-      max(0, flattenedIndex % self.size.width)
-    )
+    let col = min(self.size.width - 1, max(0, flattenedIndex % self.size.width))
 
     return Position(row: row, column: col)
   }
 
   func oneDimCellIndex(forRow row: Int, column: Int) -> Int {
-    return row * self.size.width + column
+    row * self.size.width + column
   }
 
   func oneDimCellIndex(forPosition position: Position) -> Int {
-    return position.row * self.size.width + position.column
+    position.row * self.size.width + position.column
   }
 
   func flatCharIndex(forPosition position: Position) -> Int {
-    return self.cells[position.row][position.column].flatCharIndex
+    self.cells[position.row][position.column].flatCharIndex
   }
 
   func firstPosition(fromFlatCharIndex index: Int) -> Position? {

    let column = position.column
    let row = self.cells[position.row]

    if row[column].string == wordSeparator {
      return column
    }

    if column + 1 == self.size.width {
      return column
    }

    for i in (column + 1)..<self.size.width {
      if row[i].string == wordSeparator {
        return max(i - 1, 0)
      }
    }

    return self.size.width - 1
  }

  func goto(_ position: Position) {
    self.cursorPosition = position
  }

  func scroll(
    region: Region,
    rows: Int,
    cols: Int
  ) {
    var start, stop, step: Int
    if rows > 0 {
      start = region.top;
      stop = region.bottom - rows + 1;
      step = 1;
    } else {
      start = region.bottom;
      stop = region.top - rows - 1;
      step = -1;
    }

    // copy cell data
    let rangeWithinRow = region.left...region.right
    for i in stride(from: start, to: stop, by: step) {
      self.cells[i].replaceSubrange(
        rangeWithinRow, with: self.cells[i + rows][rangeWithinRow]
      )
    }

    // clear cells in the emptied region,

}

final class UGrid: CustomStringConvertible, Codable {
  private(set) var cursorPosition = Position.zero
  private(set) var size = Size.zero
  private(set) var cells: [[UCell]] = []

  enum CodingKeys: String, CodingKey {
    case width
    case height
    case cells
  }

  init() {}

  init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
