-
Notifications
You must be signed in to change notification settings - Fork 6
/
loremipsum.go
74 lines (63 loc) · 1.75 KB
/
loremipsum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Copyright © 2022 KAI CHU CHUNG <cage.chung@gmail.com>
*/
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/atotto/clipboard"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/loremipsum.v1"
"github.com/cage1016/alfred-devtoys/alfred"
)
// loremCmd represents the lorem command
var loremCmd = &cobra.Command{
Use: "li",
Short: "Lorem ipsum is a dummy text generator",
Run: runLorem,
}
func runLorem(cmd *cobra.Command, args []string) {
query := args[0]
if strings.TrimSpace(query) == "" {
query, _ = clipboard.ReadAll()
_, err := strconv.ParseUint(query, 10, 64)
if err != nil {
query = alfred.GetLiDefault(wf)
}
}
logrus.Debugf("query: %s", query)
c, err := strconv.ParseInt(query, 10, 64)
if err != nil {
wf.NewItem(fmt.Sprintf("`%s` is invalid integer", query)).Subtitle("Try a different query?").Icon(LoremIpsumGrayIcon)
} else {
loremIpsumGeneratoe := loremipsum.New()
words := loremIpsumGeneratoe.Words(int(c))
wf.NewItem(words).
Subtitle(fmt.Sprintf("⌘+L, ↩ Copy %d Words", c)).
Valid(true).
Arg(words).
Largetype(words).Icon(LoremIpsumIcon).
Var("action", "copy").
Valid(true)
sentences := loremIpsumGeneratoe.Sentences(int(c))
wf.NewItem(sentences).
Subtitle(fmt.Sprintf("⌘+L, ↩ Copy %d Sentences", c)).
Valid(true).
Arg(sentences).
Largetype(sentences).Icon(LoremIpsumIcon).
Var("action", "copy")
paragraphs := strings.Join(strings.Split(loremIpsumGeneratoe.Paragraphs(int(c)), `\n`), "\n\n")
wf.NewItem(paragraphs).
Subtitle(fmt.Sprintf("⌘+L, ↩ Copy %d Paragraphs", c)).
Valid(true).
Arg(paragraphs).
Largetype(paragraphs).Icon(LoremIpsumIcon).
Var("action", "copy")
}
wf.SendFeedback()
}
func init() {
rootCmd.AddCommand(loremCmd)
}