cat / 012
clone

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

Derive the UI's format lists and choices from fileio's formats table

Dami committed a7587c6
The import description, the Download commands and menu, and the
choices of snapshotting a whole workbook or asking for a table now come
from Kind methods (Noun, About, MenuTitle, HoldsSheets, HasTables)
instead of switches on CSV, XLSX and SQLite. Every string is unchanged.
commit
a7587c6600841df793cc26628514f37be8628fe7
parent
44a8bd27038d20a07449f6b93aa1a3f9051a33cf
author
Dami <dami.etoile@gmail.com>
2 files changed, +32 -26
Minternal/ui/menubar.go7+----
Minternal/ui/transfer.go51++++++++++++++++++------------
M internal/ui/menubar.go +1 -6
M internal/ui/transfer.go +31 -20
@@ -63,31 +63,42 @@ const importTick = 100 * time.Millisecond
6363 6464 func init() {6565 register(&command{id: "file.import", title: "Import",66- desc: "Import a CSV, TSV, Excel, SQLite, Parquet or 1-2-3 file, replacing this sheet",66+ desc: "Import a " + importNouns() + " file, replacing this sheet",6767 run: (*Model).openImport})6868 for _, k := range fileio.Kinds() {6969 if !k.CanExport() {7070 continue7171 }7272 register(&command{73- id: "file.download." + strings.ToLower(k.String()),73+ id: downloadID(k),7474 title: "Download as " + k.String(),75- desc: downloadDesc(k),75+ desc: k.About(),7676 run: func(m *Model) tea.Cmd { return m.openDownload(k) },7777 })7878 }7979 }8080 81-func downloadDesc(k fileio.Kind) string {82- switch k {83- case fileio.CSV, fileio.TSV:84- return "Save the values as shown, as " + strings.ToLower(k.Label()) + " (" + k.Ext() + ")"85- case fileio.XLSX:86- return "Save as an Excel workbook (.xlsx) with formulas, formats and widths"87- case fileio.SQLite:88- return "Save the sheet, or the selection, as a table in a SQLite database"81+// downloadID is the command downloading in format k.82+func downloadID(k fileio.Kind) string { return "file.download." + strings.ToLower(k.String()) }83+84+// downloadItems is the File > Download menu: every format 012 exports.85+func downloadItems() []menuItem {86+ var items []menuItem87+ for _, k := range fileio.Kinds() {88+ if k.CanExport() {89+ items = append(items, menuItem{cmd: downloadID(k), title: k.MenuTitle()})90+ }91+ }92+ return items93+}94+95+// importNouns lists the formats 012 imports: "CSV, TSV, ... or 1-2-3".96+func importNouns() string {97+ var nouns []string98+ for _, k := range fileio.Kinds() {99+ nouns = append(nouns, k.Noun())89100 }90- return ""101+ return strings.Join(nouns[:len(nouns)-1], ", ") + " or " + nouns[len(nouns)-1]91102 }92103 93104 // Import sets a file to import when the program starts, e.g. from
@@ -434,9 +445,9 @@ func (m *Model) openDownload(k fileio.Kind) tea.Cmd {
434445 }435446 r := sheet.Rect{}436447 label := "Download as " + k.String() + ":"437- if k == fileio.SQLite && m.hasRange() {448+ if k.HasTables() && m.hasRange() {438449 r = m.selection()439- label = "Download " + r.String() + " as SQLite:"450+ label = "Download " + r.String() + " as " + k.String() + ":"440451 }441452 m.openText(label, m.displayBase()+k.Ext(), func(m *Model, text string) tea.Cmd {442453 if text == "" {
@@ -446,8 +457,8 @@ func (m *Model) openDownload(k fileio.Kind) tea.Cmd {
446457 if filepath.Ext(name) == "" {447458 name += k.Ext()448459 }449- if k == fileio.SQLite {450- m.openTableName(name, r)460+ if k.HasTables() {461+ m.openTableName(name, k, r)451462 return nil452463 }453464 return m.confirmReplace(filepath.Base(name)+" exists.", func(m *Model) tea.Cmd {
@@ -462,8 +473,8 @@ func exists(name string) bool {
462473 return err == nil463474 }464475 465-// openTableName asks for the table to write in a SQLite database.466-func (m *Model) openTableName(name string, r sheet.Rect) {476+// openTableName asks for the table to write in a database of kind k.477+func (m *Model) openTableName(name string, k fileio.Kind, r sheet.Rect) {467478 m.openText("Table in "+filepath.Base(name)+":", fileio.TableName(filepath.Base(m.displayBase())), func(m *Model, table string) tea.Cmd {468479 if table == "" {469480 return nil
@@ -480,7 +491,7 @@ func (m *Model) openTableName(name string, r sheet.Rect) {
480491 }481492 }482493 return m.confirmReplace("Table "+table+" exists in "+filepath.Base(name)+".", func(m *Model) tea.Cmd {483- return m.download(name, fileio.SQLite, r, table)494+ return m.download(name, k, r, table)484495 }, has)485496 })486497 }
@@ -505,7 +516,7 @@ func (m *Model) confirmReplace(msg string, do func(*Model) tea.Cmd, replaces boo
505516 // download snapshots the sheet and writes it in the background.506517 func (m *Model) download(name string, k fileio.Kind, r sheet.Rect, table string) tea.Cmd {507518 snap := fileio.Snap(m.sheet, r, filepath.Base(m.displayBase()))508- if k == fileio.XLSX && r == (sheet.Rect{}) {519+ if k.HoldsSheets() && r == (sheet.Rect{}) {509520 snap = fileio.SnapBook(m.sheet) // every sheet, as Sheets' .xlsx download510521 }511522 return func() tea.Msg {