Skip to content

Commit 21fbe11

Browse files
committed
Add count argument to source:local instruction
1 parent babbc84 commit 21fbe11

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

commands/source/exceptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ import "errors"
1919
var (
2020
errInvalidWidth error = errors.New("the width is invalid (integer, min 1)")
2121
errInvalidHeight error = errors.New("the height is invalid (integer, min 1)")
22+
errInvalidCount error = errors.New("the count is invalid (integer, min 1)")
2223
)

commands/source/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Help(a []string) {
2626
2727
Available subcommands and arguments:
2828
29-
local [width] [height] # Get a random, static color image.
29+
local [width] [height] [count] # Get random, static color images.
3030
picsum [width] [height] # Get a random image from "picsum.photos".
3131
pexels [key] [width] [height] [keyword] # Get a random image from "pexels.com".
3232
help # View help message.

commands/source/local.go

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func Local(a []string) {
3030
// Validate arguments and get parameters
31-
if len(a) != 2 {
31+
if len(a) != 3 {
3232
Help(nil)
3333
return
3434
}
@@ -55,45 +55,59 @@ func Local(a []string) {
5555
return
5656
}
5757

58-
// Create graphic
59-
rectangle := image.Rect(0, 0, width, height)
60-
graphic := image.NewRGBA(rectangle)
61-
62-
fill := color.RGBA{
63-
R: uint8(rand.Int()),
64-
G: uint8(rand.Int()),
65-
B: uint8(rand.Int()),
66-
A: uint8(rand.Int()),
67-
}
68-
69-
for x := range width {
70-
for y := range height {
71-
graphic.SetRGBA(x, y, fill)
72-
}
73-
}
74-
75-
// Create file
76-
directory, err := os.UserHomeDir()
58+
count, err := strconv.Atoi(a[2])
7759
if err != nil {
7860
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
7961
return
8062
}
8163

82-
path := filepath.Join(directory, "local.jpg")
83-
84-
file, err := os.Create(path)
85-
if err != nil {
86-
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
64+
if count < 1 {
65+
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(errInvalidCount.Error()))
8766
return
8867
}
8968

90-
// Encode graphic into the file
91-
err = jpeg.Encode(file, graphic, nil)
92-
if err != nil {
93-
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
94-
return
95-
}
69+
for i := range count {
70+
// Create graphic
71+
rectangle := image.Rect(0, 0, width, height)
72+
graphic := image.NewRGBA(rectangle)
9673

97-
// Print message
98-
fmt.Fprintf(os.Stdout, "Image saved in \"%s\"\n", path)
74+
fill := color.RGBA{
75+
R: uint8(rand.Intn(256)),
76+
G: uint8(rand.Intn(256)),
77+
B: uint8(rand.Intn(256)),
78+
A: uint8(rand.Intn(256)),
79+
}
80+
81+
for x := range width {
82+
for y := range height {
83+
graphic.SetRGBA(x, y, fill)
84+
}
85+
}
86+
87+
// Create file
88+
directory, err := os.UserHomeDir()
89+
if err != nil {
90+
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
91+
return
92+
}
93+
94+
name := fmt.Sprintf("local-%d.jpg", i+1)
95+
path := filepath.Join(directory, name)
96+
97+
file, err := os.Create(path)
98+
if err != nil {
99+
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
100+
return
101+
}
102+
103+
// Encode graphic into the file
104+
err = jpeg.Encode(file, graphic, nil)
105+
if err != nil {
106+
fmt.Fprintf(os.Stderr, "%s\n", strings.ToLower(err.Error()))
107+
return
108+
}
109+
110+
// Print message
111+
fmt.Fprintf(os.Stdout, "Image saved in \"%s\"\n", path)
112+
}
99113
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
func version() {
23-
message := `genimg v1.0.1
23+
message := `genimg v1.1.0
2424
Copyright (C) 2025 Enindu Alahapperuma
2525
Licensed under GNU GPL 3.0`
2626

0 commit comments

Comments
 (0)