Skip to content

Commit

Permalink
Fix/improve pipeline stdout behavior
Browse files Browse the repository at this point in the history
Previously, you couldn't easily set the stdout behavior for a pipe.
Now that works for the various cases you would expect.
  • Loading branch information
mitchpaulus committed Dec 17, 2024
1 parent d99cc99 commit 80f8796
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
37 changes: 21 additions & 16 deletions mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,22 +900,27 @@ MainLoop:
return result
}

asList, ok := top.(*MShellList)
if ok {
if asList.StdoutBehavior == STDOUT_LINES {
newMShellList := &MShellList{Items: []MShellObject{}, StandardInputFile: "", StandardOutputFile: "", StdoutBehavior: STDOUT_NONE}
var scanner *bufio.Scanner
scanner = bufio.NewScanner(strings.NewReader(stdout))
for scanner.Scan() {
newMShellList.Items = append(newMShellList.Items, &MShellString{scanner.Text()})
}
stack.Push(newMShellList)
} else if asList.StdoutBehavior == STDOUT_STRIPPED {
stripped := strings.TrimSpace(stdout)
stack.Push(&MShellString{stripped})
} else if asList.StdoutBehavior == STDOUT_COMPLETE {
stack.Push(&MShellString{stdout})
var stdoutBehavior StdoutBehavior
switch top.(type) {
case *MShellList:
stdoutBehavior = top.(*MShellList).StdoutBehavior
case *MShellPipe:
stdoutBehavior = top.(*MShellPipe).StdoutBehavior
}

if stdoutBehavior == STDOUT_LINES {
newMShellList := &MShellList{Items: []MShellObject{}, StandardInputFile: "", StandardOutputFile: "", StdoutBehavior: STDOUT_NONE}
var scanner *bufio.Scanner
scanner = bufio.NewScanner(strings.NewReader(stdout))
for scanner.Scan() {
newMShellList.Items = append(newMShellList.Items, &MShellString{scanner.Text()})
}
stack.Push(newMShellList)
} else if stdoutBehavior == STDOUT_STRIPPED {
stripped := strings.TrimSpace(stdout)
stack.Push(&MShellString{stripped})
} else if stdoutBehavior == STDOUT_COMPLETE {
stack.Push(&MShellString{stdout})
}

// Push the exit code onto the stack if a question was used to execute
Expand Down Expand Up @@ -1477,7 +1482,7 @@ MainLoop:
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot pipe a %s.\n", t.Line, t.Column, obj1.TypeName()))
}

stack.Push(&MShellPipe{*list, STDOUT_NONE})
stack.Push(&MShellPipe{*list, list.StdoutBehavior})
} else if t.Type == READ {
var reader io.Reader
// Check if what we are reading from is seekable. If so, we can do a buffered read and reset the position.
Expand Down
17 changes: 17 additions & 0 deletions tests/pipeline.msh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@
[sed "s/Earth/World/"]
[grep World]
] |;

# FILE:pipeline.out
[
[printf "hello\\nEarth!\\n"]
[sed "s/Earth/World/"] "pipeline.out" >
] | ;

[
[printf "hello\\nEarth!\\n"]
[sed "s/Earth/World/"]
] | os ; wl

# Swapping order of | and os shouldn't matter
[
[printf "hello\\nEarth!\\n"]
[sed "s/Earth/World/"]
] os | ; wl
4 changes: 4 additions & 0 deletions tests/pipeline.msh.stdout
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
World!
hello
World!
hello
World!
2 changes: 2 additions & 0 deletions tests/pipeline.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello
World!
2 changes: 2 additions & 0 deletions tests/pipeline.out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello
World!

0 comments on commit 80f8796

Please sign in to comment.