Documentation
¶
Index ¶
- Constants
- Variables
- func AttemptPrettyPrint(w io.Writer, chatMessage pub_models.Message, username string, raw bool) error
- func ClearLine(w io.Writer)
- func ClearTermTo(w io.Writer, upTo int) error
- func Colorize(color, s string) string
- func ConfigDirPaths() []string
- func CreateConfigDir(configPath string) error
- func CreateFile[T any](path string, toCreate *T) error
- func DeleteRange[T any](s []T, start, end int) ([]T, error)
- func EscapeEditorString(edited string) string
- func GetClaiCacheDir() (string, error)
- func GetClaiConfigDir() (string, error)
- func GetFirstTokens(prompt []string, n int) []string
- func LoadConfigFromFile[T any](configDirPath, configFileName string, migrationCb func(string) error, dflt *T) (T, error)
- func LoadTheme(configDirPath string) error
- func NoColor() bool
- func NotificationBellEnabled() bool
- func PrepareDisplayMessage(msg pub_models.Message) pub_models.Message
- func Prompt(stdinReplace string, args []string) (string, error)
- func RandomPrefix() string
- func ReadAndUnmarshal[T any](filePath string, config *T) error
- func ReadUserInput() (string, error)
- func ReplaceTildeWithHome(s string) (string, error)
- func ResolveConfigDirPath(configDir string, components []string) (string, error)
- func ReturnNonDefault[T comparable](a, b, defaultVal T) (T, error)
- func RoleColor(role string) string
- func SelectFromTable[T any](header string, paginator Paginator[T], selectionType string, ...) ([]int, error)
- func SelectFromTableAt[T any](header string, paginator Paginator[T], selectionType string, ...) ([]int, int, error)
- func ShortenedOutput(out string, maxShortenedNewlines int) string
- func TermWidth() (int, error)
- func ThemeBreadtextColor() string
- func ThemeConfigPath(configDirPath string) string
- func ThemePrimaryColor() string
- func ThemeSecondaryColor() string
- func ThemeTableItems() int
- func UnescapeEditorString(toEdit string) string
- func UpdateMessageTerminalMetadata(msg string, line *string, lineCount *int, termWidth int)
- func UseReadUserInputForTests(fn func() (string, error)) func()
- func WidthAppropriateStringTrunc(toShorten, prefix string, padding int) (string, error)
- func WidthAppropriateStringTruncColored(toShorten, prefix, prefixColor, truncColor string, padding int) (string, error)
- func WriteFile[T any](path string, toWrite *T) error
- type ContextKey
- type Paginator
- type TableAction
- type Theme
Constants ¶
const MaxShortenedNewlines = 5
Variables ¶
var ( ErrUserInitiatedExit = errors.New("user exit") ErrBack = errors.New("back") )
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 ClearLine ¶ added in v1.10.11
ClearLine writes a carriage return followed by the ANSI "clear to end of line" escape, so the next write starts at column 0 on a clean line. Useful for single-line progress indicators that may vary in length.
func ClearTermTo ¶
ClearTermTo clears upTo lines upwards, leaving the cursor at column 0 of the last cleared line. Each line is cleared via ClearLine.
If w is nil, os.Stdout is used.
func Colorize ¶
Colorize wraps s with the given ANSI color code unless NO_COLOR is set or color is empty.
func ConfigDirPaths ¶
func ConfigDirPaths() []string
func CreateConfigDir ¶
func CreateFile ¶
func DeleteRange ¶
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 GetClaiCacheDir ¶
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 ¶
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 ¶
GetFirstTokens returns the first n tokens of the prompt, or the whole prompt if it has less than n tokens
func LoadConfigFromFile ¶
func LoadTheme ¶
LoadTheme loads (and possibly creates) the theme.json file within the config dir. It is safe to call multiple times.
func NotificationBellEnabled ¶
func NotificationBellEnabled() bool
func PrepareDisplayMessage ¶ added in v1.10.10
func PrepareDisplayMessage(msg pub_models.Message) pub_models.Message
func Prompt ¶
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 ¶
ReadAndUnmarshal by first finding the file, then attempting to read + unmarshal to T
func ReadUserInput ¶
ReadUserInput and return on interrupt channel
func ReplaceTildeWithHome ¶
func ResolveConfigDirPath ¶
func ReturnNonDefault ¶
func ReturnNonDefault[T comparable](a, b, defaultVal T) (T, error)
func SelectFromTable ¶
func SelectFromTable[T any]( header string, paginator Paginator[T], selectionType string, rowFormater func(int, T) (string, error), pageSize int, onlyOneSelect bool, additionalTableActions []TableAction, out io.Writer, backLabel string, ) ([]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 SelectFromTableAt ¶ added in v1.10.11
func SelectFromTableAt[T any]( header string, paginator Paginator[T], selectionType string, rowFormater func(int, T) (string, error), pageSize int, onlyOneSelect bool, additionalTableActions []TableAction, out io.Writer, backLabel string, startPage int, ) ([]int, int, error)
SelectFromTableAt is SelectFromTable opened at startPage (clamped to the valid range). It also returns the page shown when the selection (or error) happened, so callers can reopen the table where the user left off.
func ShortenedOutput ¶
ShortenedOutput returns a shortened version of the output
func TermWidth ¶
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 ¶
ThemeConfigPath returns the fully qualified theme.json path.
func ThemePrimaryColor ¶
func ThemePrimaryColor() string
func ThemeSecondaryColor ¶
func ThemeSecondaryColor() string
func ThemeTableItems ¶ added in v1.10.11
func ThemeTableItems() int
func UnescapeEditorString ¶ added in v1.10.11
func UpdateMessageTerminalMetadata ¶
UpdateMessageTerminalMetadata updates the terminal metadata. Meaning the lineCount, to eventually clear the terminal
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.
Types ¶
type Paginator ¶ added in v1.10.4
type Paginator[T any] interface { // contains filtered or unexported methods }
func SlicePaginator ¶ added in v1.10.4
type TableAction ¶ added in v1.10.4
type TableAction struct {
// Format for the the menu item to be printed. Include short and long Format. Example [b]ack
Format string
// Short name back -> "b"
Short string
// Long name "back"
Long string
// AdditionalHotkeys which will trigger action
AdditionalHotkeys string
Action func() error
// Filter, when non-nil, turns this action into a toggleable predicate filter:
// pressing it filters the table to rows for which Filter reports true, and
// pressing it again (or enter while any filter is active) clears it. The
// predicate receives each original row as an `any`. When Filter is set, Action
// is ignored.
Filter func(any) bool
// EmptyMessage is appended in the prompt when this predicate filter is active
// and yields zero rows.
EmptyMessage string
}
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.