process static command / block commands. setmeta, setview

This commit is contained in:
sawka
2024-05-16 18:01:52 -07:00
parent 70dd4a1e30
commit d34ccfd7ab
5 changed files with 62 additions and 20 deletions
+22
View File
@@ -15,12 +15,16 @@ const CommandKey = "command"
const (
BlockCommand_Message = "message"
BlockCommand_SetView = "setview"
BlockCommand_SetMeta = "setmeta"
BlockCommand_Input = "controller:input"
)
var CommandToTypeMap = map[string]reflect.Type{
BlockCommand_Message: reflect.TypeOf(MessageCommand{}),
BlockCommand_Input: reflect.TypeOf(InputCommand{}),
BlockCommand_SetView: reflect.TypeOf(SetViewCommand{}),
BlockCommand_SetMeta: reflect.TypeOf(SetMetaCommand{}),
}
type BlockCommand interface {
@@ -67,3 +71,21 @@ type InputCommand struct {
func (ic *InputCommand) GetCommand() string {
return BlockCommand_Input
}
type SetViewCommand struct {
Command string `json:"command"`
View string `json:"view"`
}
func (svc *SetViewCommand) GetCommand() string {
return BlockCommand_SetView
}
type SetMetaCommand struct {
Command string `json:"command"`
Meta map[string]any `json:"meta"`
}
func (smc *SetMetaCommand) GetCommand() string {
return BlockCommand_SetMeta
}
+29 -12
View File
@@ -259,20 +259,8 @@ func (bc *BlockController) Run(bdata *BlockData) {
}
}()
messageCount := 0
for genCmd := range bc.InputCh {
switch cmd := genCmd.(type) {
case *MessageCommand:
fmt.Printf("MESSAGE: %s | %q\n", bc.BlockId, cmd.Message)
messageCount++
eventbus.SendEvent(application.WailsEvent{
Name: "block:ptydata",
Data: map[string]any{
"blockid": bc.BlockId,
"blockfile": "main",
"ptydata": base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("message %d\r\n", messageCount))),
},
})
case *InputCommand:
fmt.Printf("INPUT: %s | %q\n", bc.BlockId, cmd.InputData64)
if bc.ShellInputCh != nil {
@@ -317,3 +305,32 @@ func GetBlockController(blockId string) *BlockController {
defer globalLock.Unlock()
return blockControllerMap[blockId]
}
func ProcessStaticCommand(blockId string, cmdGen BlockCommand) {
switch cmd := cmdGen.(type) {
case *MessageCommand:
log.Printf("MESSAGE: %s | %q\n", blockId, cmd.Message)
case *SetViewCommand:
log.Printf("SETVIEW: %s | %q\n", blockId, cmd.View)
block := GetBlockData(blockId)
if block != nil {
block.WithLock(func() {
block.View = cmd.View
})
}
case *SetMetaCommand:
log.Printf("SETMETA: %s | %v\n", blockId, cmd.Meta)
block := GetBlockData(blockId)
if block != nil {
block.WithLock(func() {
for k, v := range cmd.Meta {
if v == nil {
delete(block.Meta, k)
continue
}
block.Meta[k] = v
}
})
}
}
}