diff --git a/README.md b/README.md index 32a0072..a93f844 100644 --- a/README.md +++ b/README.md @@ -28,25 +28,28 @@ Flags: -t, --trim Trim output (for piping) Commands: - base64 encode Encode with base64 - base64 decode Decode with base64 - count chars Count characters - count words Count words - case camel formatCamelCase - case lower format lower - case random ForMat rANdom CaSE - case snake format_snake_case - case upper FORMAT UPPER - hash sha256 Calculate Sha256 - hash sha512 Calculate Sha512 - hash md5 Calculate MD5 - hex to Convert to hexadecimal - hex from Convert hexadecimal back - htpasswd Create a htpasswd string - reverse Reverse the input - url encode Encode string to valid URL - url decode Decode URL to string - version Show version information + base64 encode Encode with base64 + base64 decode Decode with base64 + count chars Count characters + count words Count words + case camel formatCamelCase + case lower format lower + case random ForMat rANdom CaSE + case snake format_snake_case + case upper FORMAT UPPER + hash sha256 Calculate Sha256 + hash sha512 Calculate Sha512 + hash md5 Calculate MD5 + hex to Convert to hexadecimal + hex from Convert hexadecimal back + lorem words Words + lorem sentences Sentences + lorem paragraphs Paragraphs + htpasswd Create a htpasswd string + reverse Reverse the input + url encode Encode string to valid URL + url decode Decode URL to string + version Show version information Run "gstring --help" for more information on a command. ``` @@ -78,13 +81,14 @@ Most of the commands work with stdin so you have to pipe content, for example: ```sh echo "hello world" | gstring case camel -gstring sha 256 < main.go +gstring hash sha256 < main.go # äquivalent to sha256sum main.go ``` However, some commands read from named arguments, for example: ```sh gstring htpasswd -u bar -p test +gstring lorem sentences -c 20 ``` ## What about the name? diff --git a/cli.go b/cli.go index 9bb089a..20b77f2 100644 --- a/cli.go +++ b/cli.go @@ -34,6 +34,11 @@ type CLI struct { ToHex toHexCmd `cmd:"" name:"to" help:"Convert to hexadecimal"` FromHex fromHexCmd `cmd:"" name:"from" help:"Convert hexadecimal back"` } `cmd:"" name:"hex" help:"Hexadeicmal converting"` + Lorem struct { + Words loremWordsCmd `cmd:"" help:"Words"` + Sentences loremSentencesCmd `cmd:"" help:"Sentences"` + Paragraphs loremParagraphsCmd `cmd:"" help:"Paragraphs"` + } `cmd:"" help:"Generate Lorem Ipsum"` HtPassWD HtPassWDCmd `cmd:"" name:"htpasswd" help:"Create a htpasswd string"` Reverse reverseCmd `cmd:"" help:"Reverse the input"` URL struct { @@ -283,3 +288,31 @@ func (c *decodeURLCmd) Run(globals *Globals) error { return nil }() } + +type loremWordsCmd struct { + Count int `default:"5" short:"c" help:"Number of words"` +} +type loremSentencesCmd struct { + Count int `default:"1" short:"c" help:"Number of sentences"` +} +type loremParagraphsCmd struct { + Count int `default:"1" short:"c" help:"Number of Paragraphs"` +} + +func (c *loremWordsCmd) Run(globals *Globals) error { + lorem := generateLoremIpsum("words", c.Count) + printOutput(lorem, false) + return nil +} + +func (c *loremSentencesCmd) Run(globals *Globals) error { + lorem := generateLoremIpsum("sentences", c.Count) + printOutput(lorem, false) + return nil +} + +func (c *loremParagraphsCmd) Run(globals *Globals) error { + lorem := generateLoremIpsum("paragraphs", c.Count) + printOutput(lorem, false) + return nil +} diff --git a/lorem.go b/lorem.go new file mode 100644 index 0000000..211151d --- /dev/null +++ b/lorem.go @@ -0,0 +1,53 @@ +package main + +import ( + "strings" +) + +var loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ultrices sagittis orci a scelerisque purus. At volutpat diam ut venenatis tellus in. Mattis rhoncus urna neque viverra justo nec ultrices dui sapien. Tellus orci ac auctor augue mauris. Eu scelerisque felis imperdiet proin fermentum. Tortor id aliquet lectus proin nibh nisl condimentum id. Id donec ultrices tincidunt arcu non sodales. Ultrices dui sapien eget mi proin. Bibendum neque egestas congue quisque egestas diam. Sem fringilla ut morbi tincidunt augue interdum. Vel risus commodo viverra maecenas accumsan lacus vel facilisis volutpat. Morbi blandit cursus risus at ultrices mi tempus. Adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus. Sapien pellentesque habitant morbi tristique senectus et netus et malesuada.` + +var loremIpsumWords = strings.Fields(loremIpsum) +var loremIpsumSentences = strings.Split(loremIpsum, ".") + +func generateLoremIpsum(lengthType string, count int) string { + switch lengthType { + case "words": + return generateWords(count) + case "sentences": + return generateSentences(count) + case "paragraphs": + return generateParagraphs(count) + default: + return "Invalid length type. Please specify 'words', 'sentences', or 'paragraphs'." + } +} + +func generateWords(count int) string { + var words strings.Builder + for i := range count { + index := i % len(loremIpsumWords) + if i > 0 { + words.WriteString(" ") + } + words.WriteString(loremIpsumWords[index]) + } + return words.String() +} + +func generateSentences(count int) string { + var sentences strings.Builder + for i := range count { + index := i % len(loremIpsumSentences) + sentences.WriteString(loremIpsumSentences[index]) + sentences.WriteString(".") + } + return sentences.String() +} + +func generateParagraphs(count int) string { + paragraphs := []string{} + for range count { + paragraphs = append(paragraphs, loremIpsum) + } + return strings.Join(paragraphs, "\n\n") +} diff --git a/lorem_test.go b/lorem_test.go new file mode 100644 index 0000000..fb5d320 --- /dev/null +++ b/lorem_test.go @@ -0,0 +1,86 @@ +package main + +import ( + "strings" + "testing" +) + +func TestGenerateWords(t *testing.T) { + tests := []struct { + count int + expected string + }{ + {5, "Lorem ipsum dolor sit amet,"}, + {10, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"}, + {50, strings.Join(loremIpsumWords[:50], " ")}, + } + + for _, tt := range tests { + t.Run(strings.Join(loremIpsumWords[:tt.count], " "), func(t *testing.T) { + result := generateWords(tt.count) + if result != tt.expected { + t.Errorf("generateWords(%d) = %q; want %q", tt.count, result, tt.expected) + } + }) + } +} + +func TestGenerateSentences(t *testing.T) { + tests := []struct { + count int + expected string + }{ + {1, loremIpsumSentences[0] + "."}, + {2, strings.Join(loremIpsumSentences[:2], ".") + "."}, + {5, strings.Join(loremIpsumSentences[:5], ".") + "."}, + } + + for _, tt := range tests { + t.Run(strings.Join(loremIpsumSentences[:tt.count], ".")+".", func(t *testing.T) { + result := generateSentences(tt.count) + if result != tt.expected { + t.Errorf("generateSentences(%d) = %q; want %q", tt.count, result, tt.expected) + } + }) + } +} + +func TestGenerateParagraphs(t *testing.T) { + tests := []struct { + count int + expected string + }{ + {1, loremIpsum}, + {2, loremIpsum + "\n\n" + loremIpsum}, + } + + for _, tt := range tests { + t.Run(strings.Repeat(loremIpsum+"\n\n", tt.count)[:len(strings.Repeat(loremIpsum+"\n\n", tt.count))-2], func(t *testing.T) { + result := generateParagraphs(tt.count) + if result != tt.expected { + t.Errorf("generateParagraphs(%d) = %q; want %q", tt.count, result, tt.expected) + } + }) + } +} + +func TestGenerateLoremIpsum(t *testing.T) { + tests := []struct { + lengthType string + count int + expected string + }{ + {"words", 5, "Lorem ipsum dolor sit amet,"}, + {"sentences", 2, strings.Join(loremIpsumSentences[:2], ".") + "."}, + {"paragraphs", 1, loremIpsum}, + } + + for _, tt := range tests { + t.Run(tt.lengthType, func(t *testing.T) { + result := generateLoremIpsum(tt.lengthType, tt.count) + if result != tt.expected { + t.Errorf("generateLoremIpsum(%q, %d) = %q; want %q", tt.lengthType, tt.count, result, tt.expected) + } + }) + } +}