lib/markdown_exec.rb in markdown_exec-2.0.3.2 vs lib/markdown_exec.rb in markdown_exec-2.0.4

- old
+ new

@@ -21,10 +21,11 @@ require_relative 'directory_searcher' require_relative 'env' require_relative 'exceptions' require_relative 'fcb' require_relative 'filter' +require_relative 'find_files' require_relative 'fout' require_relative 'hash_delegator' require_relative 'input_sequencer' require_relative 'markdown_exec/version' require_relative 'mdoc' @@ -100,10 +101,49 @@ # execute markdown documents # module MarkdownExec include Exceptions + class SearchResultsReport < DirectorySearcher + 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]) }] + } + end + + def file_contents(search_options, highlight_value) + matched_contents = find_file_contents.map.with_index do |(file, contents), index| + [file, contents.map { |detail| format('=%4.d: %s', detail.index, detail.line) }, index] + end + { + section_title: 'file contents', + data: matched_contents.map(&:first), + formatted_text: matched_contents.map do |(file, details, index)| + { header: format('- %3.d: %s', index + 1, file), + content: AnsiFormatter.new(search_options).format_and_highlight_array( + details, + highlight: [highlight_value] + ) } + end + } + end + + def file_names(search_options, highlight_value) + matched_files = find_file_names + { + section_title: 'file names', + data: matched_files, + formatted_text: [{ content: AnsiFormatter.new(search_options).format_and_highlight_array( + matched_files, highlight: [highlight_value] + ).join("\n") }] + } + end + end + ## # # :reek:DuplicateMethodCall { allow_calls: ['block', 'item', 'lm', 'opts', 'option', '@options', 'required_blocks'] } # rubocop:enable Layout/LineLength # :reek:MissingSafeMethod { exclude: [ read_configuration_file! ] } @@ -284,10 +324,12 @@ if (pos = rest.shift)&.present? if Dir.exist?(pos) @options[:path] = pos elsif File.exist?(pos) @options[:filename] = pos + elsif @options[:default_find_select_open] + find_value(pos, execute_chosen_found: true) else raise FileMissingError, pos, caller end end @@ -301,10 +343,51 @@ exit 1 rescue StandardError error_handler('finalize_cli_argument_processing') end + # return { exit: true } to cause app to exit + def find_value(value, execute_chosen_found: false) + find_path = @options[:find_path].present? ? @options[:find_path] : @options[:path] + @fout.fout 'Searching in: ' \ + "#{HashDelegator.new(@options).string_send_color(find_path, + :menu_chrome_color)}" + searcher = SearchResultsReport.new(value, [find_path]) + file_names = searcher.file_names(options, value) + file_contents = searcher.file_contents(options, value) + directory_names = searcher.directory_names(options, value) + + ### search in file contents (block names, chrome, or text) + [file_contents, + directory_names, + file_names].each do |data| + @fout.fout "In #{data[:section_title]}" if data[:section_title] + next unless data[:formatted_text] + + data[:formatted_text].each do |fi| + @fout.fout fi[:header] if fi[:header] + @fout.fout fi[:content] if fi[:content] + end + end + return { exit: true } unless execute_chosen_found + + ## pick a document to open + # + files = directory_names[:data].map do |dn| + find_files('*', [dn], exclude_dirs: true) + end.flatten(1) + choices = \ + [{ disabled: '', name: "in #{file_names[:section_title]}".cyan }] \ + + file_names[:data] \ + + [{ disabled: '', name: "in #{directory_names[:section_title]}".cyan }] \ + + files \ + + [{ disabled: '', name: "in #{file_contents[:section_title]}".cyan }] \ + + file_contents[:data] + @options[:filename] = select_document_if_multiple(choices) + { exit: false } + end + ## Sets up the options and returns the parsed arguments # def initialize_and_parse_cli_options # @options = base_options @options = HashDelegator.new(base_options) @@ -342,39 +425,13 @@ case procname when 'debug' ->(value) { tap_config value: value } when 'exit' ->(_) { exit } - when 'find' + when 'find', 'open' ->(value) { - find_path = @options[:find_path].present? ? @options[:find_path] : @options[:path] - @fout.fout 'Searching in: ' \ - "#{HashDelegator.new(@options).string_send_color(find_path, - :menu_chrome_color)}" - searcher = DirectorySearcher.new(value, [find_path]) - - @fout.fout 'In file contents' - hash = searcher.search_in_file_contents - hash.each.with_index do |(key, v2), i1| - @fout.fout format('- %3.d: %s', i1 + 1, key) - @fout.fout AnsiFormatter.new(options).format_and_highlight_array( - v2.map { |nl| format('=%4.d: %s', nl.index, nl.line) }, - highlight: [value] - ) - end - - @fout.fout 'In directory names' - @fout.fout AnsiFormatter.new(options).format_and_highlight_array( - searcher.search_in_directory_names, highlight: [value] - ) - - @fout.fout 'In file names' - @fout.fout AnsiFormatter.new(options).format_and_highlight_array( - searcher.search_in_file_names, highlight: [value] - ).join("\n") - - exit + exit if find_value(value, execute_chosen_found: procname == 'open').fetch(:exit, false) } when 'help' ->(_) { @fout.fout menu_help exit @@ -551,13 +608,11 @@ rescue StandardError error_handler('run_last_script') end def saved_name_split(name) - # rubocop:disable Layout/LineLength mf = /#{@options[:saved_script_filename_prefix]}_(?<time>[0-9\-]+)_(?<file>.+)_,_(?<block>.+)\.sh/.match(name) - # rubocop:enable Layout/LineLength return unless mf @options[:block_name] = mf[:block] @options[:filename] = mf[:file].gsub(@options[:saved_filename_pattern], @options[:saved_filename_replacement]) @@ -576,10 +631,11 @@ # 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) - return unless result.fetch(:option, nil) + ### 2024-04-20 what for? + # return unless result.fetch(:option, nil) result[:selected] end def select_recent_output