The manager's keys act on the model through macrosHost (nine methods). It stays in package ui: it's a few keys over a picker, and what they do is the model's macro machinery.
- commit
- 31fec52c859b63a02001e3d08c49025b9814d412
- parent
- 829df5d7e91ef0fe26d48fc5b73e7b0ff5c4a632
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 1 | + |
| M | internal/ | 38 | ++++++++++++++++++++++-------- |
@@ -35,6 +35,7 @@ func (m *Model) styles() *theme.Theme { return &m.th }
3535 func (m *Model) size() (width, height int) { return m.width, m.height }3636 func (m *Model) available(id string) bool { return commands[id].available(m) }3737 func (m *Model) sheetShown() *sheet.Sheet { return m.sheet }38+func (m *Model) canEditScripts() bool { return m.macros.editor }3839 3940 // syncChanged makes the modified flag follow the undo history.4041 func (m *Model) syncChanged() { m.changed = m.sheet.StateID() != m.saved }@@ -14,6 +14,7 @@ import (
1414 "github.com/FelineStateMachine/012/internal/macro"1515 "github.com/FelineStateMachine/012/internal/sheet"1616 "github.com/FelineStateMachine/012/internal/ui/picker"17+ "github.com/FelineStateMachine/012/internal/ui/theme"1718 )1819 1920 // Managing macros, as Sheets' Extensions > Macros > Manage macros: a@@ -22,9 +23,26 @@ import (
2223 // Ctrl+D deletes it. The first row writes a new one. Questions go on the2324 // context line; the picker opens again after each.2425 26+// macrosHost is what the macro manager acts on. The model implements it.27+// The manager stays in package ui: it is a few keys over a picker, and28+// what they do (run, rename, edit a script) is the model's macro29+// machinery.30+type macrosHost interface {31+ styles() *theme.Theme32+ size() (width, height int)33+ book() *sheet.Workbook34+ closeOverlay()35+ // canEditScripts reports whether this session may open an editor.36+ canEditScripts() bool37+ renameMacro(mc sheet.Macro)38+ setMacroShortcut(mc sheet.Macro)39+ editScript(name, src string, isNew bool) tea.Cmd40+ macroManageItems() []picker.Item41+}42+2543 // macrosPicker is the picker of saved macros with its extra keys.2644 type macrosPicker struct {27- m *Model // the model it acts on45+ m macrosHost // the model, through what the manager needs of it2846 *picker.Picker2947 msg string // feedback on the last action, e.g. a deletion3048 }@@ -33,7 +51,7 @@ const writeMacroTitle = "+ Write a macro"
3351 3452 // openMacros opens the manager with the macro named sel highlighted.3553 func (m *Model) openMacros(sel string) {36- p := m.newPicker("Macros", "Type a macro's name", 72, macroManageItems(m))54+ p := m.newPicker("Macros", "Type a macro's name", 72, m.macroManageItems())3755 p.Action = "run"3856 for i, pm := range p.Shown() {3957 if strings.EqualFold(pm.Item.Title, sel) {@@ -44,7 +62,7 @@ func (m *Model) openMacros(sel string) {
4462 }4563 4664 // macroManageItems lists "Write a macro" and then every macro.47-func macroManageItems(m *Model) []picker.Item {65+func (m *Model) macroManageItems() []picker.Item {4866 var items []picker.Item4967 if m.macros.editor {5068 items = append(items, picker.Item{@@ -70,7 +88,7 @@ func macroManageItems(m *Model) []picker.Item {
7088 }7189 7290 // current is the macro highlighted, if any.73-func (p *macrosPicker) current(m *Model) (sheet.Macro, bool) {91+func (p *macrosPicker) current(m macrosHost) (sheet.Macro, bool) {7492 if p.Picker.Sel >= len(p.Shown()) || p.Shown()[p.Picker.Sel].Item.Title == writeMacroTitle {7593 return sheet.Macro{}, false7694 }@@ -97,7 +115,7 @@ func (p *macrosPicker) Key(k tea.KeyPressMsg) tea.Cmd {
97115 m.closeOverlay()98116 m.setMacroShortcut(mc)99117 case "f4":100- if !m.macros.editor {118+ if !m.canEditScripts() {101119 p.msg = "No editor in this session"102120 return nil103121 }@@ -106,7 +124,7 @@ func (p *macrosPicker) Key(k tea.KeyPressMsg) tea.Cmd {
106124 case "ctrl+d":107125 m.book().DeleteMacro(mc.Name)108126 p.msg = "Deleted " + mc.Name + "; Ctrl+Z brings it back"109- p.Items = macroManageItems(m)127+ p.Items = m.macroManageItems()110128 sel := p.Picker.Sel111129 p.Changed()112130 p.Picker.Sel = max(min(sel, len(p.Shown())-1), 0)@@ -117,16 +135,16 @@ func (p *macrosPicker) Key(k tea.KeyPressMsg) tea.Cmd {
117135 func (p *macrosPicker) Status() (string, string) {118136 m := p.m119137 pairs := []string{"Enter", "run", "F2", "rename", "F3", "shortcut", "F4", "edit", "Ctrl+D", "delete", "Esc", "close"}120- if !m.macros.editor {138+ if !m.canEditScripts() {121139 pairs = slices.Delete(pairs, 6, 8)122140 }123- if m.width < 100 {141+ if width, _ := m.size(); width < 100 {124142 pairs = slices.DeleteFunc(pairs[:len(pairs)-2], func(s string) bool { return s == "shortcut" })125143 pairs = slices.Insert(pairs, slices.Index(pairs, "F3")+1, "key")126144 }127- keys := m.th.KeyHints(pairs...)145+ keys := m.styles().KeyHints(pairs...)128146 if _, ok := p.current(m); !ok {129- keys = m.th.KeyHints("Enter", "write", "Esc", "close")147+ keys = m.styles().KeyHints("Enter", "write", "Esc", "close")130148 }131149 if p.msg != "" {132150 return p.msg, keys