-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.go
More file actions
186 lines (154 loc) · 4.45 KB
/
Copy pathcli.go
File metadata and controls
186 lines (154 loc) · 4.45 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package advancegg - CLI utilities and command-line interface
package advancegg
import (
"flag"
"fmt"
"image/color"
"path/filepath"
"strings"
"github.com/GrandpaEJ/advancegg/internal/core"
)
// CLI represents the command-line interface
type CLI struct {
verbose bool
output string
}
// NewCLI creates a new CLI instance
func NewCLI() *CLI {
return &CLI{}
}
// ParseFlags parses command-line flags
func (cli *CLI) ParseFlags() {
flag.BoolVar(&cli.verbose, "v", false, "verbose output")
flag.BoolVar(&cli.verbose, "verbose", false, "verbose output")
flag.StringVar(&cli.output, "o", "", "output file")
flag.StringVar(&cli.output, "output", "", "output file")
flag.Parse()
}
// Version returns the library version
func Version() string {
return "1.6.0"
}
// Info prints library information
func (cli *CLI) Info() {
fmt.Printf("AdvanceGG v%s\n", Version())
fmt.Println("A powerful 2D graphics library for Go")
fmt.Println("https://github.com/GrandpaEJ/advancegg")
}
// ConvertImage converts an image file to PNG using AdvanceGG
func (cli *CLI) ConvertImage(inputPath, outputPath string) error {
if cli.verbose {
fmt.Printf("Converting %s to %s\n", inputPath, outputPath)
}
// Load the input image
img, err := core.LoadImage(inputPath)
if err != nil {
return fmt.Errorf("failed to load image: %v", err)
}
// Create a context from the image
dc := core.NewContextForImage(img)
// Save as PNG
err = dc.SavePNG(outputPath)
if err != nil {
return fmt.Errorf("failed to save PNG: %v", err)
}
if cli.verbose {
fmt.Printf("Successfully converted %s to %s\n", inputPath, outputPath)
}
return nil
}
// CreateSample creates a sample image demonstrating AdvanceGG capabilities
func (cli *CLI) CreateSample(outputPath string) error {
if cli.verbose {
fmt.Printf("Creating sample image: %s\n", outputPath)
}
dc := core.NewContext(800, 600)
// White background
dc.SetRGB(1, 1, 1)
dc.Clear()
// Draw some shapes to demonstrate capabilities
// Blue circle
dc.SetRGB(0, 0, 1)
dc.DrawCircle(200, 150, 80)
dc.Fill()
// Red rectangle
dc.SetRGB(1, 0, 0)
dc.DrawRectangle(350, 100, 150, 100)
dc.Fill()
// Green triangle (using path)
dc.SetRGB(0, 1, 0)
dc.MoveTo(600, 100)
dc.LineTo(550, 200)
dc.LineTo(650, 200)
dc.ClosePath()
dc.Fill()
// Draw some text
dc.SetRGB(0, 0, 0)
dc.DrawString("AdvanceGG Sample", 50, 300)
dc.DrawString("Circles, Rectangles, and Paths", 50, 350)
// Draw a gradient rectangle
gradient := core.NewLinearGradient(50, 400, 250, 400)
gradient.AddColorStop(0, color.RGBA{R: 255, G: 0, B: 0, A: 255})
gradient.AddColorStop(1, color.RGBA{R: 0, G: 0, B: 255, A: 255})
dc.SetFillStyle(gradient)
dc.DrawRectangle(50, 400, 200, 50)
dc.Fill()
// Save the image
err := dc.SavePNG(outputPath)
if err != nil {
return fmt.Errorf("failed to save sample image: %v", err)
}
if cli.verbose {
fmt.Printf("Sample image created: %s\n", outputPath)
}
return nil
}
// Run executes the CLI with the given arguments
func (cli *CLI) Run(args []string) error {
if len(args) == 0 {
cli.Info()
fmt.Println("\nUsage:")
fmt.Println(" advancegg sample [output.png] - Create a sample image")
fmt.Println(" advancegg convert input output - Convert image to PNG")
fmt.Println(" advancegg version - Show version")
fmt.Println(" advancegg help - Show this help")
return nil
}
command := args[0]
switch command {
case "version", "-v", "--version":
fmt.Printf("AdvanceGG v%s\n", Version())
return nil
case "help", "-h", "--help":
cli.Info()
fmt.Println("\nCommands:")
fmt.Println(" sample [output.png] - Create a sample image")
fmt.Println(" convert input output - Convert image to PNG")
fmt.Println(" version - Show version")
fmt.Println(" help - Show this help")
return nil
case "sample":
outputPath := "sample.png"
if len(args) > 1 {
outputPath = args[1]
}
return cli.CreateSample(outputPath)
case "convert":
if len(args) < 3 {
return fmt.Errorf("convert command requires input and output paths")
}
return cli.ConvertImage(args[1], args[2])
default:
return fmt.Errorf("unknown command: %s", command)
}
}
// GetOutputPath returns the output path, generating one if not specified
func (cli *CLI) GetOutputPath(inputPath string) string {
if cli.output != "" {
return cli.output
}
// Generate output path based on input
ext := filepath.Ext(inputPath)
base := strings.TrimSuffix(inputPath, ext)
return base + "_converted.png"
}