Documentation
¶
Overview ¶
Package util provides shared utility functions for ntm.
Index ¶
- func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error
- func EnsureDir(path string) error
- func ExtractNewOutput(before, after string) string
- func FindGitRoot(startDir string) (string, error)
- func FormatBytes(b int64) string
- func MustParseDuration(s string) time.Duration
- func NTMDir() (string, error)
- func ParseDuration(s string) (time.Duration, error)
- func ParseDurationWithDefault(s string, defaultUnit time.Duration, flagName string) (time.Duration, error)
- func SafeSlice(s string, maxLen int) string
- func SanitizeFilename(name string) string
- func Truncate(s string, n int) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtomicWriteFile ¶
AtomicWriteFile writes data to a file atomically by writing to a temp file and then renaming it. This ensures the file is either fully written or not updated at all, preventing corruption on crash.
func ExtractNewOutput ¶
ExtractNewOutput isolates the text added to the pane after the before state. It handles cases where the buffer has scrolled (before is not a simple prefix).
func FindGitRoot ¶
FindGitRoot attempts to find the root of the git repository containing the given directory. Returns empty string if not found.
func FormatBytes ¶
FormatBytes formats bytes in a human-readable way (e.g., "1.5 KB")
func MustParseDuration ¶
MustParseDuration parses a duration string or panics. Use only for compile-time constants or values that are guaranteed to be valid.
func ParseDuration ¶
ParseDuration parses human-friendly duration strings. Supports: 30s, 5m, 1h, 1d, 1w and standard Go durations (e.g., 1h30m).
Examples:
- "30s" -> 30 seconds
- "5m" -> 5 minutes
- "1h" -> 1 hour
- "1d" -> 24 hours
- "1w" -> 7 days
- "1h30m" -> 1 hour 30 minutes (standard Go format)
func ParseDurationWithDefault ¶
func ParseDurationWithDefault(s string, defaultUnit time.Duration, flagName string) (time.Duration, error)
ParseDurationWithDefault parses a duration string, handling bare numbers with a default unit for backward compatibility. Emits a deprecation warning to stderr when bare numbers are used.
Parameters:
- s: the duration string to parse
- defaultUnit: the unit to use for bare numbers (e.g., time.Second, time.Millisecond)
- flagName: the flag name for the deprecation warning message
Examples:
ParseDurationWithDefault("30s", time.Second, "timeout") -> 30s, no warning
ParseDurationWithDefault("5000ms", time.Millisecond, "timeout") -> 5s, no warning
ParseDurationWithDefault("30", time.Second, "timeout") -> 30s, warning printed
func SafeSlice ¶
SafeSlice truncates a string to maxLen bytes, ensuring the cut is at a rune boundary. Unlike Truncate, it does not add an ellipsis.
func SanitizeFilename ¶
SanitizeFilename makes a string safe for use as a filename.
Types ¶
This section is empty.