Fixes file loading and adds trim command

This commit is contained in:
sloum 2021-03-15 20:50:13 -07:00
parent aa0a6928c1
commit 31d833b0d7
5 changed files with 57 additions and 4 deletions

View File

@ -20,7 +20,11 @@ func (c cell) String(width int, selected bool) string {
if selected {
mods += "\033[7m"
}
return fmt.Sprintf("%s%*.*s\033[0m", mods, width, width, c.mask)
format := "%s%*.*s\033[0m"
if c.kind == Text {
format = "%s%-*.*s\033[0m"
}
return fmt.Sprintf(format, mods, width, width, c.mask)
}
func (c *cell) Edit(row, col int) {

View File

@ -4,6 +4,8 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
)
@ -47,6 +49,7 @@ func writeSheet(f *os.File, sh sheet) {
}
func LoadFile(path string) {
path = ExpandedAbsFilepath(path)
fbytes, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprint(os.Stderr, "Unable to read file\n")
@ -85,7 +88,17 @@ func LoadFile(path string) {
s.cells[i][x] = c
}
}
// s.Recalculate()
wb.sheets = append(wb.sheets, s)
}
}
func ExpandedAbsFilepath(p string) string {
if strings.HasPrefix(p, "~/") {
usr, _ := user.Current()
homedir := usr.HomeDir
p = filepath.Join(homedir, p[2:])
}
path, _ := filepath.Abs(p)
return path
}

View File

@ -40,6 +40,9 @@ func runCommand(elems []string) {
} else {
WriteFile(wb.path)
}
case "trim":
wb.sheets[wb.sheet].TrimSheet()
fmt.Print("\033[2J") // Clear the screen
}
}

View File

@ -74,7 +74,6 @@ func (s sheet) CurrentValue(raw bool) string {
}
func (s *sheet) Recalculate() {
fmt.Println(s.cells)
for row, _ := range s.cells {
for col, _ := range s.cells[row] {
// TODO do this concurrently
@ -212,6 +211,39 @@ func (s *sheet) ZoomOut() {
s.zoom++ // Zooming out increases number of cols viewed
}
func (s *sheet) TrimSheet() {
for rowIndex := s.rows-1; rowIndex > 0; rowIndex-- {
remove := true
for _, cell := range s.cells[rowIndex] {
if cell.kind != Empty {
remove = false
break
}
}
if !remove {
break
}
s.cells = s.cells[:rowIndex]
s.rows--
}
for colIndex := s.cols-1; colIndex > 0; colIndex-- {
remove := true
for _, row := range s.cells {
if row[colIndex].kind != Empty {
remove = false
break
}
}
if !remove {
break
}
for i, _ := range s.cells {
s.cells[i] = s.cells[i][:len(s.cells[i])-1]
}
s.cols--
}
}
func makeSheet(name string) sheet {
if name == "" {
name = "Sheet1"

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"tildegit.org/sloum/tally/termios"
)
@ -98,7 +99,7 @@ func makeWorkbook(path string) workbook {
// path to the full path
cols, rows := termios.GetWindowSize()
sh := makeSheet("Sheet1")
wb = workbook{path, path, make([]sheet,1), 0, rows, cols}
wb = workbook{filepath.Base(path), path, make([]sheet,1), 0, rows, cols}
wb.sheets[0] = sh
return wb
}