Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Command batches support
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Nov 27, 2020
1 parent bbe332b commit c36acc4
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 84 deletions.
38 changes: 18 additions & 20 deletions client/src/controls/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@ import { Stack } from 'office-ui-fabric-react/lib/Stack';

const MyStack = React.memo(({ control }) => {

console.log(`render stack: ${control.i}`);
console.log(`render stack: ${control.i}`);

// stack props
const stackProps = {
horizontal: true,
gap: control.gap ? control.gap : 10
// verticalFill: true,
// horizontalAlign: control.horizontalalign ? control.horizontalalign : "start",
// verticalAlign: control.verticalalign ? control.verticalalign : "start",
// gap: control.gap ? control.gap : 10,
// styles: {
// root: {
// width: control.width ? control.width : "100%",
// padding: control.padding ? control.padding : "10px"
// }
// },
};
// stack props
const stackProps = {
horizontal: control.horizontal ? control.horizontal : false,
verticalFill: true,
horizontalAlign: control.horizontalalign ? control.horizontalalign : "start",
verticalAlign: control.verticalalign ? control.verticalalign : "start",
gap: control.gap ? control.gap : 10,
styles: {
root: {
width: control.width ? control.width : "100%"
}
},
};

const childControls = useSelector(state => control.c.map(childId => state.page.controls[childId]), shallowEqual);
const childControls = useSelector(state => control.c.map(childId => state.page.controls[childId]), shallowEqual);

return <Stack {...stackProps}>
<ControlsList controls={childControls} />
</Stack>
return <Stack {...stackProps}>
<ControlsList controls={childControls} />
</Stack>
})

