class Cani::Config

Constants

COMP_DIR
DEFAULTS
DIRECTORY
FILE
FISH_COMP_DIR
FISH_DIR
NO_MODIFY_FILE

Attributes

modify_shell_configs[R]
settings[R]

Public Class Methods

new(**opts) click to toggle source
# File lib/cani/config.rb, line 23
def initialize(**opts)
  no_modify_exists      = File.exist? NO_MODIFY_FILE
  @settings             = DEFAULTS.merge opts
  @modify_shell_configs =
    if allow_shellrc_modification?
      File.delete NO_MODIFY_FILE if no_modify_exists
      true
    elsif disallow_shellrc_modification?
      FileUtils.touch NO_MODIFY_FILE unless no_modify_exists
      false
    end

  if File.exist? file
    if (yml = YAML.load_file(file))
      @settings.merge! yml
    end
  else
    install!
  end
end

Public Instance Methods

allow_shellrc_modification?() click to toggle source
# File lib/cani/config.rb, line 48
def allow_shellrc_modification?
  ARGV.include?('--modify') || !disallow_shellrc_modification?
end
comp_dir() click to toggle source
# File lib/cani/config.rb, line 60
def comp_dir
  COMP_DIR
end
directory() click to toggle source
# File lib/cani/config.rb, line 56
def directory
  DIRECTORY
end
disallow_shellrc_modification?() click to toggle source
# File lib/cani/config.rb, line 44
def disallow_shellrc_modification?
  ARGV.include?('--no-modify') || File.exist?(NO_MODIFY_FILE)
end
file() click to toggle source
# File lib/cani/config.rb, line 52
def file
  FILE
end
fish_comp_dir() click to toggle source
# File lib/cani/config.rb, line 68
def fish_comp_dir
  FISH_COMP_DIR
end
fish_dir() click to toggle source
# File lib/cani/config.rb, line 64
def fish_dir
  FISH_DIR
end
install!() click to toggle source
# File lib/cani/config.rb, line 81
def install!
  hrs  = (DEFAULTS['expire'] / 3600.to_f).round 2
  days = (hrs / 24.to_f).round 2
  wk   = (days / 7.to_f).round 2
  mo   = (days / 30.to_f).round 2
  tstr = if mo >= 1
           "#{mo == mo.to_i ? mo.to_i : mo} month#{mo != 1 ? 's' : ''}"
         elsif wk >= 1
           "#{wk == wk.to_i ? wk.to_i : wk} week#{wk != 1 ? 's' : ''}"
         elsif days >= 1
           "#{days == days.to_i ? days.to_i : days} day#{days != 1 ? 's' : ''}"
         else
           "#{hrs == hrs.to_i ? hrs.to_i : hrs} hour#{hrs != 1 ? 's' : ''}"
         end

  FileUtils.mkdir_p directory
  File.open file, 'w' do |f|
    f << "---\n"
    f << "# this is the default configuration file for the \"Cani\" RubyGem.\n"
    f << "# it contains some options to control what is shown, when new data\n"
    f << "# is fetched, where it should be fetched from.\n"
    f << "# documentation: https://github.com/sidofc/cani\n"
    f << "# rubygems: https://rubygems.org/gems/cani\n\n"
    f << "# the \"expire\" key defines the interval at which new data is\n"
    f << "# fetched from \"source\". It's value is passed in as seconds\n"
    f << "# default value: #{DEFAULTS['expire']} # => #{tstr}\n"
    f << "expire: #{expire}\n\n"
    f << "# the \"source\" key is used to fetch the data required for\n"
    f << "# this command to work.\n"
    f << "source: #{source}\n\n"
    f << "# navigating means reopening the previously open window when going back by pressing <escape>\n"
    f << "# or opening the next menu by selecting an entry in fzf with <enter>\n"
    f << "# there are two different navigation modes:\n"
    f << "#   * 'always'  - always navigate back to the previous menu, exit only at root menu with <escape>\n"
    f << "#   * 'forward' - only allow navigating forward and backwards upto the menu that cani was initially open\n"
    f << "navigate: #{navigate}\n\n"
    f << "# the notes property defines how notes should be displayed\n"
    f << "# there are two different modes:\n"
    f << "#   * 'all'      - show all notes, regardless of relevance\n"
    f << "#   * 'relevant' - show only relevant (visible in an era) notes\n"
    f << "notes: #{notes}\n\n"
    f << "# the \"versions\" key defines how many versions of support\n"
    f << "# will be shown in the \"use\" command\n"
    f << "# e.g. `-ie +edge` becomes `--ie ++edge` when this is set to 2, etc...\n"
    f << "versions: #{versions}\n\n"
    f << "# the \"browsers\" key defines which browsers are shown\n"
    f << "# in the \"use\" command\n"
    f << "browsers:\n"
    f << browsers.map { |bn| "  - #{bn}" }.join("\n") + "\n"
    f << (Cani.api.browsers.map(&:name) - browsers).map { |bn| "  # - #{bn}" }.join("\n")
  end

  Completions.install!
end
method_missing(mtd, *args, &block) click to toggle source
Calls superclass method
# File lib/cani/config.rb, line 136
def method_missing(mtd, *args, &block)
  settings.key?(mtd.to_s) ? settings[mtd.to_s] : super
end
nav_type?(type) click to toggle source
remove!() click to toggle source
# File lib/cani/config.rb, line 72
def remove!
  File.unlink file if File.exist? file
  FileUtils.rm_rf directory if Dir.exist? directory
end
respond_to_missing?(mtd, include_private = false) click to toggle source
# File lib/cani/config.rb, line 140
def respond_to_missing?(mtd, include_private = false)
  settings.key? mtd.to_s
end