Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command-line flags #5

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 59 additions & 21 deletions cmd/pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@ package main
import (
"bufio"
"encoding/binary"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/mitchellh/go-wordwrap"
)

func printSpeechBubble(scanner *bufio.Scanner, width int) {
border := strings.Repeat("-", width+2)
func printSpeechBubbleLine(line string, width int) {
if len(line) > width {
fmt.Println("|", line)
} else {
fmt.Println("|", line, strings.Repeat(" ", width-len(line)), "|")
}
}

func printWrappedText(line string, width int, tabSpaces string) {
for _, wline := range strings.Split(wordwrap.WrapString(strings.Replace(line, "\t", tabSpaces, -1), uint(width)), "\n") {
printSpeechBubbleLine(wline, width)
}
}

func printSpeechBubble(scanner *bufio.Scanner, args Args) {
border := strings.Repeat("-", args.Width+2)
fmt.Println("/" + border + "\\")

for scanner.Scan() {
for _, wline := range strings.Split(wordwrap.WrapString(strings.Replace(scanner.Text(), "\t", " ", -1), uint(width)), "\n") {
if len(wline) > width {
fmt.Println("| ", wline, len(wline))
} else {
fmt.Println("|", wline, strings.Repeat(" ", width-len(wline)), "|")
}
line := scanner.Text()

if !args.NoTabSpaces {
line = strings.Replace(line, "\t", args.TabSpaces, -1)
}
if args.NoWrap {
printSpeechBubbleLine(line, args.Width)
} else {
printWrappedText(line, args.Width, args.TabSpaces)
}
}
fmt.Println("\\" + border + "/")
Expand All @@ -36,28 +54,48 @@ func randomInt(n int) int {
}

func printPokemon() {
choice := PokemonList[randomInt(len(PokemonList))]
choice := PokemonList[randomInt(len(PokemonList))]
binary.Write(os.Stdout, binary.LittleEndian, choice.Data)
fmt.Printf("choice: %s / categories: %s\n", choice.Name.Name, choice.Name.Categories)
}

type Args struct {
Width int
Width int
NoWrap bool
TabSpaces string
NoTabSpaces bool
}

func parseArgs() Args {
if len(os.Args) <= 1 {
return Args{Width: 40}
}
width, err := strconv.Atoi(os.Args[1])
if err != nil {
return Args{Width: 40}
func parseFlags() Args {
width := flag.Int("width", 80, "the max speech bubble width")
noWrap := flag.Bool("nowrap", false, "disable text wrapping (fastest)")
tabWidth := flag.Int("tabwidth", 4, "replace any tab characters with N spaces")
noTabSpaces := flag.Bool("notabspaces", false, "do not replace tab characters (fastest)")
fastest := flag.Bool("fastest", false, "run with the fastest possible configuration (-nowrap -notabspaces)")

flag.Parse()
var args Args

if *fastest {
args = Args{
Width: *width,
NoWrap: true,
TabSpaces: " ",
NoTabSpaces: true,
}
} else {
args = Args{
Width: *width,
NoWrap: *noWrap,
TabSpaces: strings.Repeat(" ", *tabWidth),
NoTabSpaces: *noTabSpaces,
}
}
return Args{Width: width}
return args
}

func main() {
args := parseArgs()
printSpeechBubble(bufio.NewScanner(os.Stdin), args.Width)
args := parseFlags()
printSpeechBubble(bufio.NewScanner(os.Stdin), args)
printPokemon()
}