From f403c8a5d3da9d42f4b3933a69f0c9caef73eb0c Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Tue, 16 Jul 2013 13:34:16 -0300 Subject: [PATCH] misc: Add FormatLog method to Episode/EpisodeList/EpisodeRange EpisodeList.FormatLog takes an EpisodeCount instead of an int; and each subrange is formatted according to the log of the count for its own type. --- misc/episode.go | 14 +++++++++++++- misc/episodelist.go | 26 ++++++++++++++++++++++++++ misc/episoderange.go | 4 ++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/misc/episode.go b/misc/episode.go index 2b9f1bd..0530f55 100644 --- a/misc/episode.go +++ b/misc/episode.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "strconv" + "strings" ) type EpisodeContainer interface { @@ -14,6 +15,10 @@ 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 @@ -68,6 +73,9 @@ func (et EpisodeType) String() string { 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. @@ -80,7 +88,7 @@ func (ep *Episode) scale() int { 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, @@ -102,6 +110,10 @@ func (ep *Episode) Format(width int) string { 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 { diff --git a/misc/episodelist.go b/misc/episodelist.go index 91ba7cc..9a93e17 100644 --- a/misc/episodelist.go +++ b/misc/episodelist.go @@ -40,6 +40,32 @@ func (el EpisodeList) String() string { 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 { diff --git a/misc/episoderange.go b/misc/episoderange.go index d78bc02..fbc5cd3 100644 --- a/misc/episoderange.go +++ b/misc/episoderange.go @@ -28,6 +28,10 @@ func (er *EpisodeRange) Format(width int) string { 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 -- 2.44.0