refactor: standardize constants and improve brightness calculations

- Replaced hardcoded values with constants for default dimensions and timeout settings in various files.
- Updated brightness calculation logic to use defined constants for maximum color value and bit shifts.
- Enhanced the ASCII image generation function to utilize new constants for improved readability and maintainability.
This commit is contained in:
0x524a
2025-12-02 08:53:13 -05:00
parent 96ac509c24
commit de752f249e
4 changed files with 36 additions and 12 deletions
+5 -5
View File
@@ -153,9 +153,9 @@ func imageToASCIIFromImage(img image.Image, config ASCIIConfig, format string) (
// Uses standard luminance formula.
func calculateBrightness(r, g, b uint32) int {
// Convert 16-bit color to 8-bit
r8 := uint8(r >> 8) //nolint:gosec // Color values are clamped to valid range
g8 := uint8(g >> 8) //nolint:gosec // Color values are clamped to valid range
b8 := uint8(b >> 8) //nolint:gosec // Color values are clamped to valid range
r8 := uint8(r >> bitShift8) //nolint:gosec // Color values are clamped to valid range
g8 := uint8(g >> bitShift8) //nolint:gosec // Color values are clamped to valid range
b8 := uint8(b >> bitShift8) //nolint:gosec // Color values are clamped to valid range
// Use standard brightness calculation
// https://en.wikipedia.org/wiki/Relative_luminance
@@ -233,8 +233,8 @@ func formatBytes(bytes int64) string {
// CreateASCIIHighQuality creates a high-quality ASCII representation.
func CreateASCIIHighQuality(imageData []byte) (string, error) {
config := ASCIIConfig{
Width: 160,
Height: 50,
Width: largeASCIIWidth,
Height: largeASCIIHeight,
Invert: false,
Quality: "high",
}
+10 -2
View File
@@ -17,6 +17,14 @@ import (
"github.com/0x524a/onvif-go/discovery"
)
const (
defaultTimeoutSeconds = 10
defaultRetryDelay = 5
ptzTimeoutSeconds = 30
maxRetries = 3
readBufferSize = 5
)
type CLI struct {
client *onvif.Client
reader *bufio.Reader
@@ -101,7 +109,7 @@ func (c *CLI) discoverCameras() {
fmt.Println("This may take a few seconds...")
fmt.Println()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
// Try auto-discovery first (no specific interface)
@@ -260,7 +268,7 @@ func (c *CLI) discoverWithInterfaceSelection() ([]*discovery.Device, error) {
// performDiscoveryOnInterface performs discovery on a specific network interface.
func (c *CLI) performDiscoveryOnInterface(interfaceName string) ([]*discovery.Device, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
opts := &discovery.DiscoverOptions{