Files
minstrel/internal/library/tracknumber.go
T
bvandeusenandClaude Opus 5.5 7e6228c13f
release / govulncheck (push) Successful in 20s
release / web (push) Successful in 1m20s
release / go (push) Successful in 1m37s
release / integration (push) Successful in 5m15s
release / android (push) Successful in 5m26s
release / Build signed APK (releases and dev) (push) Successful in 5m45s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 16s
release / Verify release artifacts (tag releases only) (push) Skipped
docs: library comments say what the scanner and resolver do, not how they came to be (#6277)
Rewrites the internal/library comment blocks to rule 32. Task, milestone,
decision and rule ids are gone; the incidents behind them are restated as
the constraint they left (why a file is deleted before its row, why the
missing mark is a mark, why duplicates block on duration). Counts and
album names from one library are dropped from production comments; test
comments keep the names their fixtures use. The tagReadVersion log keeps
each version's before and after, without the measurements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:27:34 -04:00

41 lines
974 B
Go

package library
import (
"strconv"
"strings"
"github.com/dhowden/tag"
)
// ReadTrackAndDisc reads a file's track and disc numbers. dhowden/tag reads a
// Vorbis TRACKNUMBER or DISCNUMBER only as a bare number, but taggers also
// write "3/20" there (as Minstrel's own importer once did), and that
// reads as 0. For Vorbis the number before the slash is taken instead.
func ReadTrackAndDisc(m tag.Metadata) (track, disc int) {
track, _ = m.Track()
disc, _ = m.Disc()
if m.Format() != tag.VORBIS {
return track, disc
}
raw := m.Raw()
if track == 0 {
track = leadingNumber(raw["tracknumber"])
}
if disc == 0 {
disc = leadingNumber(raw["discnumber"])
}
return track, disc
}
// leadingNumber is the number in a tag value of "3" or "3/20"; 0 for
// anything else.
func leadingNumber(v any) int {
s, _ := v.(string)
before, _, _ := strings.Cut(s, "/")
n, err := strconv.Atoi(strings.TrimSpace(before))
if err != nil || n < 0 {
return 0
}
return n
}