"fmt"
"math"
"strconv"
+ "strings"
)
type EpisodeContainer interface {
type Formatter interface {
// Returns a string where the number portion is 0-padded to fit 'width' digits
Format(width int) string
+
+ // Returns a string where the number portion is 0-padded to be the same length
+ // as max.
+ FormatLog(max int) string
}
type EpisodeType int
type Episode struct {
Type EpisodeType
Number int
+// returns how many digits are needed to represent this int
+func scale(i int) int {
+ return 1 + int(math.Floor(math.Log10(float64(i))))
}
// Converts the Episode into AniDB API episode format.
if ep == nil {
return 1
}
- return 1 + int(math.Floor(math.Log10(float64(ep.Number))))
+ return scale(ep.Number)
}
// Returns true if ec is an Episode and is identical to this episode,
return fmt.Sprintf("%s%0"+strconv.Itoa(width)+"d", ep.Type, ep.Number)
}
+func (ep *Episode) FormatLog(max int) string {
+ return ep.Format(scale(max))
+}
+
// Parses a string in the usual AniDB API episode format and converts into
// an Episode.
func ParseEpisode(s string) *Episode {
return strings.Join(parts, ",")
}
+// Formats the list according to the number of digits of
+// the count for its type, given in the EpisodeCount.
+func (el EpisodeList) FormatLog(ec EpisodeCount) string {
+ parts := make([]string, len(el))
+ for i, er := range el {
+ switch er.Type {
+ case EpisodeTypeRegular:
+ parts[i] = er.FormatLog(ec.RegularCount)
+ case EpisodeTypeSpecial:
+ parts[i] = er.FormatLog(ec.SpecialCount)
+ case EpisodeTypeCredits:
+ parts[i] = er.FormatLog(ec.CreditsCount)
+ case EpisodeTypeOther:
+ parts[i] = er.FormatLog(ec.OtherCount)
+ case EpisodeTypeTrailer:
+ parts[i] = er.FormatLog(ec.TrailerCount)
+ case EpisodeTypeParody:
+ parts[i] = er.FormatLog(ec.ParodyCount)
+ default:
+ parts[i] = er.Format(er.scale())
+ }
+ }
+
+ return strings.Join(parts, ",")
+}
+
// Returns true if any of the contained EpisodeRanges contain the
// given EpisodeContainer.
func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
return fmt.Sprintf("%s-%s", er.Start.Format(width), er.End.Format(width))
}
+func (er *EpisodeRange) FormatLog(max int) string {
+ return er.Format(scale(max))
+}
+
func (er *EpisodeRange) scale() int {
if er == nil {
return 1