Fixed unnecessary image load attempts

This commit is contained in:
Ignacio Rivero 2025-06-26 20:17:52 -03:00
parent c35188639d
commit 201ea4ae14

13
main.go
View File

@ -459,10 +459,8 @@ func findPrinter(ctx context.Context) (ble.Advertisement, error) {
ctxScan, cancel := context.WithTimeout(ctx, scanTimeout) ctxScan, cancel := context.WithTimeout(ctx, scanTimeout)
log.Println("Scanning for printer...") log.Println("Scanning for printer...")
err := ble.Scan(ctxScan, false, func(a ble.Advertisement) { err := ble.Scan(ctxScan, false, func(a ble.Advertisement) {
fmt.Printf("Found advertisement: %s, addr: %s\n", a.LocalName(), a.Addr())
if address != "" { if address != "" {
if a.Addr().String() == addr.String() { // Wonder why this works and not direct comparison if a.Addr().String() == addr.String() { // Wonder why this works and not direct comparison
println("Found target printer by address:", a.Addr())
adv = a adv = a
cancel() cancel()
} }
@ -477,6 +475,7 @@ func findPrinter(ctx context.Context) (ble.Advertisement, error) {
if adv == nil { if adv == nil {
return nil, fmt.Errorf("printer not found") return nil, fmt.Errorf("printer not found")
} }
log.Println("Found target printer with address:", adv.Addr().String())
return adv, nil return adv, nil
} }
@ -618,11 +617,13 @@ func main() {
// Get image path // Get image path
imagePath := flag.Arg(0) imagePath := flag.Arg(0)
pixels, height := []byte(nil), int(0) pixels, height, err := []byte(nil), int(0), error(nil)
pixels, height, err := loadAndProcessImage(imagePath, printMode, ditherType) if imagePath != "" {
if err != nil { pixels, height, err = loadAndProcessImage(imagePath, printMode, ditherType)
log.Fatalf("Failed to load and process image: %v", err) if err != nil {
log.Fatalf("Failed to load and process image: %v", err)
}
} }
if outputPath != "" { if outputPath != "" {