tally/main.go

115 lines
1.9 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"tildegit.org/sloum/spreadsheet/termios"
)
const (
Empty int = iota
Text
Number
Expr
Bold
Italic
)
var wb workbook
var reAddr *regexp.Regexp = regexp.MustCompile(`^[A-Z][0-9]+$`)
var reAddrRange *regexp.Regexp = regexp.MustCompile(`^[A-Z][0-9]+:[A-Z][0-9]+$`)
type point struct {
row int
col int
}
func runCommand(elems []string) {
}
func Getch() rune {
reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune()
if err != nil {
return 0
}
return char
}
func GetLine(prefix string) (string, error) {
defer termios.SetCharMode()
termios.SetLineMode()
reader := bufio.NewReader(os.Stdin)
fmt.Print(prefix)
text, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return text[:len(text)-1], nil
}
func DigitCount(num int) int {
counter := 0
for ; num > 0; num /= 10 {
counter++
}
return counter
}
func GetCell(p point) (cell, error) {
if p.row > wb.sheets[wb.sheet].rows || p.col > wb.sheets[wb.sheet].cols {
return cell{}, fmt.Errorf("Invalid reference")
}
return wb.sheets[wb.sheet].cells[p.row][p.col], nil
}
func Addr2Point(addr string) point {
p := point{}
p.col = int(addr[0]) - 65
p.row, _ = strconv.Atoi(addr[1:])
p.row--
return p
}
func Point2Addr(p point) string {
var s strings.Builder
s.WriteRune(rune(p.col) + 65)
s.WriteString(strconv.Itoa(p.row+1))
return s.String()
}
func ApplyPointDiff(p1, p2 point) point {
return point{row: p2.row - p1.row, col: p2.col - p1.col}
}
func IsAddr(addr string) bool {
return reAddr.MatchString(addr)
}
func IsRange(addr string) bool {
return reAddrRange.MatchString(addr)
}
func IsFunc(val string) bool {
switch strings.ToUpper(val) {
case "+", "-", "/", "*", "^", "MIN", "MAX", "SQRT",
"ROUND", "SUBSTR", "UPPER", "LOWER", "SUM":
return true
default:
return false
}
}
func main() {
wb = makeWorkbook("test.qsh")
wb.Run()
}