class Cookbooks::SearchForTags
Constants
- DEFAULT_SEARCH_TAG
#¶ ↑
DEFAULT_SEARCH_TAG¶ ↑
#¶ ↑
- EXIT_ON_EMPTY_DIRECTORIES
#¶ ↑
EXIT_ON_EMPTY_DIRECTORIES¶ ↑
#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE¶ ↑
#¶ ↑
- USE_THIS_DELAY
#¶ ↑
USE_THIS_DELAY¶ ↑
#¶ ↑
Public Class Methods
new( search_for = DEFAULT_SEARCH_TAG, run_already = true ) { || ... }
click to toggle source
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/search_for_tags/initialize.rb, line 8 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) if block_given? yielded = yield case yielded when :be_quiet @be_verbose = false 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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 79 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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 172 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 _ = Cookbooks::Cookbook.new(program) unless _.get_value_of 'do_not_download' if File.directory? entry # Only report it if it exists. opnn; e '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 = N+'The total filesize is '+sfancy(total_size.to_s)+ ' bytes. (This is '+simp(total_filesize)+' 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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 250 def display_every_tag( also_show_the_specific_tags = true ) array = [] # This array will keep our results. all_programs = Cookbooks.available_programs all_programs.each {|program| opnn; e 'Checking on the program `'+sfancy(program)+'` next:' begin _ = Cookbooks::Cookbook.new(program) { :bypass_menu_check } rescue Exception => error opnn; pp error opnn; e 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 opnn; e swarn('We did not find anything about `')+simp(program)+swarn('`.') opnn; e swarn('This is considered a fatal error for now, thus exiting.') exit end } # ======================================================================= # # Now report that array: # ======================================================================= # opnn; e 'Next reporting these results:' array.sort_by {|key, value| value }.each {|key,value| value = Konsole.skyblue(value) if Object.const_defined? :Colours e (key+': ').ljust(left_padding?)+value } end
do_not_show_the_index()
click to toggle source
eparse(i)
click to toggle source
left_padding?()
click to toggle source
opnn()
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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 361 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? opnn; e 'The yaml-database will be queried for the tags.' end _ = search_term? _ = _.first if _.is_a? Array if Cookbooks.all_tags?.include? _ array_with_the_results << Cookbooks.all_tags?[_] end # ======================================================================= # # === Use grep # ======================================================================= # else # else we use grep. if be_verbose? opnn; e '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; opnn; e 'The results for programs with the tag(s): `'+ sfancy(search_for?.join(' '))+'` 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? append_to_buffer(index) if @show_index append_to_buffer(the_name) _ = ''.dup _ << sfancy(index) if @show_index _ << 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 append_to_buffer(the_url) _ << simp(the_url) end append_to_buffer(N) # Prepare the xorg-buffer string. @result = _ e _ if be_verbose? if @also_open_every_entry_in_the_editor this_file = COOKBOOK_DIRECTORY+the_name+'.yml' 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 system('bluefish '+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? opnn; e 'Not found any tag-result for search entry `'+ sfancy(search_for?.first)+'`.' 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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 478 def reassign_this_input_to_that_registered_entry(original_input, i) opnn; e "Reassigning input `#{simp(original_input)} to the "\ "registered entry `#{simp(i)}`." end
reset()
click to toggle source
#¶ ↑
reset¶ ↑
#¶ ↑
Calls superclass method
Cookbooks::Base#reset
# File lib/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 66 def reset super() @xorg_buffer_string = ''.dup @show_index = true @be_verbose = true # Just to make sure that we will be verbose here. @also_show_url = false # if true then we will also show the URL. @also_open_every_entry_in_the_editor = false @array_of_programs = [] end
run()
click to toggle source
search_for?()
click to toggle source
#¶ ↑
search_for?¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 210 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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 299 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.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. # ===================================================================== # case i.first # case tag when 'games' opnn; e 'Need to correct `'+simp(original_input)+'` to `'+ simp('game')+'` tag.' i = 'game' when 'x11' i = 'xorg' opnn; e 'Reassigning input `'+simp(original_input)+' to `'+simp(i)+ ' as we should have more' opnn; e 'tags registered that way.' when 'WM','windowsmanager' i = 'windowmanager' reassign_this_input_to_that_registered_entry(original_input, i) when 'programming_languages' i = 'programming_language' reassign_this_input_to_that_registered_entry(original_input, i) 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()
click to toggle source
show_help()
click to toggle source
#¶ ↑
#show_help¶ ↑
Invoke this method like so:
tags --help
#¶ ↑
# File lib/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 221 def show_help # help tag e 'These options are available so far for class 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' e end
store_results_into_file()
click to toggle source
#¶ ↑
#store_results_into_file¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 142 def store_results_into_file this_file = store_into_this_directory?+'these_tags' if be_verbose? e # A spacer here is in order. opnn; e '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/cookbooks/utility_scripts/search_for_tags/search_for_tags.rb, line 112 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