]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: Add FormatLog method to Episode/EpisodeList/EpisodeRange
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:34:16 +0000 (13:34 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:34:16 +0000 (13:34 -0300)
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
misc/episodelist.go
misc/episoderange.go

index 2b9f1bd3651a303e85e87603985869348d82380d..0530f55874f7e1b5f85644193aaf57041498bad4 100644 (file)
@@ -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 {
index 91ba7cc1fddc4f291333b1a45b5656696146578e..9a93e17da72fb30939ec80e211f42731ef2cd66b 100644 (file)
@@ -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 {
index d78bc0223f8fdff3a56ede80a6906cdfd8f8b35a..fbc5cd35d6e98977568370f11ce69582ea6be0bb 100644 (file)
@@ -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