-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathroot.go
152 lines (132 loc) · 5.37 KB
/
root.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
145
146
147
148
149
150
151
152
package main
import (
"image/color"
"runtime"
"strings"
"github.com/jiro4989/textimg/v3/config"
"github.com/jiro4989/textimg/v3/image"
"github.com/jiro4989/textimg/v3/internal/global"
"github.com/jiro4989/textimg/v3/parser"
"github.com/spf13/cobra"
)
var (
conf config.Config
envvars config.EnvVars
)
func init() {
envvars = config.NewEnvVars()
cobra.OnInitialize()
RootCommand.Flags().SortFlags = false
RootCommand.Flags().StringVarP(&conf.Foreground, "foreground", "g", "white", `foreground text color.
available color types are [black|red|green|yellow|blue|magenta|cyan|white]
or (R,G,B,A(0~255))`)
RootCommand.Flags().StringVarP(&conf.Background, "background", "b", "black", `background text color.
color types are same as "foreground" option`)
var font string
envFontFile := envvars.FontFile
if envFontFile != "" {
font = envFontFile
}
RootCommand.Flags().StringVarP(&conf.FontFile, "fontfile", "f", font, `font file path.
You can change this default value with environment variables TEXTIMG_FONT_FILE`)
RootCommand.Flags().IntVarP(&conf.FontIndex, "fontindex", "x", 0, "")
conf.SetFontFileAndFontIndex(runtime.GOOS)
envEmojiFontFile := envvars.EmojiFontFile
RootCommand.Flags().StringVarP(&conf.EmojiFontFile, "emoji-fontfile", "e", envEmojiFontFile, "emoji font file")
RootCommand.Flags().IntVarP(&conf.EmojiFontIndex, "emoji-fontindex", "X", 0, "")
RootCommand.Flags().BoolVarP(&conf.UseEmojiFont, "use-emoji-font", "i", false, "use emoji font")
RootCommand.Flags().BoolVarP(&conf.UseShellgeiEmojiFontfile, "shellgei-emoji-fontfile", "z", false, `emoji font file for shellgei-bot (path: "`+config.ShellgeiEmojiFontPath+`")`)
RootCommand.Flags().IntVarP(&conf.FontSize, "fontsize", "F", 20, "font size")
RootCommand.Flags().StringVarP(&conf.Outpath, "out", "o", "", `output image file path.
available image formats are [png | jpg | gif]`)
RootCommand.Flags().BoolVarP(&conf.AddTimeStamp, "timestamp", "t", false, `add time stamp to output image file path.`)
RootCommand.Flags().BoolVarP(&conf.SaveNumberedFile, "numbered", "n", false, `add number-suffix to filename when the output file was existed.
ex: t_2.png`)
RootCommand.Flags().BoolVarP(&conf.UseShellgeiImagedir, "shellgei-imagedir", "s", false, `image directory path (path: "$HOME/Pictures/t.png" or "$TEXTIMG_OUTPUT_DIR/t.png")`)
RootCommand.Flags().BoolVarP(&conf.UseAnimation, "animation", "a", false, "generate animation gif")
RootCommand.Flags().IntVarP(&conf.Delay, "delay", "d", 20, "animation delay time")
RootCommand.Flags().IntVarP(&conf.LineCount, "line-count", "l", 1, "animation input line count")
RootCommand.Flags().BoolVarP(&conf.UseSlideAnimation, "slide", "S", false, "use slide animation")
RootCommand.Flags().IntVarP(&conf.SlideWidth, "slide-width", "W", 1, "sliding animation width")
RootCommand.Flags().BoolVarP(&conf.SlideForever, "forever", "E", false, "sliding forever")
RootCommand.Flags().BoolVarP(&conf.PrintEnvironments, "environments", "", false, "print environment variables")
RootCommand.Flags().BoolVarP(&conf.ToSlackIcon, "slack", "", false, "resize to slack icon size (128x128 px)")
RootCommand.Flags().IntVarP(&conf.ResizeWidth, "resize-width", "", 0, "resize width")
RootCommand.Flags().IntVarP(&conf.ResizeHeight, "resize-height", "", 0, "resize height")
}
var RootCommand = &cobra.Command{
Use: global.AppName,
Short: global.AppName + " is command to convert from colored text (ANSI or 256) to image.",
Example: global.AppName + ` $'\x1b[31mRED\x1b[0m' -o out.png`,
Version: global.Version,
RunE: func(cmd *cobra.Command, args []string) error {
return RunRootCommand(conf, args, envvars)
},
}
func RunRootCommand(c config.Config, args []string, envs config.EnvVars) error {
if c.PrintEnvironments {
config.PrintEnvs()
return nil
}
if err := c.Adjust(args, envs); err != nil {
return err
}
defer c.Writer.Close()
tokens, err := parser.Parse(strings.Join(c.Texts, "\n"))
if err != nil {
return err
}
bw := tokens.MaxStringWidth()
bh := len(tokens.StringLines())
if !c.ToSlackIcon {
// TODO: コピペコードになってるので共通化する
var (
f = c.FontSize
charWidth = f / 2
charHeight = int(float64(f) * 1.1)
w = bw * charWidth
h = bh * charHeight
)
c.ResizeWidth, c.ResizeHeight = complementWidthHeight(w, h, c.ResizeWidth, c.ResizeHeight)
}
param := &image.ImageParam{
BaseWidth: bw,
BaseHeight: bh,
ForegroundColor: color.RGBA(c.ForegroundColor),
BackgroundColor: color.RGBA(c.BackgroundColor),
FontFace: c.FontFace,
EmojiFontFace: c.EmojiFontFace,
EmojiDir: c.EmojiDir,
FontSize: c.FontSize,
Delay: c.Delay,
UseAnimation: c.UseAnimation,
AnimationLineCount: c.LineCount,
ResizeWidth: c.ResizeWidth,
ResizeHeight: c.ResizeHeight,
UseEmoji: c.UseEmojiFont,
}
img := image.NewImage(param)
if err := img.Draw(tokens); err != nil {
return err
}
if err := img.Encode(c.Writer, c.FileExtension); err != nil {
return err
}
return nil
}
// complementWidthHeight は width, height の片方が 0 の時、サイズを調整する。
func complementWidthHeight(x, y, w, h int) (int, int) {
if w == 0 {
hh := y
d := float64(h) / float64(hh)
w = int(float64(x) * d)
return w, h
}
if h == 0 {
ww := x
d := float64(w) / float64(ww)
h = int(float64(y) * d)
return w, h
}
return w, h
}