com

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const TabWidth = 4

TabWidth is how many columns a tab occupies when a tab-indented source line is rendered in a diagnostic. Rad's own formatter indents with spaces, so this only applies to hand-written source that uses tabs.

Variables

View Source
var (
	PlainF     = plain.FprintfFunc()
	GreenF     = green.FprintfFunc()
	GreenBoldF = greenBold.FprintfFunc()
	YellowF    = yellow.FprintfFunc()
	CyanF      = cyan.FprintfFunc()
	BoldF      = bold.FprintfFunc()
	FaintF     = faint.FprintfFunc()

	PlainS     = plain.SprintfFunc()
	GreenS     = green.SprintfFunc()
	GreenBoldS = greenBold.SprintfFunc()
	YellowS    = yellow.SprintfFunc()
	CyanS      = cyan.SprintfFunc()
	BoldS      = bold.SprintfFunc()
	FaintS     = faint.SprintfFunc()
)
View Source
var (
	IsTty          = checkTty()
	TerminalIsUtf8 = checkTerminalUtf8()
)

Functions

func AllNils

func AllNils[T comparable](vals []*T) bool

func CreateFilePathAndWriteJson

func CreateFilePathAndWriteJson(path string, jsonData interface{}) error

func CreateFilePathAndWriteString

func CreateFilePathAndWriteString(path string, str string) error

func DeleteFileIfExists

func DeleteFileIfExists(relativePath string) error

func DisplayWidth added in v0.12.0

func DisplayWidth(s string) int

DisplayWidth reports how many terminal columns a string occupies, ignoring ANSI escape sequences and accounting for wide (CJK) and zero-width runes.

Distinct from StrLen, which counts runes: a CJK character is one rune but two columns. Anything laying text out against a terminal width wants this one.

func Dump

func Dump(item any) string

func ExpandTabs added in v0.12.0

func ExpandTabs(line string) (string, []int)

ExpandTabs replaces tabs with spaces to the next TabWidth boundary and returns the expanded text alongside a byte-offset -> display-column table with len(line)+1 entries.

The table is the point: tree-sitter reports spans as byte offsets, but a caret has to land at a display column. Without it, tabs push the caret out of alignment and any wide character shifts everything after it.

func ExpandTilde added in v0.11.0

func ExpandTilde(path string) string

ExpandTilde resolves a leading "~" (i.e. exactly "~" or a "~/" prefix) to the user's home directory. Anything else is returned unchanged: "~user" (another user's home) is not supported and is left as a literal path rather than silently misexpanded, and a path like "~backup" is treated as a literal name. If the home dir can't be resolved, the path is returned untouched so the failure surfaces honestly at the os call site.

func FileExists

func FileExists(path string) bool

func Int64Max

func Int64Max(a, b int64) int64

func Int64Min

func Int64Min(a, b int64) int64

func IntMax

func IntMax(a, b int) int

func IntMin

func IntMin(a, b int) int

func IsBlank

func IsBlank(str string) bool

func IsRegularFile added in v0.9.0

func IsRegularFile(path string) bool

IsRegularFile returns true if the path exists and is a regular file (not a directory, device, pipe, socket, etc.).

func IsTerminal added in v0.12.0

func IsTerminal(f *os.File) bool

IsTerminal reports whether f is attached to a terminal.

func IsWindows added in v0.7.1

func IsWindows() bool

IsWindows returns true if running on Windows.

func LoadJson

func LoadJson(path string) (interface{}, error)

func Memoize

func Memoize[T any](f func() T) func() T

func NormalizeLineEndings added in v0.7.1

func NormalizeLineEndings(s string) string

NormalizeLineEndings converts Windows-style line endings (\r\n) to Unix-style (\n).

Used for script source code only - not for user data.

func NormalizePath added in v0.7.1

func NormalizePath(path string) string

NormalizePath converts OS-specific path separators to forward slashes. This ensures Rad scripts are portable across platforms - forward slashes work on all operating systems, including Windows.

Call this on any path before returning it to user code.

func NotNil

func NotNil[T comparable](val *T, nilProvider func() T) bool

this is the best way I can think of to do the 'typed nil' check...

func NumIsAre

func NumIsAre(values int) string

func OSName added in v0.12.0

func OSName() string

OSName returns the host OS as exposed to Rad scripts: "macos", "linux", or "windows". Other platforms pass through Go's runtime.GOOS value (e.g. "freebsd", "android"). "macos" is deliberately preferred over Go's "darwin", which script authors outside the Go ecosystem rarely recognize.

