-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
119 lines (101 loc) · 3.08 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
// Copyright 2016 Arsham Shirvani <[email protected]>. All rights reserved.
// Use of this source code is governed by the Apache 2.0 license.
// License can be found in the LICENSE file.
// Package main is the entrypoint to the figurine binary.
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/arsham/figurine/figurine"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultString = "Arsham"
fontName string
visualMode bool
list bool
sample bool
version = "development"
currentSha = "N/A"
)
var rootCmd = &cobra.Command{
Use: "figurine",
Short: "Print any text in style",
RunE: func(_ *cobra.Command, args []string) error {
if list {
listFonts(args)
return nil
}
if len(args) > 0 && args[0] == "version" {
fmt.Printf("figurine version %s (%s)\n", version, currentSha)
return nil
}
return decorate(strings.Join(args, " "))
},
}
func main() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
rand.Seed(time.Now().UnixNano())
cobra.OnInitialize(func() {
viper.AutomaticEnv()
})
rootCmd.Flags().BoolVarP(&visualMode, "visual", "v", false, "Prints the font name.")
rootCmd.Flags().StringVarP(&fontName, "font", "f", "", "Choose a font name. Default is a random font.")
rootCmd.Flags().BoolVarP(&list, "list", "l", false, "Lists all available fonts.")
rootCmd.Flags().BoolVarP(&sample, "sample", "s", false, "Prints a sample with that font.")
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Available Commands:
help Help about any command
version Print binary version information
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`)
}
func decorate(input string) error {
if input == "" {
input = defaultString
}
if fontName == "" {
index := rand.Intn(len(fontNames))
fontName = fontNames[index]
}
if visualMode {
fmt.Printf("Font: %s\n", fontName)
}
return figurine.Write(os.Stdout, input, fontName)
}
func listFonts(args []string) {
if len(args) == 0 {
args = []string{"Golang"}
}
input := strings.Join(args, " ")
for _, f := range fontNames {
fmt.Println(f)
if sample {
err := figurine.Write(os.Stdout, input, f)
if err != nil {
fmt.Fprintf(os.Stderr, "printing to the output: %v", err)
}
fmt.Println()
}
}
}