class RBT::Cookbooks::SearchForTags
Constants
- DEFAULT_SEARCH_TAG
#¶ ↑
DEFAULT_SEARCH_TAG
¶ ↑#¶ ↑
- EXIT_ON_EMPTY_DIRECTORIES
#¶ ↑
EXIT_ON_EMPTY_DIRECTORIES
¶ ↑Whether to exit on empty directories or whether not.
#¶ ↑
- USE_THIS_DELAY
#¶ ↑
USE_THIS_DELAY
¶ ↑If we open in the editor, we will use this delay.
#¶ ↑
- USE_XORG_BUFFER
#¶ ↑
USE_XORG_BUFFER
¶ ↑This constant can be set to automatically set the xorg buffer.
This may be useful sometimes - but it can also be annoying, which is why it is a constant that can be toggled by the user.
#¶ ↑
Public Class Methods
new( search_for = DEFAULT_SEARCH_TAG, run_already = true ) { || ... }
click to toggle source
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 87 def initialize( search_for = DEFAULT_SEARCH_TAG, # This variable is usually an Array. run_already = true ) reset if search_for.is_a? Array if search_for.any? {|entry| entry.start_with? '--' } # =================================================================== # # Ok, this will be treated as a commandline-instruction then. # =================================================================== # _ = search_for.select {|inner_entry| inner_entry.start_with? '--' } menu(_) search_for.reject! {|inner_entry| inner_entry.start_with? '--' } end end set_search_for(search_for) # ======================================================================= # # === Handle blocks next # ======================================================================= # if block_given? yielded = yield case yielded # ===================================================================== # # === :be_quiet # ===================================================================== # when :be_quiet set_be_quiet end end run if run_already end
Public Instance Methods
also_show_url?()
click to toggle source
append_to_buffer(i)
click to toggle source
array_of_programs?()
click to toggle source
#¶ ↑
array_of_programs
?¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 329 def array_of_programs? @array_of_programs end
Also aliased as: array_of_programs
calculate_and_display_the_filesize_of(this_file)
click to toggle source
#¶ ↑
calculate_and_display_the_filesize_of
¶ ↑
We will calculate the filesize of the given program at hand.
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 252 def calculate_and_display_the_filesize_of(this_file) this_file = this_file.dup.map {|entry| source_directory?+ File.basename(entry.split(':').first).sub(/\.yml$/,'')+ '/' } total_size = 0 this_file.each {|entry| entries = Dir[entry+'*'] if entries.size == 0 # =================================================================== # # Report empty directories, but only if the entry "do_not_download" # was not set. # =================================================================== # program = File.basename(entry).downcase _ = action(:SanitizeCookbook, program) { :fast } unless _.do_not_download? if File.directory? entry # Only report it if it exists. opne 'We found an empty directory here, at `'+sdir(entry)+'`.' end end exit if EXIT_ON_EMPTY_DIRECTORIES end if entries.first total_size += File.size(entries.first) if File.exist? entries.first end } total_filesize = ( total_size.to_f/1024/1024 ).round(2).to_s if be_verbose? result = "#{rev}#{N}The total filesize is #{sfancy(total_size.to_s)} "\ "#{rev}bytes. (This is #{simp(total_filesize)}#{rev} MB)" e result end end
display_every_tag( also_show_the_specific_tags = true )
click to toggle source
#¶ ↑
display_every_tag
¶ ↑
This method will show every available tag.
If the argument also_show_the_specific_tags is set to true then we will also show the specific tag.
To invoke this, do:
tags ALL
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 412 def display_every_tag( also_show_the_specific_tags = true ) array = [] # This array will keep our results. all_programs = available_programs? all_programs.each {|program| opne "#{rev}Checking on the program `#{sfancy(program)}#{rev}` next:" begin _ = action(:SanitizeCookbook, program) { :fast } rescue Exception => error opnn; pp error opne program end if _ begin if _.has_tags? e 'Yes, it has tags.' array << [ program, _.return_tags ] if also_show_the_specific_tags else # Else, it has no tags. # e 'No, it has no tags.' array << [ program, ''] end rescue Exception; end else owarn "We did not find anything about `#{simp(program)}#{swarn('`.')}" owarn 'This is considered a fatal error for now, thus exiting.' exit end } # ======================================================================= # # Now report that array: # ======================================================================= # opne "#{rev}Next reporting these results:" array.sort_by {|key, value| value }.each {|key,value| value = skyblue(value) if Object.const_defined? :Colours e ("#{key}: ").ljust(left_padding?)+value } end
do_not_show_the_index()
click to toggle source
left_padding?()
click to toggle source
perform_the_search()
click to toggle source
#¶ ↑
perform_the_search
¶ ↑
This method will perform the actual search.
We will search through our programs via grep.
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 550 def perform_the_search array_with_the_results = [] # We store the results here. # ======================================================================= # # The advantage of the yaml database is that it is faster. # # The disadvantage is that the yaml database must be manually updated. # # You can do so via file registered_tags.rb or as alias: # # regtag # # ======================================================================= # if use_yaml_database_for_tags? if be_verbose? opne "The #{slateblue('yaml-database')} #{rev}at" e e " #{sfile(RBT.file_registered_tags?)}" e opne 'will be queried for the registered tags.' end _ = search_term? _ = _.first if _.is_a? Array if RBT.all_tags?.include? _ array_with_the_results << RBT.all_tags?[_] end # ======================================================================= # # === Use grep # ======================================================================= # else # else we use grep. if be_verbose? opne 'The command grep will be used to perform this search.' end # ===================================================================== # # Next, we have an array stored in @search_for_these_tags since # April 2014. We will search for "tags: SEARCH_TERM_HERE". # ===================================================================== # search_for?.each {|this_tag| # =================================================================== # # Why do we use grep here and thus depend on it? Simply. Grep is # super-fast. # =================================================================== # _ = 'grep --ignore-case -r "^ tags: '+this_tag+'" '+individual_cookbooks_directory?+'*' e _ if be_verbose? _ = `#{_}`.split(N) _ = _.map {|entry| splitted = entry.split(': tags:') File.basename(splitted.first).gsub(/\.yml/,'') } # =================================================================== # # Now that we sanitized that temp dataset, we can add it to # our array. # =================================================================== # array_with_the_results << _ } end # ======================================================================= # # Next, as there may be multiple search tags, we may have to display # only the conjoint tags. We will do so next, by using the & # intersection operator of ruby. # ======================================================================= # if array_with_the_results.size > 1 array_with_the_results = array_with_the_results.first & array_with_the_results[1] end array_with_the_results.flatten! @array_of_programs = array_with_the_results unless array_with_the_results.empty? if be_verbose? e; opne rev+'The results for programs with the tag(s): `'+ sfancy(search_for?.join(' '))+rev+'` are:'+ N+N end # ===================================================================== # # Show either with or without index. # ===================================================================== # array_with_the_results.each_with_index {|the_name, index| index = (index+1).to_s index = ('%4s' % index)+') ' the_name = ('%-25s' % the_name.strip) if also_show_url? if use_xorg_buffer? append_to_buffer(index) if @show_index append_to_buffer(the_name) end _ = ''.dup _ << sfancy(index) if @show_index _ << "#{rev}#{the_name}" # Composite index and program-name. if also_show_url? the_url = url?(the_name) the_url = ' '+('%58s' % the_url) the_url = the_url.strip if use_xorg_buffer? append_to_buffer(the_url) end _ << simp(the_url) end if use_xorg_buffer? append_to_buffer(N) # Prepare the xorg-buffer string. end @result = _ e _ if be_verbose? if @also_open_every_entry_in_the_editor this_file = return_location_to_this_programs_yaml_file(the_name) if File.exist? this_file if be_verbose? e 'Next opening '+sfile(this_file)+' after a '+ "delay of #{simp(USE_THIS_DELAY.to_s)} seconds." end open_in_editor(this_file) sleep USE_THIS_DELAY end end } if array_with_the_results.size > 1 calculate_and_display_the_filesize_of(array_with_the_results) end store_results_into_file # Store these results. else # Else we did not find any results. if be_verbose? opne "#{rev}Not found any tag-result for search entry `"\ "#{sfancy(search_for?.first)}#{rev}`." end end set_the_xorg_buffer end
reassign_this_input_to_that_registered_entry(original_input, i)
click to toggle source
#¶ ↑
reassign_this_input_to_that_registered_entry
¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 677 def reassign_this_input_to_that_registered_entry(original_input, i) opne "#{rev}Reassigning input `#{simp(original_input)} #{rev}to "\ "the registered entry `#{simp(i)}#{rev}`." end
reset()
click to toggle source
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
Calls superclass method
RBT::LeanPrototype#reset
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 122 def reset super() infer_the_namespace try_to_require_the_xorg_buffer # ======================================================================= # # === @use_xorg_buffer # ======================================================================= # @use_xorg_buffer = USE_XORG_BUFFER # ======================================================================= # # === @xorg_buffer_string # ======================================================================= # @xorg_buffer_string = ''.dup # ======================================================================= # # === @show_index # ======================================================================= # @show_index = true # ======================================================================= # # === :be_verbose # ======================================================================= # set_be_verbose # Just to make sure that we will be verbose here. # ======================================================================= # # === @also_show_url # # If true then we will also show the URL. # ======================================================================= # @also_show_url = false # ======================================================================= # # === @also_open_every_entry_in_the_editor # ======================================================================= # @also_open_every_entry_in_the_editor = false # ======================================================================= # # === @array_of_programs # ======================================================================= # @array_of_programs = [] end
run()
click to toggle source
search_for?()
click to toggle source
#¶ ↑
search_for?¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 394 def search_for? @search_for end
Also aliased as: commandline?, search_term?
set_search_for(i = nil)
click to toggle source
#¶ ↑
set_search_for
¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 454 def set_search_for(i = nil) i = DEFAULT_SEARCH_TAG if i.nil? if i.is_a? Array and i.empty? i = [DEFAULT_SEARCH_TAG] # Fill up empty arrays in this case. end if i.is_a? Symbol i = i.to_s end if i.is_a? String i = i.strip # Get rid of whitespace. # ===================================================================== # # Next, sanitize some common misspellings. # ===================================================================== # if i.include? ' ' # If it includes internal ' '. i = i.split(' ') # Then we turn it into an Array. end end if i.is_a? Array # Next handle Arrays here. i.compact! # Remove nil entries as these can arise when we passed input through menu(). i.map! {|entry| if entry.include? '-' # '-' characters are useless - they are not part of a tag. entry = entry.dup if entry.frozen? entry.delete!('-') end entry } original_input = i.first # ===================================================================== # # === Sanitize operations # # Perform some sanitize-operations next, out of user-convenience. # # Input such as 'kde_apps' will become substituted towards the # longer variant called 'kde_application'. # ===================================================================== # case i.first # case tag # ===================================================================== # # === kde_application # ===================================================================== # when 'kdeapp', 'kdeapplicates', 'kdeapplications', 'kde_applications', 'kde_apps', /kdeapps/i i = 'kde_application' reassign_this_input_to_that_registered_entry(original_input, i) # ===================================================================== # # === xorg # ===================================================================== # when 'x11' i = 'xorg' opne 'Reassigning input `'+simp(original_input)+rev+ " to `#{simp(i)} as we should have more" opne 'tags registered that way.' # ===================================================================== # # === game # ===================================================================== # when 'games' opne 'Need to correct `'+simp(original_input)+'` to `'+ simp('game')+'` tag.' i = 'game' # ===================================================================== # # === windowmanager # ===================================================================== # when 'WM', 'wm', 'windowsmanager' i = 'windowmanager' reassign_this_input_to_that_registered_entry(original_input, i) # ===================================================================== # # === programming_language # ===================================================================== # when 'programming_languages' i = 'programming_language' reassign_this_input_to_that_registered_entry(original_input, i) # ===================================================================== # # === scanning # ===================================================================== # when 'scanning' i = 'scanner' end end i.flatten! if i.is_a? Array i = [i] unless i.is_a? Array i.compact! @search_for = i # Must be an Array. end
set_the_xorg_buffer( i = @xorg_buffer_string )
click to toggle source
show_help()
click to toggle source
#¶ ↑
show_help
¶ ↑
Invoke this method like so:
tags --help
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 166 def show_help # help tag e 'The following options are available for class '+ steelblue('RBT::Cookbooks::SearchForTags')+':' e eparse ' OPEN_ALL # open all entries in the editor' eparse ' WITH_URL # also show the URL' eparse ' NO_INDEX # do not show the leading numbers '\ 'when listing the tags' eparse ' ALL # display every tag' eparse ' --available # show which tags are available' eparse ' --open-in-editor # batch-open all files with that '\ 'tag in the main editor' e end
store_results_into_file()
click to toggle source
#¶ ↑
store_results_into_file
¶ ↑
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 373 def store_results_into_file this_file = store_into_this_directory?+'these_tags' if be_verbose? e # A spacer here is in order. opne "Now storing these results into the file `#{sfile(this_file)}`." end save_file(@xorg_buffer_string, this_file) end
string?()
click to toggle source
url?(i)
click to toggle source
#¶ ↑
url?¶ ↑
Return the URL of a program. The method perform_the_search
() will display it.
#¶ ↑
# File lib/rbt/cookbooks/search_for_tags/search_for_tags.rb, line 293 def url?(i) i = i.to_s.dup _ = "#{cookbooks_directory?}"\ "#{File.basename(i).split(' ').first.delete(':')}".dup _ << '.yml' unless _.end_with? '.yml' if File.exist? _ # We directly load this file. i = YAML.load_file(_) i = i[i.keys.first]['url1'].to_s else # File does not exist here. Should not happen. end return i end