utils

package
v0.8.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const MaxFilenameLength = 240
View Source
const NotificationAppName = "Surge"

Variables

View Source
var SuppressNotifications bool

SuppressNotifications can be set to true to prevent desktop notifications. Tests should set this to true via TestMain or init() to avoid notification spam.

Functions

func CleanupLogs

func CleanupLogs(retentionCount int)

CleanupLogs removes old log files, keeping only the most recent retentionCount files

func ConfigureDebug

func ConfigureDebug(dir string)

ConfigureDebug sets the directory for debug logs

func ConfigureDialer added in v0.8.0

func ConfigureDialer(dialer *net.Dialer, customAddr string)

ConfigureDialer modifies the provided net.Dialer to route all DNS lookups through the specified custom DNS server address. customAddr should include the port, e.g., "1.1.1.1:53".

func ConvertBytesToHumanReadable

func ConvertBytesToHumanReadable(bytes int64) string

ConvertBytesToHumanReadable converts a given number of bytes into a human-readable format (e.g., KB, MB, GB).

func CopyFile

func CopyFile(src, dst string) error

CopyFile centralizes the rename-fallback copy path used by download finalization.

func CopyRedirectHeaders

func CopyRedirectHeaders(dst, src *http.Request)

CopyRedirectHeaders preserves all headers for same-origin redirects but strips sensitive headers (cookies, auth) for cross-domain redirects.

func Debug

func Debug(format string, args ...any)

Debug writes a message to debug.log file in the configured directory

func DetermineFilename

func DetermineFilename(rawurl string, resp *http.Response) (string, io.Reader, error)

DetermineFilename extracts the filename from a URL and HTTP response, applying various heuristics. It returns the determined filename, a new io.Reader that includes any sniffed header bytes, and an error.

func EnsureAbsPath

func EnsureAbsPath(path string) string

EnsureAbsPath takes a clean path and forces it to be absolute. If it fails to get absolute path (rare), it checks if it's already absolute, otherwise relies on the input.

func IsLoggingEnabled added in v0.7.7

func IsLoggingEnabled() bool

IsLoggingEnabled returns true if debug logging is configured This allows callers to skip expensive argument evaluation

func IsWindowsAbsPath added in v0.8.1

func IsWindowsAbsPath(p string) bool

IsWindowsAbsPath reports whether p looks like a Windows absolute path even when running on a non-Windows host (for example inside Docker on Linux).

func MapWindowsPathToDefaultDir added in v0.8.1

func MapWindowsPathToDefaultDir(requestPath, defaultDir string) (string, bool)

MapWindowsPathToDefaultDir projects a Windows absolute client path onto a server-side default download directory by preserving only the suffix beneath the matching root folder name. Example:

C:/Users/me/Downloads/subdir -> /downloads/subdir

func Notify

func Notify(title, message string)

func OpenBrowser added in v0.8.0

func OpenBrowser(url string) error

OpenBrowser opens a URL in the system's default web browser.

func OpenContainingFolder

func OpenContainingFolder(path string) error

OpenContainingFolder opens the containing folder of a file in the system's default file explorer.

func OpenFile

func OpenFile(path string) error

OpenFile opens a file in the system's default file explorer or editor.

func ReadURLsFromFile

func ReadURLsFromFile(filepath string) ([]string, error)

ReadURLsFromFile reads URLs from a file. Accepts one URL per line or whitespace-separated URLs, and ignores comments. Trailing-slash-only variants are treated as the same URL so batch imports behave consistently across CLI and TUI entry points.

func Run added in v0.8.4

func Run(executable string, args []string, env []string) error

Run executes an executable with the given arguments and environment. On Unix-like systems, it replaces the current process using syscall.Exec. On Windows, it starts a new process and exits the current one.

func Truncate added in v0.8.3

func Truncate(s string, limit int) string

Truncate truncates a string to a maximum visual width and adds an ellipsis if needed.

func TruncateFilename added in v0.8.1

func TruncateFilename(name string) string

TruncateFilename ensures a filename does not exceed MaxFilenameLength while preserving the extension and being UTF-8 safe.

func TruncateMiddle added in v0.8.4

func TruncateMiddle(s string, limit int) string

TruncateMiddle truncates a string in the middle to a maximum visual width. It is ANSI-aware.

func TruncateTwoLines added in v0.8.4

func TruncateTwoLines(s string, width int) string

TruncateTwoLines middle-truncates a string to fit in at most 2 lines of a given width. It uses character-based wrapping (ignoring word boundaries) to maximize space usage.

func WrapText added in v0.8.3

func WrapText(text string, width int) string

WrapText wraps a string to a specified maximum width. It tries to wrap at word boundaries (spaces) and handles multi-byte runes and visual width.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL