utils

package
v1.10.17 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const MaxShortenedNewlines = 5

Variables

View Source
var Live bool

Live controls whether macro mode switches to interactive stdin after consuming all macro inputs (true, default) or appends trailing "q\n" terminators for graceful auto-exit (false). Set by Setup() based on the --non-interactive flag.

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 the message has ReasoningContent, it is rendered with reasoning color before the main content.

If w is nil, os.Stdout is used.

func ConfigDirPaths

func ConfigDirPaths() []string

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 EscapeEditorString added in v1.10.11

func EscapeEditorString(edited string) string

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. After loading, stores the theme for consumer access.

func NewMacroReader added in v1.10.17

func NewMacroReader(inputs []string) io.Reader

NewMacroReader creates a reader that yields each input string as a line.

By default (Live=true), after all macro inputs are consumed the reader transparently switches to os.Stdin so the user continues interacting.

When Live is false (--non-interactive), 10 trailing "q\n" lines are appended for graceful termination through nested table loops.

Returns nil when inputs is empty or nil (caller should skip injection).

func NotificationBellEnabled

func NotificationBellEnabled() bool

func PrepareDisplayMessage added in v1.10.10

func PrepareDisplayMessage(msg pub_models.Message) pub_models.Message

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 ReplaceTildeWithHome

func ReplaceTildeWithHome(s string) (string, error)

func ResolveConfigDirPath

func ResolveConfigDirPath(configDir string, components []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 SaveBase64File added in v1.10.17

func SaveBase64File(prefix, dir, b64JSON, extension string) (string, error)

SaveBase64File decodes a base64 string and writes it to dir/prefix_random.extension. Falls back to /tmp on write failure.

func ShortenedOutput

func ShortenedOutput(out string, maxShortenedNewlines int) string

ShortenedOutput returns a shortened version of the output

func TableTheme added in v1.10.17

func TableTheme() table.Theme

TableTheme returns the table.Theme derived from the current global theme.

func ThemeConfigPath

func ThemeConfigPath(configDirPath string) string

ThemeConfigPath returns the fully qualified theme.json path.

func UnescapeEditorString added in v1.10.11

func UnescapeEditorString(toEdit string) 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 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"`
	RoleReasoning string `json:"roleReasoning"`
	RoleOther     string `json:"roleOther"`

	NotificationBell bool `json:"notificationBell"`

	TableItems int `json:"tableItems"`
}

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