cat / 012
clone

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

main
012 / internal / ui / alignfmt.go
alignfmt.go 2.4K
1package ui23import (4 "strings"56 tea "charm.land/bubbletea/v2"78 "src.rest/cat/012/internal/sheet"9 "src.rest/cat/012/internal/ui/picker"10)1112// Format > Alignment's vertical alignments, and the color Format >13// Borders draws with, which it keeps as it keeps the line style.1415var valignments = []struct {16 id, title, desc string17 v sheet.VAlign18}{19 {"format.valign_top", "Align top", "Put text at the top of a tall row", sheet.VAlignTop},20 {"format.valign_middle", "Align middle", "Center text between the top and bottom of a tall row", sheet.VAlignMiddle},21 {"format.valign_bottom", "Align bottom", "Put text at the bottom of a tall row, as Sheets does unless told", sheet.VAlignBottom},22}2324func init() {25 for _, va := range valignments {26 v, title := va.v, va.title27 register(&command{id: va.id, title: title, desc: va.desc, edits: (*Model).selection, keepsSpills: true,28 checked: func(m *Model) bool { return m.sheet.CellStyle(m.cur).VAlign == v },29 run: func(m *Model) tea.Cmd {30 r := m.selection()31 m.sheet.Batch(sheet.Change{Label: strings.ToLower(title) + " " + r.String(), Focus: r}, func() error {32 m.sheet.SetStyle(r, func(s *sheet.Style) { s.VAlign = v })33 return nil34 })35 m.formatted("Aligned to the " + strings.ToLower(strings.TrimPrefix(title, "Align ")))36 return nil37 }})38 }39 register(&command{id: "format.border_color", title: "Border color",40 desc: "Choose the color borders draw with: the text's, or one of the terminal's colors",41 run: func(m *Model) tea.Cmd { m.openBorderColors(); return nil }})42}4344// openBorderColors lists the colors borders may draw with, the current45// one highlighted.46func (m *Model) openBorderColors() {47 var items []picker.Item48 for _, c := range sheet.Colors() {49 title, desc := c.Title(), "Draw borders in "+c.String()50 if c == sheet.ColorNone {51 title, desc = "Automatic", "Draw borders in the text's color, as Sheets draws them black"52 }53 items = append(items, picker.Item{Title: title, Name: len(title), Desc: desc, Swatch: m.borderRole(c).Render("━━━"), Pick: func() tea.Cmd {54 m.closeOverlay()55 m.borderColor = c56 m.note = "Borders draw in " + strings.ToLower(title) + " now"57 if c == sheet.ColorNone {58 m.note = "Borders draw in the text's color now"59 }60 return nil61 }})62 }63 p := m.newPicker("Border color", "Type a color", 40, items)64 p.Action = "choose"65 p.Answers = true66 p.Sel = int(m.borderColor)67 m.openOverlay(p)68}