class RBT::Cookbooks::Highest

Constants

DISPLAY_N_PROGRAMS_BY_DEFAULT
#

DISPLAY_N_PROGRAMS_BY_DEFAULT

How many programs to display by default.

#
EXIT_ON_MISSING_ENTRY
#

EXIT_ON_MISSING_ENTRY

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

new( display_n = DISPLAY_N_PROGRAMS_BY_DEFAULT, run_already = true ) click to toggle source
#

initialize

To use more than the defaults, you can do this:

Cookbooks::Highest.new(150)
#
# File lib/rbt/utility_scripts/highest.rb, line 50
def initialize(
    display_n   = DISPLAY_N_PROGRAMS_BY_DEFAULT,
    run_already = true
  )
  reset
  startup
  set_display_top_n(display_n) # First set how many programs we will display.
  run if run_already
end

Public Instance Methods

beautify_file_size(i) click to toggle source
#

beautify_file_size

This will attempt to beautify the given input.

We require 3 trailing positions after the , position.

#
# File lib/rbt/utility_scripts/highest.rb, line 163
def beautify_file_size(i)
  i  = i.to_s.strip
  x  = i.to_f
  kb = 1024
  mb = (1024 ** 2)
  gb = (1024 ** 3)
  # tb = (1024 ** 4)
  if x < 1024
    return "#{round(x.to_f)} bytes"
  elsif x > 1024 && x < mb
    return "#{round(x.to_f/kb)} kb"
  elsif x > mb && x < gb
    result = round(x.to_f/mb)
    result = '%.3f' % result
    return "#{result} MB"
  elsif x > gb
    return "#{round(x.to_f/gb)} GB"
  end
end
display_n?() click to toggle source
#

display_n?

#
# File lib/rbt/utility_scripts/highest.rb, line 193
def display_n?
  @display_top_n
end
inform_the_user_what_we_will_do() click to toggle source
#

inform_the_user_what_we_will_do

#
# File lib/rbt/utility_scripts/highest.rb, line 79
def inform_the_user_what_we_will_do
  # Get the local URL.
  opnn; e 'Now fetching the top `'+salmon(@display_top_n.to_s)+
          '` largest local programs from the directory'
  opnn; e '  `'+sdir(source_directory?)+'`.'
  opnn; e 'This may take a while. Please be patient.'
end
notify_the_user_that_we_have_finished_fetching_the_local_urls() click to toggle source
#

notify_the_user_that_we_have_finished_fetching_the_local_urls

#
# File lib/rbt/utility_scripts/highest.rb, line 90
def notify_the_user_that_we_have_finished_fetching_the_local_urls
  opnn; e # Add a newline.
  opnn; e "Finished fetching the local URLs. We will display them now.#{N}"
  opnn; e
end
report_results() click to toggle source
#

report_results

#
# File lib/rbt/utility_scripts/highest.rb, line 234
def report_results
  sort_results
  n_rjust = display_n?.to_s.size
  @results.each_with_index { |entry, index|
    index    += 1
    index     = index.to_s.rjust(n_rjust)
    # ===================================================================== #
    # For now, we shorten the url.
    # ===================================================================== #
    local_url = remove_file_suffix(
      File.basename(entry.first).strip
    ).strip
    local_url = '%-36s' % local_url # Then we pad it.
    _ = '%.3f' % entry[1].to_s
    filesize  = '%12s' % beautify_file_size(_)
    opnn; e '  ('+sfancy(index)+') '+local_url+' '+swarn(filesize)
  }
end
reset() click to toggle source
#

reset

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/highest.rb, line 63
def reset
  super()
  @dataset = nil
  @namespace = NAMESPACE
end
round(number, n_positions = 3) click to toggle source
#

round

#
# File lib/rbt/utility_scripts/highest.rb, line 186
def round(number, n_positions = 3)
  ( number * 10 ** n_positions ).floor.to_f / (10 ** n_positions)
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/utility_scripts/highest.rb, line 256
def run
  inform_the_user_what_we_will_do
  sanitize_available_programs
  notify_the_user_that_we_have_finished_fetching_the_local_urls
  report_results # report results
end
sanitize_available_programs() click to toggle source
#

sanitize_available_programs

#
# File lib/rbt/utility_scripts/highest.rb, line 200
def sanitize_available_programs
  array = [] # Dump the information into this array here.
  _ = @available_programs
  _.each {|i|
    # ===================================================================== #
    # Obtain the dataset for this program next.
    # ===================================================================== #
    @dataset = RBT::Cookbooks::Cookbook.new(i) { :be_quiet_and_bypass_menu }
    url  = @dataset.local_url? # Keep the local URL here.
    if url.nil?
      e '`'+simp(i.to_s)+'` is nil, probably because it does not '\
        'exist locally.'
      e 'Please consider fixing this problem.'
      if EXIT_ON_MISSING_ENTRY
        e 'We will exit now as the constant EXIT_ON_MISSING_ENTRY '\
          'was set to true.'
        exit
      end
    end 
    size = @dataset.size?    # Keep the filesize here.
    if @debug
      opnn; e crimson('DEBUG:')+' Filesize of '+slateblue(i.to_s)+
              ' is '+orange(size)+'.'
    end
    if url
      array << [ url, size ] # Then append into the main Array containing this information.
    end
  }
  @available_programs = array # Store it here finally.
end
set_display_top_n( i = DISPLAY_N_PROGRAMS_BY_DEFAULT ) click to toggle source
#

set_display_top_n

#
# File lib/rbt/utility_scripts/highest.rb, line 108
def set_display_top_n(
    i = DISPLAY_N_PROGRAMS_BY_DEFAULT
  )
  if i.is_a? Array # Handle arrays first.
    if i.empty?
      i = DISPLAY_N_PROGRAMS_BY_DEFAULT # Use default.
    else
      i = i.first
    end
  elsif i.is_a? Symbol
    case i # case tag
    when :display_ten_programs
      i = 10
    when :display_twenty_programs
      i = 20
    end
  end
  case i # case tag
  # ======================================================================= #
  # === highest --help
  # ======================================================================= #
  when /-?-?help/,'HELP'
    show_help; exit
  end
  i = i.to_i unless i.is_a? Integer
  # ======================================================================= #
  # Ensure that we will not grab too many entries.
  # ======================================================================= #
  i = available_programs?.size.to_i if i > available_programs?.size.to_i
  @display_top_n = i
end
show_help() click to toggle source
#

show_help (help tag)

#
# File lib/rbt/utility_scripts/highest.rb, line 143
def show_help
  if Object.const_defined? :ClassDocuShower
    ClassDocuShower[__FILE__]
  end
  e 'This class does not have many special commands.'
  e
  e 'You can pass in a number, which simply means how many'
  e 'programs will be displayed, e. g. as in:'
  e
  e '  highest 250'
  e
end
sort_results() click to toggle source
#

sort_results

#
# File lib/rbt/utility_scripts/highest.rb, line 99
def sort_results
  @results = @available_programs.sort_by {|name, size|
    size.to_i # We sort by size.
  }.reverse[0...@display_top_n]
end
startup() click to toggle source
#

startup

#
# File lib/rbt/utility_scripts/highest.rb, line 72
def startup
  @available_programs = available_programs?
end