This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
144 lines (125 loc) · 2.92 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/urfave/cli"
)
// Message is something given by user
type Message []string
func main() {
app := cli.NewApp()
app.Name = "conohasay"
app.Description = app.Name + " is a program that generates ASCII picture of ConoHa characters"
app.Version = "1.0"
app.Authors = []cli.Author{
cli.Author{
Name: "Hironobu Saito",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "character,c",
Usage: "Specifies a particular character to use. You can also use -l option to get the list of characters",
Value: "conoha",
},
cli.StringFlag{
Name: "size,s",
Usage: `Specifies the size of the picture. The value should be "s", "m" or "l".`,
Value: "s",
},
cli.BoolFlag{
Name: "force-vertical,f",
Usage: "Force the vertical layout",
},
cli.IntFlag{
Name: "wrapcolumn,W",
Usage: "Specifies roughly where the message should be wrapped",
Value: 0,
},
cli.BoolFlag{
Name: "l,list",
Usage: "List characters",
},
}
// Setup the help writer
cli.AppHelpTemplate = `
{{.Name}} version {{.Version}}
Usage: {{.Name}} [-flv] [-h] [-c name]
[-s size(s,m or l)] [-W wrapcolumn] [message]
`
originalHelpPrinter := cli.HelpPrinter
cli.HelpPrinter = func(w io.Writer, templ string, d interface{}) {
app := d.(*cli.App)
buf := bytes.NewBuffer(make([]byte, 0, len(templ)*2))
originalHelpPrinter(buf, templ, app)
cow, err := NewCow("conoha", "s")
if err != nil {
fmt.Fprintf(os.Stdout, buf.String())
return
}
msg := strings.Split(buf.String(), "\n")
output, err := Conohasay(cow, msg, 0)
if err != nil {
fmt.Fprintf(os.Stdout, buf.String())
return
}
fmt.Fprintf(os.Stdout, output)
}
// Run
app.Action = action
app.RunAndExitOnError()
}
func action(ctx *cli.Context) error {
if ctx.Bool("list") {
cows := ListCows()
for _, cow := range cows {
fmt.Fprintf(os.Stdout, "%s\n", cow)
}
return nil
}
name := ctx.String("character")
size := ctx.String("size")
if size != "s" && size != "m" && size != "l" {
return fmt.Errorf(`Parameter size should be "l", "m" or "s".\n`)
}
cow, err := NewCow(name, size)
if err != nil {
return err
}
wrapcolumn := ctx.Int("wrapcolumn")
message := scanMessage(ctx)
output, err := Conohasay(cow, message, wrapcolumn)
if err != nil {
return err
}
fmt.Fprint(os.Stdout, output)
return nil
}
func scanMessage(ctx *cli.Context) Message {
var input string
if ctx.NArg() > 0 {
input = strings.Join(ctx.Args(), " ")
} else {
for {
sc := bufio.NewScanner(os.Stdin)
sc.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
return len(data), data, nil
})
sc.Scan()
input = strings.Trim(sc.Text(), "\r\n")
if input != "" {
break
}
}
}
input = strings.Trim(input, "\r\n \t")
return strings.Split(input, "\n")
}