This commit is contained in:
Simon Vieille 2024-08-25 21:51:15 +02:00
commit 3413de82e4
4 changed files with 103 additions and 0 deletions

29
README.md Normal file
View file

@ -0,0 +1,29 @@
# Lorem
Lorem ipsum generator.
## Usage
Generates 5 words:
```
lorem -w 5
```
Generates 4 paragraphs:
```
lorem -p 4
```
Generates 3 sentences:
```
lorem -s 3
```
## Build
```
go build
```

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module gitnet.fr/deblan/lorem
go 1.23.0
require github.com/go-loremipsum/loremipsum v1.1.3 // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/go-loremipsum/loremipsum v1.1.3 h1:ZRhA0ZmJ49lGe5HhWeMONr+iGftWDsHfrYBl5ktDXso=
github.com/go-loremipsum/loremipsum v1.1.3/go.mod h1:OJQjXdvwlG9hsyhmMQoT4HOm4DG4l62CYywebw0XBoo=

67
main.go Normal file
View file

@ -0,0 +1,67 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/go-loremipsum/loremipsum"
)
func RandomWords(generator *loremipsum.LoremIpsum, count int) string {
values := []string{}
for i := 0; i < count; i++ {
values = append(values, generator.Word())
}
return strings.Join(values, " ")
}
func RemoveFirstItem(value, separator, newSeparator string) string {
datas := strings.Split(value, separator)
return strings.Join(datas[1:], newSeparator)
}
func usage(cmd string) string {
return `Usage of ` + cmd + `:
-h Show help
-p int
Nomber of paragraphs
-s int
Nomber of sentences
-w int
Nomber of words
`
}
func main() {
var value string
wordFlag := flag.Int("w", 0, "Nomber of words")
paragraphFlag := flag.Int("p", 0, "Nomber of paragraphs")
sentenceFlag := flag.Int("s", 0, "Nomber of sentences")
helpFlag := flag.Bool("h", false, "Show help")
flag.Parse()
generator := loremipsum.New()
if *helpFlag {
fmt.Print(usage(os.Args[0]))
os.Exit(0)
}
if *wordFlag > 0 {
value = RandomWords(generator, *wordFlag)
} else if *paragraphFlag > 0 {
value = RemoveFirstItem(generator.Paragraphs(*paragraphFlag+1), "\n", "\n\n")
} else if *sentenceFlag > 0 {
value = RemoveFirstItem(generator.Sentences(*sentenceFlag+1), ". ", ". ")
} else {
fmt.Fprint(os.Stderr, usage(os.Args[0]))
os.Exit(1)
}
fmt.Println(value)
}