utils

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TuskDirName    = ".tusk"
	TracesSubDir   = "traces"
	LogsSubDir     = "logs"
	ConfigFileName = "config.yaml"
)
View Source
const NoWrapMarker = "\x00NOWRAP\x00"
View Source
const SoftLineBreak = "\x00SOFT\x00"

SoftLineBreak marks a continuation line (join directly when copying, no newline) The space (if any) is preserved in the content itself

Variables

View Source
var PossibleTraceDirs = []string{
	".tusk/traces",
	"traces",
	"tmp",
	".",
}

List of directories to search for trace files

Functions

func BatchSlice

func BatchSlice[T any](items []T, batchSize int) [][]T

BatchSlice splits a slice into batches of the specified size. The last batch may contain fewer elements if the slice length is not evenly divisible by batchSize.

func CIWarning

func CIWarning(message string)

CIWarning emits a warning annotation visible in the CI provider's UI. On providers without annotation support, this is a no-op — the caller should log the message separately via log.Stderrln or similar.

Supported providers:

  • GitHub Actions: ::warning:: annotation (shows in workflow summary)
  • Azure Pipelines: ##vso[task.logissue] (shows in pipeline summary)
  • Buildkite: buildkite-agent annotate --style warning (shows on build page)
  • GitLab CI: ANSI yellow text (visible in job log, no annotation UI)

func CopyToClipboard

func CopyToClipboard(text string) error

CopyToClipboard copies text to the system clipboard. It tries OSC52 first (for remote terminals), then falls back to OS clipboard.

func EnsureDir

func EnsureDir(dir string) error

EnsureDir creates a directory if it doesn't exist

func EnvDefault

func EnvDefault(k, def string) string

func FindTraceFile

func FindTraceFile(traceID string, filename string) (string, error)

FindTraceFile searches for a JSONL trace file containing the given trace ID. If filename is provided, it tries that first before searching

func FindTuskRoot

func FindTuskRoot() string

FindTuskRoot traverses up the directory tree looking for a .tusk directory Returns the directory containing .tusk, or empty string if not found

func FormatJSONDiff

func FormatJSONDiff(expected, actual any) string

FormatJSONDiff creates a git-style unified diff between two JSON values

func FormatJSONDiffPlain added in v0.2.8

func FormatJSONDiffPlain(expected, actual any) string

FormatJSONDiffPlain creates a plain unified diff between two JSON values without ANSI colors or box borders. Suitable for writing to files consumed by coding agents.

func FormatJSONForDiff

func FormatJSONForDiff(v any) string

FormatJSONForDiff formats a JSON value with proper indentation for diff display

func GenerateDeterministicHash

func GenerateDeterministicHash(value any) string

GenerateDeterministicHash creates a deterministic hash of any JSON-serializable value

func GenerateSchemaAndHash

func GenerateSchemaAndHash(value any) (schema any, valueHash string, schemaHash string)

GenerateSchemaAndHash generates JSON schema and deterministic hash for any value

func GetGitRootDir added in v0.2.12

func GetGitRootDir() (string, error)

GetGitRootDir returns the root of the current git repository. Returns empty string and an error if not in a git repo.

func GetLogsDir

func GetLogsDir() string

GetLogsDir returns the logs directory path

func GetPossibleTraceDirs

func GetPossibleTraceDirs() []string

GetPossibleTraceDirs returns the list of directories to search for trace files, preferring override first.

func GetTracesDir

func GetTracesDir() string

GetTracesDir returns the traces directory path

func GetTuskDir

func GetTuskDir() string

GetTuskDir returns the .tusk directory path (either local or in home directory)

func GetTuskRoot

func GetTuskRoot() string

GetTuskRoot returns the root directory containing .tusk, or current directory if not found

func IsSoftLineBreak

func IsSoftLineBreak(line string) bool

IsSoftLineBreak checks if a line is a continuation from wrapping

func IsTerminal

func IsTerminal() bool

func JoinWrappedLines

func JoinWrappedLines(lines []string) string

JoinWrappedLines joins lines respecting soft line breaks, SoftLineBreak lines are joined directly

func MarkNonWrappable

func MarkNonWrappable(text string) string

MarkNonWrappable adds an invisible marker to indicate text should not be wrapped

func ParseProtobufSpanFromJSON

func ParseProtobufSpanFromJSON(jsonData []byte) (*core.Span, error)

ParseProtobufSpanFromJSON parses a JSON line into a protobuf Span

func ParseSpansFromFile

func ParseSpansFromFile(filename string, filter SpanFilter) ([]*core.Span, error)

ParseSpansFromFile reads a JSONL trace file and returns spans matching the filter. If the file is malformed, it returns an error.

func PromptUserChoice

func PromptUserChoice(question string, options []string) int

PromptUserChoice displays a question with numbered options and returns the index of the selected option. Returns 0 if input is invalid or empty (defaults to first option).

func ReduceByMatchImportance

