At 60 columns six type chips leave no room for the options, so when options would be dropped the types become one chip of the current type (a click moves on) if that shows more of them. The axis bar's key hints are short enough to fit at 60 columns.
- commit
- 79791ff4a67338cb2a756d5f370d566d40c111a4
- parent
- e34b72346c07acc6a4059598923fa1d273f3245f
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 2 | +- |
| M | internal/ | 12 | +++++++++- |
| M | internal/ | 35 | ++++++++++++++++++++++++------ |
@@ -93,5 +93,5 @@ func (e *chartEditor) askAxisEnd(m *Model, label, question, initial string, end
9393 }9494 9595 func (e *chartEditor) axisStatus(m *Model) string {96- return fitHints(m, []string{"N", "minimum", "X", "maximum", "L", "log scale", "G", "gridlines", "P", "legend", "Enter", "back", "Esc", "back"})96+ return fitHints(m, []string{"N", "min", "X", "max", "L", "log", "G", "grid", "P", "legend", "Esc", "back"})9797 }@@ -4,6 +4,8 @@ import (
44 "strings"55 "testing"66 7+ "github.com/charmbracelet/x/ansi"8+79 "github.com/FelineStateMachine/012/internal/sheet"810 )911 @@ -114,10 +116,18 @@ func TestChartEditorFitsNarrow(t *testing.T) {
114116 if ctx := line(m, contextLine); !strings.Contains(ctx, "Legend bottom") {115117 t.Errorf("axis bar clipped at 60 columns: %q", ctx)116118 }119+ if _, keys := m.overlay.Status(); !strings.Contains(keys, "grid") || !strings.Contains(keys, "legend") || ansi.StringWidth(keys) > 60 {120+ t.Errorf("axis keys at 60 columns: %q", ansi.Strip(keys))121+ }117122 press(t, m, "<esc>")118- for _, want := range []string{"Column", "Scatter"} {123+ for _, want := range []string{"◂ Column ▸", "Series in columns"} {119124 if ctx := line(m, contextLine); !strings.Contains(ctx, want) {120125 t.Errorf("main bar lacks %q at 60 columns: %q", want, ctx)121126 }122127 }128+ // Clicking the one type chip moves on to the next type.129+ leftClick(m, 3, contextLine)130+ if c := m.sheet.Charts()[0]; c.Type != sheet.ChartBar || !strings.Contains(line(m, contextLine), "◂ Bar ▸") {131+ t.Errorf("after a click: %v %q", c.Type, line(m, contextLine))132+ }123133 }@@ -169,16 +169,24 @@ func (e *chartEditor) chip(on bool, name, key string) editorPart {
169169 }170170 171171 // parts returns the bar's parts, and how many of them lead it: the172-// types, which are never dropped and have a wider gap after them.173-func (e *chartEditor) parts(m *Model) ([]editorPart, int) {172+// types, which are never dropped and have a wider gap after them. Compact,173+// the types are one chip of the current type, which a click moves on.174+func (e *chartEditor) parts(m *Model, compact bool) ([]editorPart, int) {174175 if e.axes {175176 return e.axisParts(m), 0176177 }177178 c := e.chart(m)178179 var parts []editorPart179180 for i, t := range sheet.ChartTypes {180- parts = append(parts, e.chip(t == c.Type, t.Title(), strconv.Itoa(i+1)))181+ switch {182+ case !compact:183+ parts = append(parts, e.chip(t == c.Type, t.Title(), strconv.Itoa(i+1)))184+ case t == c.Type:185+ next := (i+1)%len(sheet.ChartTypes) + 1186+ parts = append(parts, e.chip(true, "◂ "+t.Title()+" ▸", strconv.Itoa(next)))187+ }181188 }189+ lead := len(parts)182190 parts = append(parts, editorPart{text: m.th.Key.Render(c.Data.String()), key: editorRange})183191 switch {184192 case c.Type.Stackable():@@ -197,13 +205,26 @@ func (e *chartEditor) parts(m *Model) ([]editorPart, int) {
197205 return append(parts,198206 e.chip(true, series, editorSwitch),199207 e.chip(c.Header, header, editorHeader),200- e.chip(c.Labels, labels, editorLabels)), len(sheet.ChartTypes)208+ e.chip(c.Labels, labels, editorLabels)), lead201209 }202210 203-// spans lays the parts out on the context line, dropping options from the204-// right on narrow screens, and returns each part's x.211+// spans lays the parts out on the context line and returns each part's212+// x. On narrow screens the types become one chip when that leaves room213+// for more options, and options that still don't fit are dropped from the214+// right.205215 func (e *chartEditor) spans(m *Model) ([]editorPart, []int) {206- parts, lead := e.parts(m)216+ parts, xs := e.fit(m, false)217+ if all, _ := e.parts(m, false); len(parts) < len(all) && !e.axes {218+ if cp, cxs := e.fit(m, true); len(cp)+len(sheet.ChartTypes)-1 > len(parts) {219+ return cp, cxs220+ }221+ }222+ return parts, xs223+}224+225+// fit lays out the parts, dropping options from the right until they fit.226+func (e *chartEditor) fit(m *Model, compact bool) ([]editorPart, []int) {227+ parts, lead := e.parts(m, compact)207228 for {208229 xs := make([]int, len(parts))209230 x := 0