Documentation
¶
Index ¶
- Constants
- Variables
- func AllNils[T comparable](vals []*T) bool
- func CreateFilePathAndWriteJson(path string, jsonData interface{}) error
- func CreateFilePathAndWriteString(path string, str string) error
- func DeleteFileIfExists(relativePath string) error
- func DisplayWidth(s string) int
- func Dump(item any) string
- func ExpandTabs(line string) (string, []int)
- func ExpandTilde(path string) string
- func FileExists(path string) bool
- func Int64Max(a, b int64) int64
- func Int64Min(a, b int64) int64
- func IntMax(a, b int) int
- func IntMin(a, b int) int
- func IsBlank(str string) bool
- func IsRegularFile(path string) bool
- func IsTerminal(f *os.File) bool
- func IsWindows() bool
- func LoadJson(path string) (interface{}, error)
- func Memoize[T any](f func() T) func() T
- func NormalizeLineEndings(s string) string
- func NormalizePath(path string) string
- func NotNil[T comparable](val *T, nilProvider func() T) bool
- func NumIsAre(values int) string
- func OSName() string
- func Pluralize(count int, singular string) string
- func PluralizeCustom(count int, singular string, plural string) string
- func Reverse(str string) string
- func ShortenPathLeft(path string, width int) string
- func SliceColumns(s string, start, end int) string
- func SortedKeys[T any](m map[string]T) []string
- func StrLen(str string) int
- func ToAbsoluteNormalizedPath(path string) string
- func ToAbsolutePath(path string) string
- func Truncate(str string, maxLen int64) string
- func Wrap(s string, width int) []string
- func WrapPrefixed(s, first, cont string, width int) []string
- type ControllingTerminal
- type LoadFileResult
- type Rgb
- type Stack
Constants ¶
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 ¶
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() )
var ( IsTty = checkTty() TerminalIsUtf8 = checkTerminalUtf8() )
Functions ¶
func AllNils ¶
func AllNils[T comparable](vals []*T) bool
func DeleteFileIfExists ¶
func DisplayWidth ¶ added in v0.12.0
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 ExpandTabs ¶ added in v0.12.0
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
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 IsRegularFile ¶ added in v0.9.0
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
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 NormalizeLineEndings ¶ added in v0.7.1
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
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 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 ShortenPathLeft ¶ added in v0.12.0
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
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 StrLen ¶
Simple len(str) call counts bytes, not runes, so e.g. emojis gets counted as multiple characters
func ToAbsoluteNormalizedPath ¶ added in v0.7.1
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 Wrap ¶ added in v0.12.0
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
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 ¶
func LoadFile ¶
func LoadFile(path string) LoadFileResult