expr/preset.go

55 lines
1.1 KiB
Go
Raw Normal View History

2024-04-02 05:02:15 +02:00
// preset.go
package expr
import "strings"
// Preset variables
const (
preset_last_result = "_last"
preset_bool_shortcut = "_bool_shortcut"
2024-04-02 06:49:16 +02:00
preset_import_path = "_import_path"
)
// Initial values
const (
init_import_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
2024-04-02 05:02:15 +02:00
)
func initDefaultVars(ctx exprContext) {
ctx.SetValue(preset_bool_shortcut, true)
2024-04-02 06:49:16 +02:00
ctx.SetValue(preset_import_path, init_import_path)
2024-04-02 05:02:15 +02:00
}
func enable(ctx exprContext, name string) {
if strings.HasPrefix(name, "_") {
ctx.SetValue(name, true)
} else {
ctx.SetValue("_"+name, true)
}
}
func disable(ctx exprContext, name string) {
if strings.HasPrefix(name, "_") {
ctx.SetValue(name, false)
} else {
ctx.SetValue("_"+name, false)
}
}
func isEnabled(ctx exprContext, name string) (status bool) {
if v, exists := ctx.GetValue(name); exists {
if b, ok := v.(bool); ok {
status = b
}
}
return
}
2024-04-02 06:49:16 +02:00
func getPresetString(ctx exprContext, name string) (s string, exists bool) {
var v any
if v, exists = ctx.GetValue(name); exists {
s, exists = v.(string)
}
return
}