Skip to content

Commit

Permalink
Merge pull request #5 from tmck-code/cli-flags
Browse files Browse the repository at this point in the history
Add command-line flags
  • Loading branch information
tmck-code authored Nov 29, 2021
2 parents 2a2f682 + 1e4c312 commit 12ae562
Showing 1 changed file with 59 additions and 21 deletions.
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()
}

0 comments on commit 12ae562

Please sign in to comment.