Skip to content

Commit

Permalink
#14 - Added option to list templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomad-software committed Apr 17, 2021
1 parent c4785b3 commit ec8784f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

[![Go report card](https://goreportcard.com/badge/github.com/nomad-software/meme)](https://goreportcard.com/report/github.com/nomad-software/meme)

**Requires Go v1.16+ to compile.**

---

![Am i the only one around here?](http://i.imgur.com/WP1TAzg.png)
Expand Down
44 changes: 23 additions & 21 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
imageIds []string
ImageIds []string
)

// Initialise the package.
Expand All @@ -23,24 +23,25 @@ func init() {

for _, image := range images {
id := strings.TrimSuffix(filepath.Base(image.Name()), data.ImageExtension)
imageIds = append(imageIds, id)
ImageIds = append(ImageIds, id)
}

sort.Sort(sort.StringSlice(imageIds))
sort.Strings(ImageIds)
}

// Options holds the options passed on the command line.
type Options struct {
Anim bool
Bottom string
ClientID string
Help bool
Image string
ImageType string
OutName string
Shake bool
Top string
Trigger bool
Gif bool
Bottom string
ClientID string
Help bool
Image string
ImageType string
OutName string
Shake bool
Top string
Trigger bool
ListTemplates bool
}

// ParseOptions parses the command line options.
Expand All @@ -50,13 +51,14 @@ func ParseOptions() Options {

flag.BoolVar(&opt.Help, "h", false, "Show help.\n")
flag.BoolVar(&opt.Help, "help", false, "Show help.\n")
flag.StringVar(&opt.ClientID, "cid", "", "The client id of an application registered with imgur.com.\n If specified, the new meme will be uploaded to imgur.com.\n (See README for full details.)\n")
flag.StringVar(&opt.Image, "i", "", "A built-in template, a URL or the path to a local file.\n You can also use '-' to read an image from stdin.\n")
flag.StringVar(&opt.OutName, "o", "", "The optional name of the output file.\n If omitted, a temporary file will be created.\n")
flag.StringVar(&opt.ClientID, "cid", "", "The client id of an application registered with imgur.com.\nIf specified, the new meme will be uploaded to imgur.com.\n(See README for full details.)\n")
flag.StringVar(&opt.Image, "i", "", "A built-in template, a URL or the path to a local file.\nYou can also use '-' to read an image from stdin.\n")
flag.StringVar(&opt.OutName, "o", "", "The optional name of the output file.\nIf omitted, a temporary file will be created.\n")
flag.StringVar(&text, "t", "", "The meme text. Separate the top and bottom banners using a pipe '|'.\n")
flag.BoolVar(&opt.Anim, "gif", false, "Gif animations will be preserved and the output will be a gif.\n Does nothing for other image types.\n")
flag.BoolVar(&opt.Gif, "gif", false, "Gif animations will be preserved and the output will be a gif.\nDoes nothing for other image types.\n")
flag.BoolVar(&opt.Shake, "shake", false, "Shake the image to intensify it. Always outputs a gif.\n")
flag.BoolVar(&opt.Trigger, "trigger", false, "Shake the image and add a triggered banner. Always outputs a gif.\n")
flag.BoolVar(&opt.ListTemplates, "list-templates", false, "List all of the built in templates.\n")
flag.Parse()

parsed := strings.Split(text, "|")
Expand All @@ -78,13 +80,13 @@ func (opt *Options) Valid() bool {
output.Error("An image is required")
}

if !(opt.Anim || opt.Trigger || opt.Shake) && opt.OutName != "" {
if !(opt.Gif || opt.Trigger || opt.Shake) && opt.OutName != "" {
if !strings.HasSuffix(strings.ToLower(opt.OutName), ".png") {
output.Error("The output file name must have the suffix of .png")
}
}

if (opt.Anim || opt.Trigger || opt.Shake) && opt.OutName != "" {
if (opt.Gif || opt.Trigger || opt.Shake) && opt.OutName != "" {
if !strings.HasSuffix(strings.ToLower(opt.OutName), ".gif") {
output.Error("The output file name must have the suffix of .gif")
}
Expand All @@ -108,15 +110,15 @@ func (opt *Options) PrintUsage() {

fmt.Println(" Templates")
fmt.Println("")
for x, name := range imageIds {
for x, name := range ImageIds {
if ((x + 1) % 2) == 0 {
fmt.Fprintln(output.Stdout, color.CyanString("%s", name))
} else {
fmt.Fprint(output.Stdout, color.CyanString(" %-30s", name))
}
}

if len(imageIds)%3 != 0 {
if len(ImageIds)%3 != 0 {
fmt.Println("")
}

Expand Down
2 changes: 1 addition & 1 deletion image/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RenderImage(opt cli.Options, st stream.Stream) stream.Stream {
st = shake(st)
}

if opt.Trigger || opt.Shake || (opt.Anim && st.IsGif()) {
if opt.Trigger || opt.Shake || (opt.Gif && st.IsGif()) {
st = renderGif(opt, st)
} else {
st = renderImage(opt, st)
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"

_ "github.com/jteeuwen/go-bindata"
"github.com/fatih/color"
"github.com/nomad-software/meme/cli"
"github.com/nomad-software/meme/image"
"github.com/nomad-software/meme/output"
Expand All @@ -17,6 +18,11 @@ func main() {
if opt.Help {
opt.PrintUsage()

} else if opt.ListTemplates {
for _, id := range cli.ImageIds {
fmt.Fprintln(output.Stdout, color.CyanString("%s", id))
}

} else if opt.Valid() {
st := image.Load(opt)
st = image.RenderImage(opt, st)
Expand Down

0 comments on commit ec8784f

Please sign in to comment.