Method: Tkri::App#fetch_ri

Defined in:
lib/tkri.rb

#fetch_ri(topic) ⇒ Object

Executes the ‘ri’ command and returns its output.



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/tkri.rb', line 927

def fetch_ri topic
  if !@ri_cache
    @ri_cache = {}
    @cached_topics = []
  end

  return @ri_cache[topic] if @ri_cache[topic]

  command = Settings::COMMAND.select { |k,v| RUBY_PLATFORM.index(k) }.first.to_a[1] || Settings::COMMAND['__default__']
  text = Kernel.`(command % topic)  # `
  if $? != 0
    text += "\n" + "ERROR: Failed to run the command '%s' (exit code: %d). Please make sure you have this command in your PATH.\n\nYou may wish to modify this program's source (%s) to update the command to something that works on your system." % [command % topic, $?, $0]
  else
    if text == "nil\n"
      text = 'Topic "%s" not found.' % topic
    end
    text = _post_process_text(text)
    @ri_cache[topic] = text
    @cached_topics << topic
  end

  # Remove the oldest topic from the cache
  if @cached_topics.length > 20
    @ri_cache.delete @cached_topics.shift
  end

  return text
end