lib/markdown_exec.rb in markdown_exec-2.1.0 vs lib/markdown_exec.rb in markdown_exec-2.2.0

- old
+ new

@@ -43,10 +43,12 @@ tap_config envvar: MarkdownExec::TAP_DEBUG $stderr.sync = true $stdout.sync = true +$pd = false unless defined?($pd) + ARGV_SEP = '--' # custom error: file specified is missing # class FileMissingError < StandardError; end @@ -84,24 +86,10 @@ # :reek:UtilityFunction def extract_named_captures_from_option(str, option) str.match(Regexp.new(option))&.named_captures&.sym_keys end -# :reek:UtilityFunction -def list_recent_output(saved_stdout_folder, saved_stdout_glob, - list_count) - SavedFilesMatcher.most_recent_list(saved_stdout_folder, - saved_stdout_glob, list_count) -end - -# :reek:UtilityFunction -def list_recent_scripts(saved_script_folder, saved_script_glob, - list_count) - SavedFilesMatcher.most_recent_list(saved_script_folder, - saved_script_glob, list_count) -end - # execute markdown documents # module MarkdownExec include Exceptions @@ -165,16 +153,18 @@ choices << { disabled: '', name: "in #{file_names[:section_title]}".send(@chrome_color) } choices += file_names[:data].map { |str| FileInMenu.for_menu(str) } # Conditionally add directory names if data is present unless directory_names[:data].count.zero? - choices << { disabled: '', name: "in #{directory_names[:section_title]}".send(@chrome_color) } + choices << { disabled: '', + name: "in #{directory_names[:section_title]}".send(@chrome_color) } choices += files_in_directories end # Adding found in block names - choices << { disabled: '', name: "in #{found_in_block_names[:section_title]}".send(@chrome_color) } + choices << { disabled: '', + name: "in #{found_in_block_names[:section_title]}".send(@chrome_color) } choices += vbn choices end @@ -184,20 +174,26 @@ def directory_names(search_options, highlight_value) matched_directories = find_directory_names { section_title: 'directory names', data: matched_directories, - formatted_text: [{ content: AnsiFormatter.new(search_options).format_and_highlight_array(matched_directories, highlight: [highlight_value]) }] + formatted_text: [{ content: AnsiFormatter.new(search_options).format_and_highlight_array( + matched_directories, highlight: [highlight_value] + ) }] } end - def found_in_block_names(search_options, highlight_value, formspec: '=%<index>4.d: %<line>s') + def found_in_block_names(search_options, highlight_value, + formspec: '=%<index>4.d: %<line>s') matched_contents = (find_file_contents do |line| - read_block_name(line, search_options[:fenced_start_and_end_regex], search_options[:block_name_match], search_options[:block_name_nick_match]) + read_block_name(line, search_options[:fenced_start_and_end_regex], + search_options[:block_name_match], search_options[:block_name_nick_match]) end).map.with_index do |(file, contents), index| # [file, contents.map { |detail| format(formspec, detail.index, detail.line) }, index] - [file, contents.map { |detail| format(formspec, { index: detail.index, line: detail.line }) }, index] + [file, contents.map do |detail| + format(formspec, { index: detail.index, line: detail.line }) + end, index] end { section_title: 'block names', data: matched_contents.map(&:first), formatted_text: matched_contents.map do |(file, details, index)| @@ -220,11 +216,12 @@ matched_files, highlight: [highlight_value] ).join("\n") }] } end - def read_block_name(line, fenced_start_and_end_regex, block_name_match, block_name_nick_match) + def read_block_name(line, fenced_start_and_end_regex, block_name_match, + block_name_nick_match) return unless line.match(fenced_start_and_end_regex) bm = extract_named_captures_from_option(line, block_name_match) return if bm.nil? @@ -239,11 +236,10 @@ end ## # # :reek:DuplicateMethodCall { allow_calls: ['block', 'item', 'lm', 'opts', 'option', '@options', 'required_blocks'] } - # rubocop:enable Layout/LineLength # :reek:MissingSafeMethod { exclude: [ read_configuration_file! ] } # :reek:TooManyInstanceVariables ### temp # :reek:TooManyMethods ### temp class MarkParse attr_reader :options, :prompt, :run_state @@ -265,10 +261,24 @@ "CachedNestedFileReader.#{name} -- #{$!}", opts ) end + # :reek:UtilityFunction + def list_recent_output(saved_stdout_folder, saved_stdout_glob, + list_count) + SavedFilesMatcher.most_recent_list(saved_stdout_folder, + saved_stdout_glob, list_count) + end + + # :reek:UtilityFunction + def list_recent_scripts(saved_script_folder, saved_script_glob, + list_count) + SavedFilesMatcher.most_recent_list(saved_script_folder, + saved_script_glob, list_count) + end + def warn_format(name, message, opts = {}) Exceptions.warn_format( "CachedNestedFileReader.#{name} -- #{message}", opts ) @@ -388,12 +398,10 @@ options[:saved_script_glob], options[:list_count] ) }, pwd: -> { @fout.fout File.expand_path('..', __dir__) }, run_last_script: -> { run_last_script }, - select_recent_output: -> { select_recent_output }, - select_recent_script: -> { select_recent_script }, tab_completions: -> { @fout.fout tab_completions }, menu_export: -> { @fout.fout menu_export } } return if execute_simple_commands(simple_commands) @@ -478,22 +486,27 @@ # files_in_directories = directory_names[:data].map do |dn| find_files('*', [dn], exclude_dirs: true) end.flatten(1).map { |str| FileInMenu.for_menu(str) } - return { exit: true } unless file_names[:data]&.count.positive? || files_in_directories&.count.positive? || found_in_block_names[:data]&.count.positive? + unless file_names[:data]&.count.positive? || files_in_directories&.count.positive? || found_in_block_names[:data]&.count.positive? + return { exit: true } + end vbn = found_in_block_names[:matched_contents].map do |matched_contents| filename, details, = matched_contents nexo = AnsiFormatter.new(@options).format_and_highlight_array( details, highlight: [value] ) - [FileInMenu.for_menu(filename)] + nexo.map { |str| { disabled: '', name: (' ' * 20) + str } } + [FileInMenu.for_menu(filename)] + nexo.map do |str| + { disabled: '', name: (' ' * 20) + str } + end end.flatten - choices = MenuBuilder.new.build_menu(file_names, directory_names, found_in_block_names, files_in_directories, vbn) + choices = MenuBuilder.new.build_menu(file_names, directory_names, found_in_block_names, + files_in_directories, vbn) @options[:filename] = FileInMenu.from_menu( select_document_if_multiple( choices, prompt: options[:prompt_select_md].to_s + ' ยค Age in months'.fg_rgbh_AF_AF_00 @@ -543,11 +556,12 @@ ->(value) { tap_config value: value } when 'exit' ->(_) { exit } when 'find', 'open' ->(value) { - exit if find_value(value, execute_chosen_found: procname == 'open').fetch(:exit, false) + exit if find_value(value, execute_chosen_found: procname == 'open').fetch(:exit, + false) } when 'help' ->(_) { @fout.fout menu_help exit @@ -569,13 +583,13 @@ when 'val_as_bool' ->(value) { value.instance_of?(::String) ? (value.chomp != '0') : value } when 'val_as_int' - ->(value) { value.to_i } + lambda(&:to_i) when 'val_as_str' - ->(value) { value.to_s } + lambda(&:to_s) when 'version' lambda { |_| @fout.fout MarkdownExec::VERSION exit } @@ -732,65 +746,32 @@ @options[:block_name] = mf[:block] @options[:filename] = mf[:file].gsub(@options[:saved_filename_pattern], @options[:saved_filename_replacement]) end - def select_document_if_multiple(files = list_markdown_files_in_path, prompt: options[:prompt_select_md].to_s) + def select_document_if_multiple(files = list_markdown_files_in_path, + prompt: options[:prompt_select_md].to_s) return files[0] if (count = files.count) == 1 return unless count >= 2 opts = options.dup - select_option_or_exit(HashDelegator.new(@options).string_send_color(prompt, :prompt_color_after_script_execution), - files, - opts.merge(per_page: opts[:select_page_height])) + select_option_or_exit( + HashDelegator.new(@options).string_send_color(prompt, + :prompt_color_after_script_execution), + files, + opts.merge(per_page: opts[:select_page_height]) + ) end # Presents a TTY prompt to select an option or exit, returns selected option or nil def select_option_or_exit(prompt_text, strings, opts = {}) - result = @options.select_option_with_metadata(prompt_text, strings, - opts) - ### 2024-04-20 what for? - # return unless result.fetch(:option, nil) - - result[:selected] + @options.select_option_with_metadata( + prompt_text, strings, opts + )&.fetch(:selected) end - def select_recent_output - filename = select_option_or_exit( - HashDelegator.new(@options).string_send_color(@options[:prompt_select_output].to_s, - :prompt_color_after_script_execution), - list_recent_output( - @options[:saved_stdout_folder], - @options[:saved_stdout_glob], - @options[:list_count] - ), - @options.merge({ per_page: @options[:select_page_height] }) - ) - return unless filename.present? - - `open #{filename} #{options[:output_viewer_options]}` - end - - def select_recent_script - filename = select_option_or_exit( - HashDelegator.new(@options).string_send_color(@options[:prompt_select_md].to_s, - :prompt_color_after_script_execution), - list_recent_scripts( - @options[:saved_script_folder], - @options[:saved_script_glob], - @options[:list_count] - ), - @options.merge({ per_page: @options[:select_page_height] }) - ) - return if filename.nil? - - saved_name_split(filename) - - @options.document_inpseq ### ({ save_executed_script: false }) - end - public def tab_completions(data = menu_for_optparse) data.map do |item| "--#{item[:long_name]}" if item[:long_name] @@ -825,6 +806,6 @@ assert_equal block1, block assert_equal MenuState::CONTINUE, state end end # module MarkdownExec -end # if +end # if