2015-03-24 02:12:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
// Cell
|
|
|
|
|
type Cell struct {
|
2018-09-01 01:27:34 +00:00
|
|
|
Value string
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Standard model
|
|
|
|
|
type StdModel struct {
|
2018-09-01 01:27:34 +00:00
|
|
|
Cells [][]Cell
|
|
|
|
|
dirty bool
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The dimensions of the model (height, width).
|
|
|
|
|
*/
|
|
|
|
|
func (sm *StdModel) Dimensions() (int, int) {
|
2018-09-01 01:27:34 +00:00
|
|
|
if len(sm.Cells) == 0 {
|
|
|
|
|
return 0, 0
|
|
|
|
|
} else {
|
|
|
|
|
return len(sm.Cells), len(sm.Cells[0])
|
|
|
|
|
}
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value of a cell.
|
|
|
|
|
*/
|
|
|
|
|
func (sm *StdModel) CellValue(r, c int) string {
|
2018-09-01 01:27:34 +00:00
|
|
|
rs, cs := sm.Dimensions()
|
|
|
|
|
if (r >= 0) && (c >= 0) && (r < rs) && (c < cs) {
|
|
|
|
|
return sm.Cells[r][c].Value
|
|
|
|
|
} else {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resize the model.
|
|
|
|
|
func (sm *StdModel) Resize(rs, cs int) {
|
2018-09-01 01:27:34 +00:00
|
|
|
oldRowCount := len(sm.Cells)
|
2015-03-24 02:12:40 +00:00
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
newRows := make([][]Cell, rs)
|
|
|
|
|
for r := range newRows {
|
|
|
|
|
newCols := make([]Cell, cs)
|
|
|
|
|
if r < oldRowCount {
|
|
|
|
|
copy(newCols, sm.Cells[r])
|
|
|
|
|
}
|
|
|
|
|
newRows[r] = newCols
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sm.Cells = newRows
|
|
|
|
|
sm.dirty = true
|
2015-03-24 02:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets the cell value
|
|
|
|
|
func (sm *StdModel) SetCellValue(r, c int, value string) {
|
2018-09-01 01:27:34 +00:00
|
|
|
rs, cs := sm.Dimensions()
|
|
|
|
|
if (r >= 0) && (c >= 0) && (r < rs) && (c < cs) {
|
|
|
|
|
sm.Cells[r][c].Value = value
|
|
|
|
|
}
|
|
|
|
|
sm.dirty = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// appendStr appends the model with the given row
|
|
|
|
|
func (sm *StdModel) appendStr(row []string) {
|
|
|
|
|
if len(sm.Cells) == 0 {
|
|
|
|
|
cells := sm.strSliceToCell(row, len(row))
|
|
|
|
|
sm.Cells = [][]Cell{ cells }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cols := len(sm.Cells[0])
|
|
|
|
|
if len(row) > cols {
|
|
|
|
|
sm.Resize(len(sm.Cells), len(row))
|
|
|
|
|
cols = len(sm.Cells[0])
|
|
|
|
|
}
|
|
|
|
|
cells := sm.strSliceToCell(row, cols)
|
|
|
|
|
sm.Cells = append(sm.Cells, cells)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *StdModel) strSliceToCell(row []string, targetRowLen int) []Cell {
|
|
|
|
|
cs := make([]Cell, targetRowLen)
|
|
|
|
|
for i, c := range row {
|
|
|
|
|
if i < targetRowLen {
|
|
|
|
|
cs[i].Value = c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sm *StdModel) IsDirty() bool {
|
|
|
|
|
return sm.dirty
|
|
|
|
|
}
|