expr/control.go

66 lines
1.5 KiB
Go
Raw Normal View History

// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// control.go
2024-04-02 05:02:15 +02:00
package expr
import "strings"
// Preset control variables
2024-04-02 05:02:15 +02:00
const (
ControlLastResult = "last"
ControlBoolShortcut = "_bool_shortcut"
ControlImportPath = "_import_path"
ControlPluginPath = "_plugin_path"
)
// Other control variables
const (
control_export_all = "_export_all"
2024-04-02 06:49:16 +02:00
)
// Initial values
const (
init_import_path = "~/.local/lib/go-pkg/expr/sources:/usr/local/lib/go-pkg/expr/sources:/usr/lib/go-pkg/expr/sources"
init_plugin_path = "~/.local/lib/go-pkg/expr/plugins:/usr/local/lib/go-pkg/expr/plugins:/usr/lib/go-pkg/expr/plugins"
2024-04-02 05:02:15 +02:00
)
func initDefaultVars(ctx ExprContext) {
ctx.SetVar(ControlBoolShortcut, true)
ctx.SetVar(ControlImportPath, init_import_path)
ctx.SetVar(ControlPluginPath, init_plugin_path)
2024-04-02 05:02:15 +02:00
}
func enable(ctx ExprContext, name string) {
2024-04-02 05:02:15 +02:00
if strings.HasPrefix(name, "_") {
ctx.SetVar(name, true)
2024-04-02 05:02:15 +02:00
} else {
ctx.SetVar("_"+name, true)
2024-04-02 05:02:15 +02:00
}
}
func disable(ctx ExprContext, name string) {
2024-04-02 05:02:15 +02:00
if strings.HasPrefix(name, "_") {
ctx.SetVar(name, false)
2024-04-02 05:02:15 +02:00
} else {
ctx.SetVar("_"+name, false)
2024-04-02 05:02:15 +02:00
}
}
func isEnabled(ctx ExprContext, name string) (status bool) {
if v, exists := ctx.GetVar(name); exists {
2024-04-02 05:02:15 +02:00
if b, ok := v.(bool); ok {
status = b
}
}
return
}
2024-04-02 06:49:16 +02:00
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
2024-04-02 06:49:16 +02:00
var v any
if v, exists = ctx.GetVar(name); exists {
2024-04-02 06:49:16 +02:00
s, exists = v.(string)
}
return
}