func ReduceByMatchImportance(value any, schema *core.JsonSchema) any

ReduceByMatchImportance returns a copy of 'value' keeping only fields with matchImportance != 0. Schema is a JsonSchema proto with optional per-field "matchImportance": 0|1 (default 1). Supports nested objects and arrays (uses 'items' schema for arrays if present).

func ReduceSchemaByMatchImportance

func ReduceSchemaByMatchImportance(schema *core.JsonSchema) *core.JsonSchema

ReduceSchemaByMatchImportance returns a copy of the schema with only fields that have matchImportance != 0 Theoretically match importance could be a decimal, but we don't support that yet.

func RemoveHeadersFromInputValue

func RemoveHeadersFromInputValue(inputValue any) any

RemoveHeadersFromInputValue removes non-critical headers for header-agnostic matching but preserves headers that affect response format like Accept and Content-Type

func RenderMarkdown

func RenderMarkdown(markdown string) string

func RenderMarkdownWithWidth

func RenderMarkdownWithWidth(markdown string, width int) string

RenderMarkdownWithWidth renders markdown with a specific word wrap width. Uses width of 80 as a fallback if width <= 0.

func ResolveTuskPath

func ResolveTuskPath(path string) string

ResolveTuskPath resolves a path relative to the tusk root if it's relative, or returns it as-is if it's already absolute

func SetTracesDirOverride

func SetTracesDirOverride(dir string)

SetTracesDirOverride sets an explicit traces directory to use.

func StripANSI

func StripANSI(s string) string

StripANSI removes ANSI escape sequences from a string

func StripAllMarkers

func StripAllMarkers(text string) string

StripAllMarkers removes all internal markers from text

func StripNoWrapMarker

func StripNoWrapMarker(text string) string

StripNoWrapMarker removes the non-wrappable marker from text before display

func StripSoftLineBreak

func StripSoftLineBreak(text string) string

StripSoftLineBreak removes the soft line break marker

func TUICIMode

func TUICIMode() bool

TUICIMode returns true if TUSK_TUI_CI_MODE=1 is set. This enables CI-friendly TUI mode: forces TUI without a TTY, skips terminal size warnings, and auto-exits on completion.

func TruncateWithEllipsis

func TruncateWithEllipsis(text string, maxWidth int) string

func WrapLine

func WrapLine(text string, maxWidth int) []string

WrapLine wraps a single line of text to the specified width, trying to break at word boundaries This function is ANSI-aware and preserves color codes It avoids wrapping lines that appear to be formatted output (like diffs)

func WrapLines

func WrapLines(lines []string, maxWidth int) []string

WrapLines wraps multiple lines to fit within the specified width Returns a slice of wrapped lines with SoftLineBreak markers on continuation lines NoWrapMarker is stripped from output

func WrapText

func WrapText(content string, maxWidth int) string

WrapText wraps multiple lines of content to fit within the specified width

func WriteToSystemClipboard

func WriteToSystemClipboard(text string) error

WriteToSystemClipboard writes text to the OS clipboard with proper UTF-8 encoding. On macOS, it sets __CF_USER_TEXT_ENCODING to force pbcopy to interpret input as UTF-8 (the default is Mac OS Roman, which garbles multi-byte Unicode characters).

Types

type ProgressBar

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

ProgressBar shows a progress bar based on current/total counts

func NewProgressBar

func NewProgressBar(message string) *ProgressBar

NewProgressBar creates a new progress bar that outputs to stderr

func (*ProgressBar) Add

func (p *ProgressBar) Add(count int)

Add increments the current count by the given amount

func (*ProgressBar) Finish

func (p *ProgressBar) Finish(finalMessage string)

Finish stops the progress bar and shows a completion message

func (*ProgressBar) SetCurrent

func (p *ProgressBar) SetCurrent(current int)

SetCurrent sets the current count directly

func (*ProgressBar) SetTotal

func (p *ProgressBar) SetTotal(total int)

SetTotal sets the total count for the progress bar

func (*ProgressBar) Start

func (p *ProgressBar) Start()

Start initializes the progress bar (shows empty bar)

func (*ProgressBar) Stop

func (p *ProgressBar) Stop()

Stop clears the progress bar without a final message

type ProgressTracker

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

ProgressTracker handles progress reporting for paginated fetches

func NewProgressTracker

func NewProgressTracker(message string, interactive, quiet bool) *ProgressTracker

func (*ProgressTracker) Finish

func (pt *ProgressTracker) Finish(finalMessage string)

func (*ProgressTracker) SetTotal

func (pt *ProgressTracker) SetTotal(total int)

func (*ProgressTracker) Stop

func (pt *ProgressTracker) Stop()

func (*ProgressTracker) Update

func (pt *ProgressTracker) Update(current int)

type SpanFilter

type SpanFilter func(*core.Span) bool

SpanFilter is a function type for filtering spans during parsing

Jump to

Keyboard shortcuts

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