1package headless23import (4 "fmt"5 "strconv"6 "strings"78 "src.rest/cat/012/internal/sheet"9)1011// FindChart finds a chart of s by its number, 1 for the first in the12// order the sheet keeps them, or by its title, ignoring case.13func FindChart(s *sheet.Sheet, which string) (int, error) {14 charts := s.Charts()15 if n, err := strconv.Atoi(strings.TrimSpace(which)); err == nil {16 if n < 1 || n > len(charts) {17 return 0, fmt.Errorf("%s has %d %s: chart %d isn't one", s.Name(), len(charts), plural(len(charts), "chart", "charts"), n)18 }19 return n - 1, nil20 }21 var titles []string22 for i, c := range charts {23 if strings.EqualFold(ChartTitle(c), which) {24 return i, nil25 }26 titles = append(titles, ChartTitle(c))27 }28 if len(titles) == 0 {29 return 0, fmt.Errorf("%s has no charts", s.Name())30 }31 return 0, fmt.Errorf("%s has no chart titled %q (charts: %s)", s.Name(), which, strings.Join(titles, ", "))32}3334// ChartTitle is a chart's title, or its type's name when it has none.35func ChartTitle(c sheet.Chart) string {36 if c.Title != "" {37 return c.Title38 }39 return c.Type.Title() + " chart"40}4142func plural(n int, one, many string) string {43 if n == 1 {44 return one45 }46 return many47}