utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateHash

func CalculateHash(filePath string) (string, error)

CalculateHash computes the SHA-256 hash of a file. It uses io.Copy to stream the file into the hasher to minimize memory usage.

func CleanComment

func CleanComment(raw string) string

CleanComment strips comment markers and normalizes docstrings across Go, TS, and Python.

func CompactPath

func CompactPath(path string) string

CompactPath strips common prefixes like src/, lib/, internal/ from a path.

func CountLines

func CountLines(s string) int

CountLines counts the number of lines in s.

func EstimateTokens

func EstimateTokens(s string) int

EstimateTokens estimates token count using ~4 chars/token heuristic.

func ExtractCodeBlock

func ExtractCodeBlock(s string) string

ExtractCodeBlock isolates code from Markdown blocks or returns the raw string if no blocks found. Unlike ExtractJSON, it does not attempt to match braces, making it safe for Go code.

func ExtractJSON

func ExtractJSON(s string) string

ExtractJSON isolates a JSON block from potentially conversational text.

func ExtractLines

func ExtractLines(filePath string, startLine, endLine int) (string, error)

ExtractLines reads a file and returns the content between startLine and endLine (inclusive, 1-indexed).

func ExtractList

func ExtractList(text, header string) []string

ExtractList extracts a bulleted list following a specific header.

func FormatTokens

func FormatTokens(n int) string

FormatTokens formats a token count for display: "1.2M", "59.2K", "694".

func GetRepoName

func GetRepoName(ctx context.Context) string

GetRepoName returns the name of the repository (e.g., "scouter") from origin remote.

func GetRepoRoot

func GetRepoRoot() (string, error)

GetRepoRoot locates the dynamic anchor of the project (go.mod or .git).

func HashString

func HashString(s string) string

HashString computes the SHA-256 hash of a string.

func OkConfirmation

func OkConfirmation(action, detail string) string

OkConfirmation produces a compact confirmation message.

func ParseRating

func ParseRating(s string) (float64, error)

ParseRating extracts a rating like "8.5 / 10" from text.

func RestoreFile

func RestoreFile(ctx context.Context, path string) error

RestoreFile executes 'git restore <file>' to revert changes in a specific file.

func SafeCommand

func SafeCommand(ctx context.Context, name string, args ...string) (*exec.Cmd, error)

SafeCommand creates a validated exec.Cmd to prevent command injection and unauthorized execution. It enforces an allow-list of binaries and sanitizes arguments for dangerous shell characters.

func SafeUintToInt

func SafeUintToInt(u uint) (int, error)

SafeUintToInt safely converts a uint to an int, preventing overflow on 32-bit systems.

func SanitizeFTS

func SanitizeFTS(q string) string

SanitizeFTS sanitizes a raw search string for safe use in SQLite FTS5 MATCH expressions. It escapes double quotes and wraps the query in double quotes to neutralize control characters.

func StringHash

func StringHash(s string) string

StringHash computes the SHA-256 hash of a string.

func StripANSI

func StripANSI(s string) string

StripANSI removes ANSI escape codes from s.

func SymbolSignatureHash

func SymbolSignatureHash(symbol, path, signature string) string

func Truncate

func Truncate(s string, max int) string

Truncate truncates s to max runes, appending "..." if truncated.

func ValidatePath

func ValidatePath(path string) (string, error)

ValidatePath implements "Project Jail" (CWE-22 prevention) with absolute signal.

Types

type DiffRange

type DiffRange struct {
	Path      string
	StartLine int
	EndLine   int
}

DiffRange represents a range of modified lines in a file.

func GetLocalChanges

func GetLocalChanges(ctx context.Context) ([]DiffRange, error)

GetLocalChanges parses 'git diff HEAD' to find modified line ranges. Using HEAD ensures we include both staged and unstaged changes (Divine Fix).

type LazyRegex

type LazyRegex struct {
	// contains filtered or unexported fields
}

LazyRegex compiles a regex pattern on first use and caches the result.

func NewLazyRegex

func NewLazyRegex(pattern string) *LazyRegex

NewLazyRegex creates a LazyRegex that will compile pattern on first use.

func (*LazyRegex) Re

func (lr *LazyRegex) Re() *regexp.Regexp

Re returns the compiled regexp, compiling it on first call. Panics if the pattern is invalid.

Jump to

Keyboard shortcuts

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