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

feat: added quote option #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ var rootCmd = &cobra.Command{
fmt.Println("Error: Something went wrong with monkeytype flag", err)
}

q, err := cmd.Flags().GetBool("quote")
if err != nil {
fmt.Println("Error: Something went wrong with the quote flag", err)
}

flagStruct := flags.Flags{
Length: length,
MinWordLength: minWordLength,
Capital: c,
Punctuation: p,
Quote: q,
}

stat, err := os.Stdin.Stat()
Expand All @@ -52,16 +58,13 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}

switch true {
switch {
case (stat.Mode() & os.ModeCharDevice) == 0:
err = typer.FromStdin(length, &flagStruct)
break
case m:
err = typer.FromMonkeytype(monkeytypeLanguage, &flagStruct)
break
case filePath != "":
err = typer.FromFile(filePath, &flagStruct)
break
default:
err = typer.FromRandom(length, &flagStruct)
}
Expand All @@ -84,12 +87,13 @@ func init() {
rootCmd.PersistentFlags().StringVar(&monkeytypeLanguage, "monkeytype-language", "english", "monkeytype language")
rootCmd.PersistentFlags().StringVarP(&filePath, "file", "f", "", "path to input file")

rootCmd.PersistentFlags().IntVarP(&length, "length", "l", flags.DefaultLength, "set max text length")
rootCmd.PersistentFlags().IntVarP(&length, "length", "l", flags.DefaultLength, "set max text length, if -q flag is used, it will be the max quote length")
rootCmd.PersistentFlags().IntVar(&minWordLength, "min-word-length", -1, "set min word length")

rootCmd.PersistentFlags().BoolP("capital", "c", false, "true to include capital letters")
rootCmd.PersistentFlags().BoolP("punctuation", "p", false, "true to include punctuation")
rootCmd.PersistentFlags().BoolP("monkeytype", "m", false, "true to use monkeytype as a source")
rootCmd.PersistentFlags().BoolP("quote", "q", false, "true to use quotes, needs monkeytype flag. it's ignores -p and -c flags")

if length > flags.MaxLength {
fmt.Println("Error: Max length value exceeded. Restoring to max length value.")
Expand Down
45 changes: 43 additions & 2 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flags

import (
"fmt"
"math/rand"
"strings"

util "github.com/maaslalani/typer/pkg/utility"
Expand All @@ -17,10 +18,20 @@ type Flags struct {
MinWordLength int
Capital bool
Punctuation bool
Quote bool
}

// formatText applies formatting based on flags
func (f *Flags) FormatText(s string) (string, error) {
// It is not very correct to have Quote in this file, but if it is in typer, it causes an import cycle
// TODO make a new file for Quote or something
type Quote struct {
Text string `json:"text"`
Source string `json:"source"`
Length int `json:"length"`
Id int `json:"id"`
}

// formatWords applies formatting based on flags
func (f *Flags) FormatWords(s string) (string, error) {
var err error
s, err = util.AdjustWhitespace(s)
if err != nil {
Expand Down Expand Up @@ -58,3 +69,33 @@ func (f *Flags) FormatText(s string) (string, error) {

return s, nil
}

// formatQuote applies formatting based on flags for quotes.
// this method ignores the p and c flags because they are quotes
// returns random quote from struct
func (f *Flags) FormatQuote(q struct {
Quotes []Quote `json:"quotes"`
}) (string, error) {
var quotes []string

if f.Length <= 0 {
f.Length = DefaultLength
} else if f.Length > MaxLength {
f.Length = MaxLength
}

// for quotes, f.Length is the max length of the quote because there is no
// information about words count in json file, so we will filter by letters count
// and use f.Length as max length
for _, q := range q.Quotes {
if q.Length >= f.Length {
s, err := util.AdjustWhitespace(q.Text)
if err != nil {
return "", err
}
quotes = append(quotes, s)
}
}

return quotes[rand.Intn(len(quotes))], nil
}
60 changes: 40 additions & 20 deletions pkg/typer/typer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func FromStdin(n int, flagStruct *flags.Flags) error {
}

text := string(stdin)
text, err := flagStruct.FormatText(text)
text, err := flagStruct.FormatWords(text)
if err != nil {
return err
}
Expand All @@ -51,7 +51,7 @@ func FromStdin(n int, flagStruct *flags.Flags) error {
func FromRandom(n int, flagStruct *flags.Flags) error {
text := util.RandomWords(n)

text, err := flagStruct.FormatText(text)
text, err := flagStruct.FormatWords(text)
if err != nil {
return err
}
Expand All @@ -66,7 +66,7 @@ func FromFile(path string, flagStruct *flags.Flags) error {
return err
}

text, err = flagStruct.FormatText(text)
text, err = flagStruct.FormatWords(text)
if err != nil {
return err
}
Expand All @@ -79,7 +79,12 @@ func FromMonkeytype(language string, flagStruct *flags.Flags) error {
language = "english"
}

resp, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/monkeytypegame/monkeytype/master/frontend/static/languages/%s.json", language))
textType := "languages"
if flagStruct.Quote {
textType = "quotes"
}

resp, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/monkeytypegame/monkeytype/master/frontend/static/%s/%s.json", textType, language))
if err != nil {
return err
}
Expand All @@ -95,22 +100,37 @@ func FromMonkeytype(language string, flagStruct *flags.Flags) error {
return err
}

words := struct {
Words []string `json:"words"`
}{}
if err := json.Unmarshal(bodyBytes, &words); err != nil {
return err
}

seed := rand.NewSource(time.Now().Unix() + int64(len(words.Words)))
r := rand.New(seed)
r.Shuffle(len(words.Words), func(i, j int) {
words.Words[i], words.Words[j] = words.Words[j], words.Words[i]
})

formatted, err := flagStruct.FormatText(strings.Join(words.Words, "\n"))
if err != nil {
return err
var formatted string
if !flagStruct.Quote {
words := struct {
Words []string `json:"words"`
}{}
if err := json.Unmarshal(bodyBytes, &words); err != nil {
return err
}

seed := rand.NewSource(time.Now().Unix() + int64(len(words.Words)))
r := rand.New(seed)
r.Shuffle(len(words.Words), func(i, j int) {
words.Words[i], words.Words[j] = words.Words[j], words.Words[i]
})

formatted, err = flagStruct.FormatWords(strings.Join(words.Words, "\n"))
if err != nil {
return err
}
} else {
quotes := struct {
Quotes []flags.Quote `json:"quotes"`
}{}
if err := json.Unmarshal(bodyBytes, &quotes); err != nil {
return err
}

formatted, err = flagStruct.FormatQuote(quotes)
if err != nil {
return err
}
}

return run(formatted)
Expand Down