Example #1
0
File: main.go Project: moul/sapin
func main() {
	if _, err := flags.Parse(&opts); err == nil {
		sapin := sapin.NewSapin(opts.Size)
		if opts.Star {
			sapin.AddStar()
		}
		sapin.AddBalls(opts.Balls)
		sapin.AddGarlands(opts.Garlands)
		if opts.Emoji {
			sapin.Emojize()
		}
		if opts.Color {
			sapin.ColorOpts = "bh"
			sapin.Colorize()
		}
		if opts.Presents {
			sapin.AddPresents()
		}
		fmt.Print(sapin.String())
		os.Exit(0)
	}
	os.Exit(1)
}
Example #2
0
File: app.go Project: moul/sapin
func handler(w http.ResponseWriter, r *http.Request) {
	// extract query from url
	u, err := url.Parse(r.URL.String())
	if err != nil {
		fmt.Fprintf(w, "URL error: %v:\n", err)
		return
	}

	// parse query
	m, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		fmt.Fprintf(w, "URL query error: %v:\n", err)
		return
	}

	// check if no arguments
	if len(m) == 0 {
		http.Redirect(w, r, "?size=5&balls=4&star=true&emoji=false&color=false&presents=true&garlands=5", http.StatusFound)
	}

	// unmarshal arguments
	opts := Options{}
	decoder := schema.NewDecoder()
	err = decoder.Decode(&opts, m)
	if err != nil {
		fmt.Fprintf(w, "Parameters error: %v:\n", err)
		return
	}

	// check size argument
	if opts.Size > 10 {
		fmt.Fprintf(w, "Max size is: 10\n")
		return
	}
	if opts.Size < 0 {
		fmt.Fprintf(w, "Min size is: 0\n")
		return
	}

	if opts.Balls < 0 {
		fmt.Fprintf(w, "Min value for balls is 0\n")
		return
	}
	if opts.Balls > 100 {
		fmt.Fprintf(w, "Max value for balls is 100\n")
		return
	}
	if opts.Garlands < 0 {
		fmt.Fprintf(w, "Min value for garlands is 0\n")
		return
	}
	if opts.Garlands > 50 {
		fmt.Fprintf(w, "Max value for garlands is 50\n")
		return
	}

	sapin := sapin.NewSapin(opts.Size)
	if opts.Star {
		sapin.AddStar()
	}
	sapin.AddBalls(opts.Balls)
	sapin.AddGarlands(opts.Garlands)
	if opts.Emoji {
		sapin.Emojize()
	}
	if opts.Color {
		w.Header().Set("Content-Type", "text/html")
		sapin.Colorize()
		if opts.Presents {
			sapin.AddPresents()
		}
		coloredOutput := string(terminal.Render([]byte(sapin.String())))
		html := strings.Replace(htmlTemplate, "CONTENT", coloredOutput, 1)
		fmt.Fprint(w, html)
	} else {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		if opts.Presents {
			sapin.AddPresents()
		}
		fmt.Fprintf(w, "%s\n", sapin)
	}
}