utils

package
v1.10.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserInitiatedExit = errors.New("user exit")

Functions

func AttemptPrettyPrint

func AttemptPrettyPrint(w io.Writer, chatMessage pub_models.Message, username string, raw bool) error

AttemptPrettyPrint by first checking if the glow command is available, and if so, pretty print the chat message. If not found, simply print the message as is.

If w is nil, os.Stdout is used.

func ClearTermTo

func ClearTermTo(w io.Writer, termWidth, upTo int) error

ClearTermTo a certain amount of rows upwards by printing termWidth amount of empty spaces.

If w is nil, os.Stdout is used.

func Colorize

func Colorize(color, s string) string

Colorize wraps s with the given ANSI color code unless NO_COLOR is set or color is empty.

func CreateConfigDir

func CreateConfigDir(configPath string) error

func CreateFile

func CreateFile[T any](path string, toCreate *T) error

func DeleteRange

func DeleteRange[T any](s []T, start, end int) ([]T, error)

DeleteRange removes elements in [start, end] (inclusive) from the slice and returns the new slice. Panics if indices are invalid. Works for any slice type (Go 1.18+).

func GetClaiCacheDir

func GetClaiCacheDir() (string, error)

GetClaiCacheDir returns the path to the clai cache directory. The directory is located inside the user's cache directory as <UserCacheDir>/clai, unless overridden by CLAI_CACHE_DIR.

func GetClaiConfigDir

func GetClaiConfigDir() (string, error)

GetClaiConfigDir returns the path to the clai configuration directory. The directory is located inside the user's configuration directory as <UserConfigDir>/.clai, unless overridden by CLAI_CONFIG_DIR.

func GetFirstTokens

func GetFirstTokens(prompt []string, n int) []string

GetFirstTokens returns the first n tokens of the prompt, or the whole prompt if it has less than n tokens

func LoadConfigFromFile

func LoadConfigFromFile[T any](
	configDirPath,
	configFileName string,
	migrationCb func(string) error,
	dflt *T,
) (T, error)

func LoadTheme

func LoadTheme(configDirPath string) error

LoadTheme loads (and possibly creates) the theme.json file within the config dir. It is safe to call multiple times.

func NoColor

func NoColor() bool

NoColor reports whether color output should be disabled.

func Prompt

func Prompt(stdinReplace string, args []string) (string, error)

Prompt returns the prompt by checking all the arguments and stdin. If there is no arguments, but data in stdin, stdin will become the prompt. If there are arguments and data in stdin, all stdinReplace tokens will be substituted with the data in stdin

func RandomPrefix

func RandomPrefix() string

func ReadAndUnmarshal

func ReadAndUnmarshal[T any](filePath string, config *T) error

ReadAndUnmarshal by first finding the file, then attempting to read + unmarshal to T

func ReadUserInput

func ReadUserInput() (string, error)

ReadUserInput and return on interrupt channel

func ReplaceTildeWithHome

func ReplaceTildeWithHome(s string) (string, error)

func ReturnNonDefault

func ReturnNonDefault[T comparable](a, b, defaultVal T) (T, error)

func RoleColor

func RoleColor(role string) string

RoleColor returns the theme color for a chat role.

func SelectFromTable

func SelectFromTable[T any](header string, items []T,
	choicesFormat string,
	rowFormater func(int, T) (string, error),
	pageSize int,
	onlyOneSelect bool,
) ([]int, error)

SelectFromTable by: 1. Listing rows according to rowFormater 2. Returning a list of chosen numbers

Valid inputs:

  • nr = int < len(items)
  • nr,nr,nr - This selects multiple numbers
  • nr:nr,nr,nr:nr - This selects two ranges of nr, as well as a singular nr

func ShortenedOutput

func ShortenedOutput(out string, maxShortenedNewlines int) string

ShortenedOutput returns a shortened version of the output

func TermWidth

func TermWidth() (int, error)

TermWidth returns the current terminal width.

In CI / tests there is often no TTY attached, so ioctl(TIOCGWINSZ) returns "inappropriate ioctl for device". In that case we fall back to a sane default width (80) or the value from $COLUMNS if present.

func ThemeBreadtextColor

func ThemeBreadtextColor() string

func ThemeConfigPath

func ThemeConfigPath(configDirPath string) string

ThemeConfigPath returns the fully qualified theme.json path.

func ThemePrimaryColor

func ThemePrimaryColor() string

func ThemeSecondaryColor

func ThemeSecondaryColor() string

func UpdateMessageTerminalMetadata

func UpdateMessageTerminalMetadata(msg string, line *string, lineCount *int, termWidth int)

UpdateMessageTerminalMetadata updates the terminal metadata. Meaning the lineCount, to eventually clear the terminal

func WidthAppropriateStringTrunc

func WidthAppropriateStringTrunc(toShorten, prefix string, padding int) (string, error)

func WidthAppropriateStringTruncColored

func WidthAppropriateStringTruncColored(toShorten, prefix, prefixColor, truncColor string, padding int) (string, error)

WidthAppropriateStringTruncColored is like WidthAppropriateStringTrunc but allows coloring of the prefix and the truncation infix.

prefixColor and truncColor are raw ANSI sequences (or empty). Colors are disabled when NO_COLOR is truthy.

func WriteFile

func WriteFile[T any](path string, toWrite *T) error

Types

type ContextKey

type ContextKey string
const (
	ContextCancelKey ContextKey = "contextCancel"
)

type Theme

type Theme struct {
	Primary   string `json:"primary"`
	Secondary string `json:"secondary"`
	Breadtext string `json:"breadtext"`

	RoleSystem string `json:"roleSystem"`
	RoleUser   string `json:"roleUser"`
	RoleTool   string `json:"roleTool"`
	RoleOther  string `json:"roleOther"`
}

Theme holds ANSI color configuration for terminal output. Values are raw ANSI escape sequences (e.g. "\u001b[38;2;120;140;160m").

This file is loaded from <clai-config-dir>/theme.json on startup. If NO_COLOR is set truthy, all colorization should be disabled.

Keep this config stable; new fields should be appended with defaults. Users can customize their own theme.json.

Jump to

Keyboard shortcuts

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