2024-04-13 06:00:22 +02:00
|
|
|
// 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"
|
|
|
|
|
2024-04-06 03:01:17 +02:00
|
|
|
// Preset control variables
|
2024-04-02 05:02:15 +02:00
|
|
|
const (
|
2024-04-13 05:16:23 +02:00
|
|
|
ControlLastResult = "last"
|
|
|
|
ControlBoolShortcut = "_bool_shortcut"
|
|
|
|
ControlImportPath = "_import_path"
|
2024-04-06 03:01:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
|
2024-04-02 05:02:15 +02:00
|
|
|
)
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func initDefaultVars(ctx ExprContext) {
|
2024-04-13 05:16:23 +02:00
|
|
|
ctx.SetVar(ControlBoolShortcut, true)
|
|
|
|
ctx.SetVar(ControlImportPath, init_import_path)
|
2024-04-02 05:02:15 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func enable(ctx ExprContext, name string) {
|
2024-04-02 05:02:15 +02:00
|
|
|
if strings.HasPrefix(name, "_") {
|
2024-04-03 06:29:57 +02:00
|
|
|
ctx.SetVar(name, true)
|
2024-04-02 05:02:15 +02:00
|
|
|
} else {
|
2024-04-03 06:29:57 +02:00
|
|
|
ctx.SetVar("_"+name, true)
|
2024-04-02 05:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func disable(ctx ExprContext, name string) {
|
2024-04-02 05:02:15 +02:00
|
|
|
if strings.HasPrefix(name, "_") {
|
2024-04-03 06:29:57 +02:00
|
|
|
ctx.SetVar(name, false)
|
2024-04-02 05:02:15 +02:00
|
|
|
} else {
|
2024-04-03 06:29:57 +02:00
|
|
|
ctx.SetVar("_"+name, false)
|
2024-04-02 05:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func isEnabled(ctx ExprContext, name string) (status bool) {
|
2024-04-03 06:29:57 +02:00
|
|
|
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
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
|
2024-04-02 06:49:16 +02:00
|
|
|
var v any
|
2024-04-03 06:29:57 +02:00
|
|
|
if v, exists = ctx.GetVar(name); exists {
|
2024-04-02 06:49:16 +02:00
|
|
|
s, exists = v.(string)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|