From ed44179991bc02196132e111e5ad25ccd6a91e6c Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Wed, 20 Mar 2024 05:11:02 +0000 Subject: [PATCH] add `Renderable#render_options` for `argument/case.gtx` --- lib/bashly/concerns/renderable.rb | 5 +++- lib/bashly/views/argument/case.gtx | 6 +++++ lib/bashly/views/argument/case_repeatable.gtx | 25 +++++++++++++++++++ .../parse_requirements_case_catch_all.gtx | 9 ++----- .../parse_requirements_case_repeatable.gtx | 23 +---------------- .../parse_requirements_case_simple.gtx | 9 ++----- 6 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 lib/bashly/views/argument/case.gtx create mode 100644 lib/bashly/views/argument/case_repeatable.gtx diff --git a/lib/bashly/concerns/renderable.rb b/lib/bashly/concerns/renderable.rb index 7558ad14..2a971ad3 100644 --- a/lib/bashly/concerns/renderable.rb +++ b/lib/bashly/concerns/renderable.rb @@ -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 diff --git a/lib/bashly/views/argument/case.gtx b/lib/bashly/views/argument/case.gtx new file mode 100644 index 00000000..9602213d --- /dev/null +++ b/lib/bashly/views/argument/case.gtx @@ -0,0 +1,6 @@ += view_marker + +condition = render_options[:index].zero? ? 'if' : 'elif' +> {{ condition }} [[ -z ${args['{{ name }}']+x} ]]; then +> args['{{ name }}']=$1 +> shift diff --git a/lib/bashly/views/argument/case_repeatable.gtx b/lib/bashly/views/argument/case_repeatable.gtx new file mode 100644 index 00000000..f6e1bfab --- /dev/null +++ b/lib/bashly/views/argument/case_repeatable.gtx @@ -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 diff --git a/lib/bashly/views/command/parse_requirements_case_catch_all.gtx b/lib/bashly/views/command/parse_requirements_case_catch_all.gtx index 288c5d4e..2ee6a6da 100644 --- a/lib/bashly/views/command/parse_requirements_case_catch_all.gtx +++ b/lib/bashly/views/command/parse_requirements_case_catch_all.gtx @@ -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 diff --git a/lib/bashly/views/command/parse_requirements_case_repeatable.gtx b/lib/bashly/views/command/parse_requirements_case_repeatable.gtx index 9a3ab550..5173d92a 100644 --- a/lib/bashly/views/command/parse_requirements_case_repeatable.gtx +++ b/lib/bashly/views/command/parse_requirements_case_repeatable.gtx @@ -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 diff --git a/lib/bashly/views/command/parse_requirements_case_simple.gtx b/lib/bashly/views/command/parse_requirements_case_simple.gtx index 3779e6eb..03f7b464 100644 --- a/lib/bashly/views/command/parse_requirements_case_simple.gtx +++ b/lib/bashly/views/command/parse_requirements_case_simple.gtx @@ -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