cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / headless / results.go
results.go 3.1K
1package headless23import (4 "bytes"5 "encoding/json"6 "os"78 "src.rest/cat/012/internal/diff"9 "src.rest/cat/012/internal/nuon"10)1112// The results commands write with --format json or nuon, and the MCP13// tools return. Each is a stable schema (docs/reference/json.md): fields14// are only added, never renamed or removed.1516// Change is one change as 012 diff --format json lists it: what kind17// of thing changed (cell, sheet, region, name, macro or a layout18// field), on which sheet, which item, which field of it, and its old19// and new values, null where it wasn't or isn't there.20type Change struct {21 Kind string `json:"kind"`22 Sheet string `json:"sheet"`23 Item string `json:"item"`24 Field string `json:"field"`25 Old json.RawMessage `json:"old"`26 New json.RawMessage `json:"new"`27}2829// Changes are diff's changes as records.30func Changes(changes []diff.Change) []Change {31 out := make([]Change, 0, len(changes))32 for _, c := range changes {33 out = append(out, Change{Kind: c.Kind, Sheet: c.Sheet, Item: c.Item, Field: c.Field,34 Old: nuon.AppendJSON(nil, c.Old), New: nuon.AppendJSON(nil, c.New)})35 }36 return out37}3839// SetResult is what 012 set writes: the file, whether it was saved (not40// with --dry-run), what changed, and the warnings of entries a rule41// marks invalid.42type SetResult struct {43 File string `json:"file"`44 Saved bool `json:"saved"`45 Changes []Change `json:"changes"`46 Warnings []string `json:"warnings"`47}4849// ProblemRecord is a formula showing an error.50type ProblemRecord struct {51 Cell string `json:"cell"` // with its sheet: Q3!B752 Sheet string `json:"sheet"`53 Addr string `json:"addr"`54 Value string `json:"value"` // #DIV/0!55 Why string `json:"why"`56 JEV bool `json:"jev"` // a JEV function's, answered with --jev57}5859// Problems are problems as records.60func Problems(ps []Problem) []ProblemRecord {61 out := make([]ProblemRecord, 0, len(ps))62 for _, p := range ps {63 out = append(out, ProblemRecord{Cell: p.At(), Sheet: p.Sheet, Addr: p.Addr.String(), Value: p.Value, Why: p.Why, JEV: p.JEV})64 }65 return out66}6768// RecalcResult is what 012 recalc writes.69type RecalcResult struct {70 File string `json:"file"`71 Errors []ProblemRecord `json:"errors"`72 Circular bool `json:"circular"`73 // NotebookFailures are the notebook cells --notebooks couldn't run.74 NotebookFailures []string `json:"notebook_failures"`75}7677// ExportResult is what 012 export --report writes.78type ExportResult struct {79 File string `json:"file"`80 Format string `json:"format"`81 Rows int `json:"rows"`82 Notes []string `json:"notes"`83}8485// Changes compares the workbook as it is with its file as it was86// opened (nothing, for a new file), before it's saved.87func (f *File) Changes() ([]diff.Change, error) {88 var old []byte89 if !f.New {90 var err error91 if old, err = os.ReadFile(f.Path); err != nil {92 return nil, err93 }94 }95 var now bytes.Buffer96 if err := f.Book.Write(&now); err != nil {97 return nil, err98 }99 a, err := diff.Read(old)100 if err != nil {101 return nil, err102 }103 b, err := diff.Read(now.Bytes())104 if err != nil {105 return nil, err106 }107 return diff.Compare(a, b), nil108}