Skip to content

Commit

Permalink
Merge pull request #506 from DannyBen/add/render-options
Browse files Browse the repository at this point in the history
Add `Renderable#render_options` for `argument/case.gtx`
  • Loading branch information
DannyBen authored Mar 20, 2024
2 parents 35b9eb3 + ed44179 commit dab78e5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
5 changes: 4 additions & 1 deletion lib/bashly/concerns/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

module Bashly
module Renderable
def render(view)
attr_reader :render_options

def render(view, render_options = {})
@render_options = render_options
GTX.render_file view_path(view), context: binding, filename: "#{views_subfolder}.#{view}"
end

Expand Down
6 changes: 6 additions & 0 deletions lib/bashly/views/argument/case.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= view_marker

condition = render_options[:index].zero? ? 'if' : 'elif'
> {{ condition }} [[ -z ${args['{{ name }}']+x} ]]; then
> args['{{ name }}']=$1
> shift
25 changes: 25 additions & 0 deletions lib/bashly/views/argument/case_repeatable.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= view_marker

condition = render_options[:index].zero? ? 'if' : 'elif'

if render_options[:index] == 0
> escaped="$(printf '%q' "$1")"
end

> {{ condition }} [[ -z ${args['{{ name }}']+x} ]]; then
if repeatable
> args['{{ name }}']="$escaped"
if unique
> unique_lookup["{{ name }}:$escaped"]=1
> elif [[ -z "${unique_lookup["{{ name }}:$escaped"]:-}" ]]; then
> args['{{ name }}']="${args['{{ name }}']} $escaped"
> unique_lookup["{{ name }}:$escaped"]=1
else
> else
> args['{{ name }}']="${args['{{ name }}']} $escaped"
end

else
> args['{{ name }}']="$1"

end
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
= view_marker

if args.any?
condition = "if"
args.each do |arg|
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
> args['{{ arg.name }}']=$1
> shift

condition = "elif"
args.each_with_index do |arg, index|
= arg.render :case, index: index
end

> else
Expand Down
23 changes: 1 addition & 22 deletions lib/bashly/views/command/parse_requirements_case_repeatable.gtx
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
= view_marker

condition = "if"
args.each_with_index do |arg, index|
if index == 0
> escaped="$(printf '%q' "$1")"
end
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
if arg.repeatable
> args['{{ arg.name }}']="$escaped"
if arg.unique
> unique_lookup["{{ arg.name }}:$escaped"]=1
> elif [[ -z "${unique_lookup["{{ arg.name }}:$escaped"]:-}" ]]; then
> args['{{ arg.name }}']="${args['{{ arg.name }}']} $escaped"
> unique_lookup["{{ arg.name }}:$escaped"]=1
else
> else
> args['{{ arg.name }}']="${args['{{ arg.name }}']} $escaped"
end

else
> args['{{ arg.name }}']="$1"

end
condition = "elif"
= arg.render :case_repeatable, index: index
end

> fi
Expand Down
9 changes: 2 additions & 7 deletions lib/bashly/views/command/parse_requirements_case_simple.gtx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
= view_marker

if args.any?
condition = "if"
args.each do |arg|
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
> args['{{ arg.name }}']=$1
> shift

condition = "elif"
args.each_with_index do |arg, index|
= arg.render :case, index: index
end

> else
Expand Down

0 comments on commit dab78e5

Please sign in to comment.