diff options
-rw-r--r-- | cmd/mfmt/main.go | 35 | ||||
-rw-r--r-- | mintage/parser.go | 33 | ||||
-rw-r--r-- | mintage/parser_test.go | 17 |
3 files changed, 40 insertions, 45 deletions
diff --git a/cmd/mfmt/main.go b/cmd/mfmt/main.go index 5ff2a0a..3144372 100644 --- a/cmd/mfmt/main.go +++ b/cmd/mfmt/main.go @@ -5,7 +5,6 @@ import ( "io" "os" "path/filepath" - "strings" "unsafe" "git.thomasvoss.com/euro-cash.eu/mintage" @@ -77,24 +76,20 @@ func mfmt(path string, in io.Reader, out io.Writer) error { func formatSection(out io.Writer, rows []mintage.Row) { var ( - year string longestMM int longestNum [cols]int ) - for _, row := range rows { - s, mm, ok := strings.Cut(row.Label, "\u00A0") - if ok { - n := len(mm) - if s != year { - n++ - } - longestMM = max(longestMM, n) + for i, row := range rows { + n := len(row.Mintmark) + if n != 0 && i != 0 && row.Year != rows[i-1].Year { + n++ } - year = s - for i, col := range row.Cols { + longestMM = max(longestMM, n) + + for j, col := range row.Cols { n := intlen(col) - longestNum[i] = max(longestNum[i], n+(n-1)/3) + longestNum[j] = max(longestNum[j], n+(n-1)/3) } } @@ -103,21 +98,15 @@ func formatSection(out io.Writer, rows []mintage.Row) { extraSpace = 2 } - year = "" - for i, row := range rows { var label string - - s, mm, ok := strings.Cut(row.Label, "\u00A0") - if ok { - if s != year { - label = mm + "*: " + if row.Mintmark != "" { + if i != 0 && row.Year != rows[i-1].Year { + label = row.Mintmark + "*: " } else { - label = mm + ": " + label = row.Mintmark + ": " } } - year = s - fmt.Fprintf(out, "%-*s", longestMM+extraSpace, label) for j, n := range row.Cols { if j != 0 { diff --git a/mintage/parser.go b/mintage/parser.go index 242a3bb..4c5e6f9 100644 --- a/mintage/parser.go +++ b/mintage/parser.go @@ -27,8 +27,9 @@ func (e SyntaxError) Error() string { } type Row struct { - Label string - Cols [8]int + Year int + Mintmark string + Cols [8]int } type Set struct { @@ -36,11 +37,17 @@ type Set struct { Circ, BU, Proof []Row } +func (r Row) Label() string { + if r.Mintmark != "" { + return fmt.Sprintf("%d %s", r.Year, r.Mintmark) + } + return strconv.Itoa(r.Year) +} + func Parse(reader io.Reader, file string) (Set, error) { var ( data Set // Our data struct slice *[]Row // Where to append mintages - year int // The current year we are at ) scanner := bufio.NewScanner(reader) @@ -86,8 +93,6 @@ func Parse(reader io.Reader, file string) (Set, error) { } data.StartYear, _ = strconv.Atoi(arg) } - - year = data.StartYear - 1 case isLabel(tokens[0]): n := len(tokens[0]) if n > 2 && tokens[0][n-2] == '*' { @@ -140,16 +145,14 @@ func Parse(reader io.Reader, file string) (Set, error) { } } - var row Row - switch { - case mintmark.s == "": - year++ - row.Label = strconv.Itoa(year) - case mintmark.star: - year++ - fallthrough - default: - row.Label = fmt.Sprintf("%d %s", year, mintmark.s) + row := Row{Mintmark: mintmark.s} + if len(*slice) == 0 { + row.Year = data.StartYear + } else { + row.Year = (*slice)[len(*slice)-1].Year + if row.Mintmark == "" || mintmark.star { + row.Year++ + } } for i, tok := range tokens { diff --git a/mintage/parser_test.go b/mintage/parser_test.go index 0a67685..dd78c71 100644 --- a/mintage/parser_test.go +++ b/mintage/parser_test.go @@ -128,14 +128,17 @@ func TestParserMintmarks(t *testing.T) { } } - if data.Circ[0].Label != "2020" { - t.Fatalf(`Expected data.Circ[0].Label="2020"; got %s`, data.Circ[0].Label) - } - if data.Circ[1].Label != "2021\u00A0KNM" { - t.Fatalf(`Expected data.Circ[1].Label="2021 KNM"; got %s`, data.Circ[1].Label) + for i, y := range [...]int{2020, 2021, 2021} { + if data.Circ[i].Year != y { + t.Fatalf("Expected data.Circ[%d].Year=%d; got %d", + i, y, data.Circ[i].Year) + } } - if data.Circ[2].Label != "2021\u00A0MdP" { - t.Fatalf(`Expected data.Circ[2].Label="2021 MdP"; got %s`, data.Circ[2].Label) + for i, s := range [...]string{"", "KNM", "MdP"} { + if data.Circ[i].Mintmark != s { + t.Fatalf(`Expected data.Circ[%d].Mintmark="%s"; got "%s"`, + i, s, data.Circ[i].Mintmark) + } } } |