Skip to content

Commit

Permalink
add strip, rstrip, and lstrip commands; add sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmariecom committed Sep 10, 2024
1 parent f0556a2 commit 1cafe0b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions a
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
2
3
8 changes: 8 additions & 0 deletions a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env -S sh -c '[ "$0".bin -nt "$0" ] || tail -n+2 "$0" | cc -x c - -o "$0".bin || exit 1; exec "$0".bin "$@"'
#include <stdio.h>

int main(void)
{
printf("Hello, world!\n");
return 0;
}
46 changes: 44 additions & 2 deletions lib/dorian/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ def run
arguments.delete("ls")
@command = :ls
command_ls
when :strip
arguments.delete("strip")
@command = :strip
command_strip
when :rstrip
arguments.delete("rstrip")
@command = :rstrip
command_rstrip
when :lstrip
arguments.delete("lstrip")
@command = :lstrip
command_lstrip
else
arguments.delete("read")
@command = :read
Expand Down Expand Up @@ -266,6 +278,36 @@ def command_read
each(stdin_arguments + arguments) { |input| outputs(reads(input)) }
end

def command_strip
each(stdin_files + files) do |input|
outputs(lines(reads(File.read(input)), strip: :strip), file: input)
end

each(stdin_arguments + arguments) do |input|
outputs(lines(reads(input), strip: :strip))
end
end

def command_rstrip
each(stdin_files + files) do |input|
outputs(lines(reads(File.read(input)), strip: :rstrip), file: input)
end

each(stdin_arguments + arguments) do |input|
outputs(lines(reads(input), strip: :rstrip))
end
end

def command_lstrip
each(stdin_files + files) do |input|
outputs(lines(reads(File.read(input)), strip: :lstrip), file: input)
end

each(stdin_arguments + arguments) do |input|
outputs(lines(reads(input), strip: :lstrip))
end
end

def everything
read_stdin_files + stdin_arguments + read_files + arguments
end
Expand Down Expand Up @@ -724,9 +766,9 @@ def reject(collection, progress: false, &)
end
end

def lines(input)
def lines(input, strip: :rstrip)
if input.is_a?(String)
input.lines.map(&:rstrip)
input.lines.map(&strip)
elsif deep?
deep_lines(input)
else
Expand Down

0 comments on commit 1cafe0b

Please sign in to comment.