func Pluralize

func Pluralize(count int, singular string) string

follows some basic rules of english, use PluralizeCustom to override the plural.

func PluralizeCustom

func PluralizeCustom(count int, singular string, plural string) string

func Reverse

func Reverse(str string) string

func ShortenPathLeft added in v0.12.0

func ShortenPathLeft(path string, width int) string

ShortenPathLeft trims a path from the left to at most width columns, marking the cut with "...". Paths lose their least useful information at the front, so "/Users/me/src/proj/main.rad" shortens to ".../proj/main.rad".

func SliceColumns added in v0.12.0

func SliceColumns(s string, start, end int) string

SliceColumns returns the part of s between display columns [start, end). A wide rune straddling either boundary is dropped rather than half-drawn, so the result never exceeds end-start columns.

func SortedKeys

func SortedKeys[T any](m map[string]T) []string

func StrLen

func StrLen(str string) int

Simple len(str) call counts bytes, not runes, so e.g. emojis gets counted as multiple characters

func ToAbsoluteNormalizedPath added in v0.7.1

func ToAbsoluteNormalizedPath(path string) string

ToAbsoluteNormalizedPath expands ~ to home directory, resolves to absolute path, and normalizes to forward slashes. This is the standard way to process user-provided paths before returning them.

func ToAbsolutePath

func ToAbsolutePath(path string) string

func Truncate

func Truncate(str string, maxLen int64) string

func Wrap added in v0.12.0

func Wrap(s string, width int) []string

Wrap greedily fills lines of at most width columns, breaking on whitespace. A word wider than width on its own is hard-broken rather than allowed to overflow, so every returned line satisfies DisplayWidth(line) <= width. Always returns at least one line.

The single exception is a lone rune wider than width - a CJK character at width 1. It is emitted overflowing rather than dropped, since the alternative is silently losing text or looping forever. Callers wrapping to any sane terminal width never reach it.

Wrap takes PLAIN text. Colorize the lines it returns, never the string you pass in: a break landing inside an escape sequence would split it, and even a clean break leaves the sequence unterminated on the line before. Wrapping first and coloring after also keeps line breaks identical with and without --color, which is what lets a single snapshot cover both.

Greedy rather than balanced (go-tbl's WrapString minimizes raggedness) for two reasons: greedy is what terminal diagnostics conventionally look like, and it re-flows locally. Adding a word to a balanced paragraph moves every line, which turns every message edit into an unreviewable snapshot diff.

func WrapPrefixed added in v0.12.0

func WrapPrefixed(s, first, cont string, width int) []string

WrapPrefixed wraps s to width, prefixing the first line with first and the rest with cont. Each line is filled against its own prefix, so a wide "= help: " tag on line one doesn't shorten every line after it.

Types

type ControllingTerminal added in v0.12.0

type ControllingTerminal struct {
	In  *os.File
	Out *os.File
	// contains filtered or unexported fields
}

ControllingTerminal is the process's controlling terminal, opened directly rather than inherited through the standard streams.

In and Out are the same file on Unix, where /dev/tty is one read/write device, and distinct on Windows, whose console splits input and output into separate devices. Callers must therefore read from In and write to Out rather than treating either as the whole terminal.

func OpenControllingTerminal added in v0.12.0

func OpenControllingTerminal() (*ControllingTerminal, error)

OpenControllingTerminal opens the process's controlling terminal for reading and writing. This is what lets an interactive prompt still reach the user when stdin is a pipe or a here-string - notably RED-6's Bash embedding, where the script source itself arrives on stdin.

It fails when there is no controlling terminal, which is exactly the case rad wants to detect: CI, cron, and agent-driven runs.

func (*ControllingTerminal) Close added in v0.12.0

func (t *ControllingTerminal) Close()

type LoadFileResult

type LoadFileResult struct {
	Content   string
	SizeBytes int64
	Error     error
}

func LoadFile

func LoadFile(path string) LoadFileResult

type Rgb

type Rgb struct {
	R int
	G int
	B int
}

func NewRgb

func NewRgb(r, g, b int) Rgb

func NewRgb64

func NewRgb64(r, g, b int64) Rgb

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

func NewStack

func NewStack[T any]() *Stack[T]

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Jump to

Keyboard shortcuts

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