Last active
August 29, 2015 14:22
-
-
Save orian/0d5ebbd20613b0ea07c5 to your computer and use it in GitHub Desktop.
pattern name generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"path" | |
) | |
type StringGenerator interface { | |
Next() bool | |
Get() string | |
} | |
type NameGen struct { | |
basePath string | |
baseFile string | |
start int // [start, end) | |
end int | |
num int | |
withoutIdx bool | |
current string | |
} | |
func NewNameGen(base, file string, start, num int, withoutIdx bool) *NameGen { | |
return &NameGen{base, file, start, start + num, num, withoutIdx, ""} | |
} | |
func (n *NameGen) Next() bool { | |
if n.withoutIdx { | |
n.current = path.Join(n.basePath, fmt.Sprintf("%s.jpeg", n.baseFile)) | |
n.withoutIdx = false | |
return true | |
} | |
if n.start < n.end { | |
n.current = path.Join(n.basePath, fmt.Sprintf("%s_%d.jpeg", n.baseFile, n.start)) | |
n.start = n.start + 1 | |
return true | |
} | |
return false | |
} | |
func (n *NameGen) Has() bool { | |
return len(n.current) > 0 | |
} | |
func (n *NameGen) Get() string { | |
return n.current | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
var g StringGenerator = NewNameGen("/usr", "name", 0, 5, true) | |
for g.Next() { | |
fmt.Println(g.Get()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment