Going from a REPL to a TUI

This commit is contained in:
2026-01-05 00:23:55 +00:00
parent 785db2a59b
commit 0162c14c1d
9 changed files with 460 additions and 151 deletions
+29 -4
View File
@@ -11,6 +11,7 @@ import (
const (
newWindowCMDID types.Command = 3
destroyWindowCMDID types.Command = 4
moveWindowCMDID types.Command = 5
)
var (
@@ -68,6 +69,13 @@ var CDashDisplay = Device{
ArgCheck: destroyWindowArgCheck,
Fn: destroyWindow,
},
"move-window": {
Identifier: moveWindowCMDID,
Name: "move-window",
Desc: "Moves a selected window - pass the window ID",
ArgCheck: moveWindowArgCheck,
Fn: moveWindow,
},
},
}
@@ -83,8 +91,6 @@ func createWindowArgCheck(args []string) error {
func createWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) {
// Parse the command
// x0, y0, width, height, title # add other decorations later on
// fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
x0, err := strconv.ParseInt(args[0], 10, 0)
if err != nil {
return 0, []byte{}, err
@@ -132,8 +138,6 @@ func destroyWindowArgCheck(args []string) error {
func destroyWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) {
// Parse the command
fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
type UIWindowDestructPacket struct {
WinID int16
}
@@ -154,3 +158,24 @@ func destroyWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error
return dCMD.GetIdentifier(), bytes, nil
}
func moveWindowArgCheck(args []string) error {
if len(args) != 1 {
return fmt.Errorf("wrong parameters, got %d, want %d. "+
"Command asks for: winID", len(args), 1)
}
return nil
}
func moveWindow(dCMD *DeviceCMD, args []string) (types.Command, []byte, error) {
// We can edit this struct to be a generic 'window edit' struct. Either
// that or implement a way of passing arbitrary fields to update
type UIWindowMovePacket struct {
WinID int16
Direction uint8
Quantity uint8
}
return dCMD.GetIdentifier(), []byte{}, nil
}
+5 -1
View File
@@ -1,7 +1,10 @@
// Package devices will define the available devices we can communicate with
package devices
import "esdi/peripheral/types"
import (
"esdi/peripheral/types"
"fmt"
)
// IDs for our devices. They need to be correctly mapped on the devices
// themselves so we can discover them
@@ -66,6 +69,7 @@ func (dCMD *DeviceCMD) Run(args []string) (types.Command, []byte, error) {
}
// Run the function
fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
return dCMD.Fn(dCMD, args)
}
+22 -30
View File
@@ -5,6 +5,7 @@ import (
"esdi/peripheral/devices"
"fmt"
"path/filepath"
"strings"
)
type PeripheralType string
@@ -47,12 +48,13 @@ func (clerk *PeripheralDeviceClerk) FindDeviceAPI(ID uint8) *devices.Device {
return nil
}
func (clerk *PeripheralDeviceClerk) FindDevices() error {
func (clerk *PeripheralDeviceClerk) FindDevices() (string, error) {
ports, err := clerk.listPorts()
if err != nil {
return err
return "", err
}
var s strings.Builder
for _, p := range ports {
newDevice := NewPeripheralDevice(p)
@@ -73,17 +75,17 @@ func (clerk *PeripheralDeviceClerk) FindDevices() error {
fmt.Println(p, string(newDevice.Name[:]))
}
// go clerk.clerkBackgroundJob()
return nil
return s.String(), nil
}
func (clerk *PeripheralDeviceClerk) ListDevices() error {
func (clerk *PeripheralDeviceClerk) ListDevices() (string, error) {
var s strings.Builder
for _, d := range clerk.Devices {
fmt.Printf("[%2d] %s\n", d.ID, d.Name)
s.WriteString(fmt.Sprintf("[%2d] %s\n", d.ID, d.Name))
}
return nil
return s.String(), nil
}
func (clerk *PeripheralDeviceClerk) getDevice(ID uint8) *PeripheralDevice {
@@ -94,53 +96,43 @@ func (clerk *PeripheralDeviceClerk) getDevice(ID uint8) *PeripheralDevice {
return clerk.Devices[ID]
}
func (clerk *PeripheralDeviceClerk) ListDeviceAPI(ID uint8) error {
func (clerk *PeripheralDeviceClerk) ListDeviceAPI(ID uint8) (string, error) {
dev := clerk.getDevice(ID)
if dev == nil {
return fmt.Errorf("device with ID %d not found", ID)
return "", fmt.Errorf("device with ID %d not found", ID)
}
var s strings.Builder
for _, f := range dev.DeviceAPI.API {
fmt.Printf("%s\t%s\n", f.Name, f.Desc)
s.WriteString(fmt.Sprintf("%s\t%s\n", f.Name, f.Desc))
}
return nil
return s.String(), nil
}
func (clerk *PeripheralDeviceClerk) RunDeviceFunction(ID uint8, f string, args []string) error {
func (clerk *PeripheralDeviceClerk) RunDeviceFunction(ID uint8, f string,
args []string) (string, error) {
dev := clerk.getDevice(ID)
if dev == nil {
return fmt.Errorf("device with ID %d not found", ID)
return "", fmt.Errorf("device with ID %d not found", ID)
}
devAPI := dev.DeviceAPI
cmd := devAPI.HasFunction(f)
if cmd == nil {
return fmt.Errorf("requested function %s doesnt exist", f)
return "", fmt.Errorf("requested function %s doesnt exist", f)
}
// Execute the function
command, payload, err := cmd.Run(args)
if err != nil {
return err
return "", err
}
err = dev.SendCommand(command, payload)
if err != nil {
return err
return "", err
}
return nil
return "", nil
}
// func (c *PeripheralDeviceClerk) clerkBackgroundJob() {
// for {
// c.mu.RLock()
// for _, d := range c.Devices {
// // Check the state of the device
// if d.CommState == CommIdle {
// }
// }
// c.mu.Unlock()
// }
// }