builtin

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MPL-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Jobs = make(map[int]*Job)

	JobsMu sync.Mutex
)
View Source
var Builtins = map[string]*BuiltinCommand{}
View Source
var KeywordsDoc = map[string]KeywordInfo{
	"func": {
		Description: "定义函数",
		Usage:       "func 函数名(参数列表) { 函数体 }",
		Details:     "用于声明一个新的函数。支持位置参数,函数体内可以访问闭包环境中的变量。",
	},
	"return": {
		Description: "从函数返回",
		Usage:       "return [值]",
		Details:     "用于从当前执行的函数中退出,并可选地返回一个值给调用者。",
	},
	"if": {
		Description: "条件判断",
		Usage:       "if 条件 { 后果 } [else { 替代 }]",
		Details:     "根据布尔条件的真假来执行相应的代码块。支持可选的 else 分支。",
	},
	"else": {
		Description: "条件分支",
		Usage:       "if ... { ... } else { ... }",
		Details:     "if 语句的备选分支,当 if 条件不满足时执行。",
	},
	"for": {
		Description: "循环结构",
		Usage:       "for [条件] { 循环体 }",
		Details:     "基本的循环结构。如果省略条件,则为无限循环。",
	},
	"range": {
		Description: "迭代集合",
		Usage:       "for 变量 := range 集合 { ... }",
		Details:     "用于迭代数组或迭代器。支持数组 range(for i, v := range arr)和迭代器 range-over-func(for v := range iter(args))。",
	},
	"go": {
		Description: "启动 Goroutine (异步执行)",
		Usage:       "go 表达式",
		Details:     "在新的 Goroutine 中并发执行指定的函数调用或语句块。",
	},
	"var": {
		Description: "显式变量声明",
		Usage:       "var 变量名 [类型] [= 初始值]",
		Details:     "声明一个具有可选类型约束的变量。如果指定了类型,后续赋值必须匹配该类型。",
	},
	"const": {
		Description: "常量定义",
		Usage:       "const 常量名 = 值",
		Details:     "定义一个不可修改的常量值。",
	},
	"import": {
		Description: "导入 Go 标准库",
		Usage:       "import \"Go/包名\"",
		Details:     "导入 Go 标准库函数,编译时直接解析为原生 Go 调用。已支持的包:fmt、math、strings、strconv、os。",
	},
	"exec": {
		Description: "执行外部命令",
		Usage:       "exec <command> [args...] 或 exec(cmd)",
		Details:     "关键字形式:exec echo hello - 直接执行命令,参数按空格分割,支持引号。函数形式:exec(cmd) - 执行字符串命令。注意:exec \"...\" 已弃用。",
	},
	"export": {
		Description: "设置环境变量",
		Usage:       "export 变量名=值",
		Details:     "设置并导出环境变量,使其对子进程可见。",
	},
	"exit": {
		Description: "退出 Shell",
		Usage:       "exit [退出码]",
		Details:     "终止当前的 Kamishell 会话。可选提供退出状态码(默认为 0)。",
	},
	"true": {
		Description: "布尔真",
		Usage:       "true",
		Details:     "布尔逻辑值中的 '真'。",
	},
	"false": {
		Description: "布尔假",
		Usage:       "false",
		Details:     "布尔逻辑值中的 '假'。",
	},
	"nil": {
		Description: "空值",
		Usage:       "nil",
		Details:     "表示缺失的值或空指针。在错误处理中常用作无错误状态。",
	},
	":=": {
		Description: "短变量声明",
		Usage:       "变量名 := 初始值",
		Details:     "自动推断类型的变量声明并赋值。后续可以通过 '=' 重新赋值,但类型固定。",
	},
	"|": {
		Description: "管道操作符",
		Usage:       "命令1 | 命令2",
		Details:     "将前一个命令的标准输出重定向为后一个命令的标准输入。",
	},
	">": {
		Description: "重定向(覆盖)",
		Usage:       "命令 > 文件",
		Details:     "将命令的标准输出重定向到指定文件,并覆盖原有内容。",
	},
	">>": {
		Description: "重定向(追加)",
		Usage:       "命令 >> 文件",
		Details:     "将命令的标准输出重定向到指定文件,并追加到末尾。",
	},
	"&": {
		Description: "后台运行",
		Usage:       "命令 &",
		Details:     "在后台异步执行命令,不阻塞当前 shell。",
	},
}

Functions

func BoolFlag

func BoolFlag(fs *flag.FlagSet, m *CommandMeta, long, short string, def bool, desc string) *bool

BoolFlag registers a bool flag with both the FlagSet and metadata. Returns a pointer to the bool value. Short and long names both bind to the same variable.

func BoolFlagVar

func BoolFlagVar(fs *flag.FlagSet, m *CommandMeta, target *bool, long, short string, def bool, desc string)

BoolFlagVar registers a bool flag bound to an existing variable. Both short and long names bind to the same target.

func BuiltinHelpRequested

func BuiltinHelpRequested(args []string) bool

func BuiltinNames

func BuiltinNames() []string

func Cat