export default MyStack
2 changes: 1 addition & 1 deletion internal/client/pipe_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (pc *PipeClient) commandLoop() {
cmdText := pc.pipe.nextCommand()

// parse command
cmd, err := command.Parse(cmdText)
cmd, err := command.Parse(cmdText, true)
if err != nil {
log.Errorln(err)
pc.pipe.writeResult(fmt.Sprintf("error %s", err))
Expand Down
28 changes: 23 additions & 5 deletions internal/page/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type CommandMetadata struct {
ShouldReturn bool
}

func Parse(cmdText string) (*Command, error) {
func Parse(cmdText string, parseName bool) (*Command, error) {

var command *Command = nil
var err error
Expand All @@ -62,7 +62,7 @@ func Parse(cmdText string) (*Command, error) {
if command == nil {
if !utils.WhiteSpaceOnly(line) {
// parse command
command, err = parseLine(line)
command, err = parseCommandLine(line, parseName)
if err != nil {
return nil, err
}
Expand All @@ -75,13 +75,15 @@ func Parse(cmdText string) (*Command, error) {
return command, nil
}

func parseLine(line string) (*Command, error) {
func parseCommandLine(line string, parseName bool) (*Command, error) {
command := &Command{
Attrs: make(map[string]string),
Values: make([]string, 0),
Lines: make([]string, 0),
}

command.Indent = utils.CountIndent(line)

var err error
var s scanner.Scanner
s.Init(strings.NewReader(line))
Expand Down Expand Up @@ -119,7 +121,7 @@ func parseLine(line string) (*Command, error) {
prevLit = ""
} else if tok != "=" && prevToken != "=" && prevLit != "" {
v := utils.TrimQuotes(prevLit)
if command.Name == "" {
if command.Name == "" && parseName {
command.Name = utils.ReplaceEscapeSymbols(v)
} else {
command.Values = append(command.Values, utils.ReplaceEscapeSymbols(v))
Expand All @@ -133,7 +135,15 @@ func parseLine(line string) (*Command, error) {

// consume last token collected
if prevLit != "" {
command.Values = append(command.Values, prevLit)
if command.Name == "" && parseName {
command.Name = utils.ReplaceEscapeSymbols(prevLit)
} else {
command.Values = append(command.Values, utils.ReplaceEscapeSymbols(prevLit))
}
}

if parseName && !command.IsSupported() {
return nil, fmt.Errorf("unknown command: %s", command.Name)
}

return command, nil
Expand All @@ -152,3 +162,11 @@ func (cmd *Command) ShouldReturn() bool {
cmdMeta, _ := supportedCommands[strings.ToLower(cmd.Name)]
return cmdMeta.ShouldReturn
}

func (cmd *Command) String() string {
attrs := make([]string, 0)
for k, v := range cmd.Attrs {
attrs = append(attrs, fmt.Sprintf("%s=\"%s\"", k, v))
}
return fmt.Sprintf("%s %s %s\n%s", cmd.Name, strings.Join(cmd.Values, " "), strings.Join(attrs, " "), strings.Join(cmd.Lines, "\n"))
}
68 changes: 51 additions & 17 deletions internal/page/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@ package command
import (
"log"
"testing"

"github.com/pglet/pglet/internal/utils"
)

func TestParse1(t *testing.T) {
cmd, err := Parse(`Add value:1 c=3.1 TextField Text=aaa value="Hello,\n 'wor\"ld!" aaa='bbb' cmd2=1`)
cmd, err := Parse(` Add value:1 c=3.1 TextField Text=aaa value="Hello,\n 'wor\"ld!" aaa='bbb' cmd2=1`, true)

if err != nil {
t.Fatal("Error parsing command", err)
}

// visualize command
log.Printf("%s", utils.ToJSON(cmd))
log.Printf("%s", cmd)

if cmd.Name != "Add" {
t.Errorf("command name is %s, want %s", cmd.Name, "Add")
}

if cmd.Indent != 2 {
t.Errorf("command indent is %d, want %d", cmd.Indent, 2)
}
}

func TestParse2(t *testing.T) {
cmd, err := Parse(`set body:form:fullName value='John Smith' another_prop=value`)
cmd, err := Parse(`set body:form:fullName value='John Smith' another_prop=value`, true)

if err != nil {
t.Fatal(err)
}

// visualize command
log.Printf("%s", utils.ToJSON(cmd))
log.Printf("%s", cmd)

if len(cmd.Values) != 1 {
t.Errorf("the number of values is %d, want %d", len(cmd.Values), 1)
Expand All @@ -40,17 +42,40 @@ func TestParse2(t *testing.T) {
if cmd.Values[0] != expValue {
t.Errorf("command values[0] is %s, want %s", cmd.Values[0], expValue)
}

if cmd.Indent != 0 {
t.Errorf("command indent is %d, want %d", cmd.Indent, 0)
}
}

func TestParseSingleCommand(t *testing.T) {
cmd, err := Parse(`set`, true)

if err != nil {
t.Fatal(err)
}

// visualize command
log.Printf("%s", cmd)

if cmd.Name != "set" {
t.Errorf("command name is %s, want %s", cmd.Name, "set")
}

if len(cmd.Values) != 0 {
t.Errorf("the number of values is %d, want %d", len(cmd.Values), 0)
}
}

func TestParseClean(t *testing.T) {
cmd, err := Parse(`clean page`)
cmd, err := Parse(`clean page`, true)

if err != nil {
t.Fatal(err)
}

// visualize command
log.Printf("%s", utils.ToJSON(cmd))
log.Printf("%s", cmd)

if len(cmd.Values) != 1 {
t.Errorf("the number of values is %d, want %d", len(cmd.Values), 1)
Expand All @@ -64,26 +89,35 @@ func TestParseClean(t *testing.T) {

func TestParseMultilineCommand(t *testing.T) {
cmd, err := Parse(`
add to=footer
stack
text value="Hello, world!"
stack
textbox id=txt1
button id=ok`)
button id=ok`, true)

if err != nil {
t.Fatal(err)
}

// visualize command
log.Printf("%s", utils.ToJSON(cmd))
log.Printf("%s", cmd)

// if len(cmd.Values) != 1 {
// t.Errorf("the number of values is %d, want %d", len(cmd.Values), 1)
// }
expName := "add"
if cmd.Name != expName {
t.Errorf("command name is %s, want %s", cmd.Name, expName)
}

if len(cmd.Values) != 0 {
t.Errorf("the number of values is %d, want %d", len(cmd.Values), 0)
}

// expValue := "page"
// if cmd.Values[0] != expValue {
// t.Errorf("command values[0] is %s, want %s", cmd.Values[0], expValue)
// }
if len(cmd.Lines) != 5 {
t.Errorf("the number of lines is %d, want %d", len(cmd.Lines), 5)
}

if cmd.Indent != 6 {
t.Errorf("command indent is %d, want %d", cmd.Indent, 6)
}
}

0 comments on commit c36acc4

Please sign in to comment.