98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// control.go
|
|
package expr
|
|
|
|
import "strings"
|
|
|
|
// Preset control variables
|
|
const (
|
|
ControlPreset = "_preset"
|
|
ControlLastResult = "last"
|
|
ControlBoolShortcut = "_bool_shortcut"
|
|
ControlSearchPath = "_search_path"
|
|
ControlParentContext = "_parent_context"
|
|
)
|
|
|
|
// Other control variables
|
|
const (
|
|
control_export_all = "_export_all"
|
|
)
|
|
|
|
// Initial values
|
|
const (
|
|
init_search_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
|
|
)
|
|
|
|
func initDefaultVars(ctx ExprContext) {
|
|
if _, exists := ctx.GetVar(ControlPreset); exists {
|
|
return
|
|
}
|
|
ctx.SetVar(ControlPreset, true)
|
|
ctx.SetVar(ControlBoolShortcut, true)
|
|
ctx.SetVar(ControlSearchPath, init_search_path)
|
|
}
|
|
|
|
func CtrlEnable(ctx ExprContext, name string) (currentStatus bool) {
|
|
if !strings.HasPrefix(name, "_") {
|
|
name = "_" + name
|
|
}
|
|
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
|
currentStatus, _ = v.(bool)
|
|
}
|
|
|
|
ctx.SetVar(name, true)
|
|
return currentStatus
|
|
}
|
|
|
|
func CtrlDisable(ctx ExprContext, name string) (currentStatus bool) {
|
|
if !strings.HasPrefix(name, "_") {
|
|
name = "_" + name
|
|
}
|
|
if v, exists := ctx.GetVar(name); exists && IsBool(v) {
|
|
currentStatus, _ = v.(bool)
|
|
}
|
|
|
|
ctx.SetVar(name, false)
|
|
return currentStatus
|
|
}
|
|
|
|
func CtrlSet(ctx ExprContext, name string, newValue any) (currentValue any) {
|
|
if !strings.HasPrefix(name, "_") {
|
|
name = "_" + name
|
|
}
|
|
currentValue, _ = ctx.GetVar(name)
|
|
|
|
ctx.SetVar(name, newValue)
|
|
for parent := ctx.GetParent(); parent != nil; parent = parent.GetParent() {
|
|
parent.SetVar(name, newValue)
|
|
}
|
|
return currentValue
|
|
}
|
|
|
|
func CtrlGet(ctx ExprContext, name string) (currentValue any) {
|
|
if !strings.HasPrefix(name, "_") {
|
|
name = "_" + name
|
|
}
|
|
currentValue, _ = ctx.GetVar(name)
|
|
return currentValue
|
|
}
|
|
|
|
func isEnabled(ctx ExprContext, name string) (status bool) {
|
|
if v, exists := ctx.GetVar(name); exists {
|
|
if b, ok := v.(bool); ok {
|
|
status = b
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
|
|
var v any
|
|
if v, exists = ctx.GetVar(name); exists {
|
|
s, exists = v.(string)
|
|
}
|
|
return
|
|
}
|