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

Commit

Permalink
New lines in result supported
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Nov 29, 2020
1 parent f96eeac commit cc52576
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/client/named_pipe_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCommandLoop(t *testing.T) {
cmd := pc.nextCommand()
t.Log(cmd)

pc.writeResult(fmt.Sprintf("%s - OK", strings.TrimSpace(strings.Split(cmd, "\n")[0])))
pc.writeResult(fmt.Sprintf("%s - OK", strings.TrimSpace(cmd)))
}
}()

Expand Down
11 changes: 6 additions & 5 deletions internal/client/pipe_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"runtime"
"strings"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -66,7 +67,7 @@ func (pc *PipeClient) commandLoop() {
cmdText := pc.pipe.nextCommand()

// parse command
cmd, err := command.Parse(cmdText, true)
cmd, err := command.Parse(strings.Trim(cmdText, "\n"), true)
if err != nil {
log.Errorln(err)
pc.pipe.writeResult(fmt.Sprintf("error %s", err))
Expand Down Expand Up @@ -97,13 +98,13 @@ func (pc *PipeClient) commandLoop() {
}

// save command results
result := fmt.Sprintf("ok %s", payload.Result)
if payload.Error != "" {
result = fmt.Sprintf("error %s", payload.Error)
pc.pipe.writeResult(fmt.Sprintf("error %s", payload.Error))
} else {
linesCount := utils.CountRune(payload.Result, '\n')
pc.pipe.writeResult(fmt.Sprintf("%d %s", linesCount, payload.Result))
}

pc.pipe.writeResult(result)

} else {
// fire and forget
pc.hostClient.CallAndForget(page.PageCommandFromHostAction, &page.PageCommandRequestPayload{
Expand Down
5 changes: 5 additions & 0 deletions internal/page/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
log "github.com/sirupsen/logrus"

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

const (
Expand Down Expand Up @@ -113,6 +114,10 @@ func add(session *Session, cmd command.Command) (result string, err error) {

// sub-commands
for _, line := range cmd.Lines {
if utils.WhiteSpaceOnly(line) {
continue
}

childCmd, err := command.Parse(line, false)
if err != nil {
return "", err
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func CountIndent(s string) int {
return len(r)
}

func CountRune(s string, r rune) int {
count := 0
for _, c := range s {
if c == r {
count++
}
}
return count
}

func ToJSON(v interface{}) string {
json, _ := json.MarshalIndent(v, "", " ")
return string(json)
Expand Down
58 changes: 35 additions & 23 deletions tests/test-add-command.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
echo "Pipe name: $1"
page_pipe=$1
res=`pglet page page2`

function pglet() {
# send command
echo "$1" > "$page_pipe"
IFS=' ' read -r page_pipe page_url <<< "$res"

# read result
IFS=' ' read result_status result_value < "$page_pipe"
echo $result_value
echo "Pipe name: $page_pipe"
#page_pipe="$1"

function pglet_send() {
# send command
echo "$1" > "$page_pipe"

# read result
local firstLine="true"
local result_value=""
IFS=''
while read -r line; do
if [[ $firstLine == "true" ]]; then
IFS=' ' read -r result_status result_value <<< "$line"
firstLine="false"
else
result_value="$line"
fi
echo "$result_value"
done <"$page_pipe"
}

function pglet_event() {
Expand All @@ -29,31 +43,29 @@ function pglet_event() {
done
}

full_name=`curl $REPLIT_DB_URL/full_name`

function main() {
pglet "clean page"
rowID=`pglet "add row"`
colID=`pglet "add col to=$rowID"`
pglet "add text to=$colID value='Enter your name:'"
pglet "add textbox to=$colID id=fullName value='$full_name'"
pglet "add button to=$colID id=submit text=Submit"
pglet_send "clean page"
rowID=`pglet_send "add row"`
colID=`pglet_send "add col to=$rowID"`
pglet_send "add text to=$colID value='Enter your name:'"
pglet_send "add textbox to=$colID id=fullName value='$full_name' multiline=true"
pglet_send "add button to=$colID id=submit text=Submit"

events=("submit click welcome")
pglet_event "${events[@]}"
}

function welcome() {
# get fullName value
full_name=`pglet "get fullName value"`

# save to Repl database
curl $REPLIT_DB_URL -d "full_name=$full_name"
echo "before get name"
full_name=`pglet_send "get fullName value"`
full_name="${full_name//$'\n'/\\n}"
echo "full name: $full_name"

# output welcome message
pglet "clean page"
pglet "add text value='Hello, $full_name'"
pglet "add button id=again text=Again"
pglet_send "clean page"
pglet_send "add text value='Hello, $full_name'"
pglet_send "add button id=again text=Again"

events=("again click main")
pglet_event "${events[@]}"
Expand Down

0 comments on commit cc52576

Please sign in to comment.