util

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package util provides shared utility functions for ntm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWriteFile

func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error

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 EnsureDir

func EnsureDir(path string) error

EnsureDir ensures that a directory exists, creating it if necessary.

func ExtractNewOutput

func ExtractNewOutput(before, after string) string

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

func FindGitRoot(startDir string) (string, error)

FindGitRoot attempts to find the root of the git repository containing the given directory. Returns empty string if not found.

func FormatBytes

func FormatBytes(b int64) string

FormatBytes formats bytes in a human-readable way (e.g., "1.5 KB")

func MustParseDuration

func MustParseDuration(s string) time.Duration

MustParseDuration parses a duration string or panics. Use only for compile-time constants or values that are guaranteed to be valid.

func NTMDir

func NTMDir() (string, error)

NTMDir returns the path to the ~/.ntm directory.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

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

func SafeSlice(s string, maxLen int) string

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

func SanitizeFilename(name string) string

SanitizeFilename makes a string safe for use as a filename.

func Truncate

func Truncate(s string, n int) string

Truncate shortens a string to maxLen with ellipsis. Uses three ASCII periods "..." to indicate truncation.

Types

This section is empty.

Jump to

Keyboard shortcuts

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