Skip to content

Commit

Permalink
perf: use the IO each_line in the earlier stage of the process
Browse files Browse the repository at this point in the history
  • Loading branch information
initdc committed Jul 3, 2024
1 parent 90d4d26 commit 5b591ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions spec/exec_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe Exec do
Exec.each_line("ls spec").should eq ["exec_spec.cr\n", "spec_helper.cr\n"]
end

it "ensure $? was set" do
Exec.each_line("unamea")
$?.exit_code.should eq 127
end

it "list file with option" do
Exec.each_line("ls spec", chomp: true).should eq ["exec_spec.cr", "spec_helper.cr"]
end
Expand Down
8 changes: 4 additions & 4 deletions src/exec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class Exec < Process

def self.each_line(command, chomp = false, &block : String ->) : Nil
process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit)
output = process.output.gets_to_end
status = process.wait
$? = status
output.each_line(chomp) do |line|
process.output.each_line(chomp) do |line|
yield line
end
ensure
status = process.wait
$? = status
end

def self.each_line(command, chomp = false)
Expand Down

0 comments on commit 5b591ba

Please sign in to comment.