func Cat(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Cd

func Cd(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func CompleteExternalCommands

func CompleteExternalCommands(prefix string) []string

CompleteExternalCommands scans PATH for executable files matching the prefix.

func CompleteJob

func CompleteJob(id int)

func CompleteJobWithResult

func CompleteJobWithResult(id int, success bool, errMsg string)

func Cp

func Cp(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func DurationFlag

func DurationFlag(fs *flag.FlagSet, m *CommandMeta, long, short string, def time.Duration, desc string) *time.Duration

DurationFlag registers a duration flag with both the FlagSet and metadata.

func DurationFlagVar

func DurationFlagVar(fs *flag.FlagSet, m *CommandMeta, target *time.Duration, long, short string, def time.Duration, desc string)

DurationFlagVar registers a duration flag bound to an existing variable.

func Env

func Env(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Exit

func Exit(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Export

func Export(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func GetArch

func GetArch() string

func GetOS

func GetOS() string

func Grep

func Grep(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func HTTP

func HTTP(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func HandleBuiltinHelp

func HandleBuiltinHelp(cmd *BuiltinCommand, args []string, stdout io.Writer) bool

func Help

func Help(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func IntFlag

func IntFlag(fs *flag.FlagSet, m *CommandMeta, long, short string, def int, desc string) *int

IntFlag registers an int flag with both the FlagSet and metadata.

func IntFlagVar

func IntFlagVar(fs *flag.FlagSet, m *CommandMeta, target *int, long, short string, def int, desc string)

IntFlagVar registers an int flag bound to an existing variable.

func JobsCmd

func JobsCmd(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func KillAllJobs

func KillAllJobs()

KillAllJobs cancels all running jobs and marks them as killed. Used by signal handler during graceful shutdown.

func Ls

func Ls(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Mkdir

func Mkdir(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Mv

func Mv(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func PreprocessArgs

func PreprocessArgs(args []string) []string

func PrintBuiltinHelp

func PrintBuiltinHelp(cmd *BuiltinCommand, stdout io.Writer)

func Pwd

func Pwd(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func RegisterBuiltin

func RegisterBuiltin(cmd *BuiltinCommand)

RegisterBuiltin adds a new builtin command to the registry.

func RegisterJob

func RegisterJob(cmd string, cancel context.CancelFunc) int

func Rm

func Rm(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Sed

func Sed(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func SetArgCompleter

func SetArgCompleter(name string, c ArgCompleter)

SetArgCompleter sets the positional argument completer for a command.

func StringFlag

func StringFlag(fs *flag.FlagSet, m *CommandMeta, long, short string, def string, desc string) *string

StringFlag registers a string flag with both the FlagSet and metadata.

func StringFlagVar

func StringFlagVar(fs *flag.FlagSet, m *CommandMeta, target *string, long, short string, def string, desc string)

StringFlagVar registers a string flag bound to an existing variable.

func Touch

func Touch(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Type

func Type(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

func Which

func Which(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

Types

type ArgCompleter

type ArgCompleter func(cmdName string, argIndex int, prefix string) []string

ArgCompleter provides dynamic completion for positional arguments. argIndex is the 0-based index of the positional argument being completed.

type BuiltinCommand

type BuiltinCommand struct {
	Name        string
	Description string
	Usage       string
	Help        string
	Action      BuiltinFunc
}

BuiltinCommand represents a builtin tool with metadata.

type BuiltinFunc

type BuiltinFunc func(args []string, env Environment, stdin io.Reader, stdout io.Writer, stderr io.Writer) int

type CommandMeta

type CommandMeta struct {
	Flags     []*FlagMeta
	Completer ArgCompleter
}

CommandMeta stores all flag metadata and optional arg completer for a command.

func GetMeta

func GetMeta(name string) *CommandMeta

GetMeta returns the CommandMeta for the given command name, or nil.

func RegisterMeta

func RegisterMeta(name string) *CommandMeta

RegisterMeta returns the CommandMeta for the given command name. Creates a new one if it doesn't exist. Idempotent.

func (*CommandMeta) FindFlag

func (m *CommandMeta) FindFlag(token string) *FlagMeta

FindFlag returns the FlagMeta matching the given token (with or without dashes).

func (*CommandMeta) FindFlagByToken

func (m *CommandMeta) FindFlagByToken(token string) *FlagMeta

FindFlagByToken returns the FlagMeta matching the given token (with or without dashes).

func (*CommandMeta) LongFlags

func (m *CommandMeta) LongFlags() []string

LongFlags returns all long flag names for a command.

func (*CommandMeta) RegisterFlag

func (m *CommandMeta) RegisterFlag(long, short string, desc string, typ FlagType)

RegisterFlag manually registers a flag's metadata. Use this for custom flag.Value types where BoolFlag/StringFlag can't be used.

func (*CommandMeta) SetFlagCompleter

func (m *CommandMeta) SetFlagCompleter(long string, c ArgCompleter)

SetFlagCompleter sets a value completer for the flag with the given long name.

type Environment

type Environment interface {
	Set(name string, val any)
	Get(name string) (any, bool)
	SetString(name string, val string)
	GetString(name string) (string, bool)
}

type FlagMeta

type FlagMeta struct {
	Long           string       // long name, e.g. "recursive"
	Short          string       // short name, e.g. "r" (without dash)
	Desc           string       // description for completion display
	Type           FlagType     // bool, string, int, duration
	ValueCompleter ArgCompleter // optional: completes the value for this flag
}

FlagMeta stores completion-relevant metadata for a single flag.

type FlagType

type FlagType string

FlagType represents the type of a flag for completion purposes.

const (
	FlagBool     FlagType = "bool"
	FlagString   FlagType = "string"
	FlagInt      FlagType = "int"
	FlagDuration FlagType = "duration"
)

type Inspector

type Inspector interface {
	Inspect() string
}

type Job

type Job struct {
	ID      int
	Command string
	Status  string
	Error   string
	Cancel  context.CancelFunc
}

type KeywordInfo

type KeywordInfo struct {
	Description string
	Usage       string
	Details     string
}

Jump to

Keyboard shortcuts

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