module Lingo::Ctl

Constants

ALIASES
OPTIONS
OPTWIDTH
USAGE

Public Instance Methods

ctl() click to toggle source
# File lib/lingo/ctl.rb, line 89
def ctl
  parse_options
  send("do_#{ALIASES[ARGV.shift]}")
end

Private Instance Methods

copy(what) click to toggle source
# File lib/lingo/ctl.rb, line 112
def copy(what)
  do_usage('Source and target are the same.') if OPTIONS[:scope] == :local

  source = find(what, false)
  target = File.join(path_for_scope(:local), Lingo.basepath(what, source))

  do_usage('Source and target are the same.') if source == target

  FileUtils.mkdir_p(File.dirname(target))
  FileUtils.cp(source, target, verbose: true)
end
copy_list(what) click to toggle source
# File lib/lingo/ctl.rb, line 222
def copy_list(what)
  files = list(what, false)
  files.select!(&Proc.new) if block_given?
  files.each { |file| ARGV.replace([file]); copy(what) }
end
do_clearstore() click to toggle source
# File lib/lingo/ctl.rb, line 124
def do_clearstore
  store = Dir["#{find(:store, false)}.*"]
  FileUtils.rm(store, verbose: true) unless store.empty?
end
do_demo() click to toggle source
# File lib/lingo/ctl.rb, line 129
def do_demo
  OPTIONS.update(path: ARGV.shift, scope: :system)
  no_args

  copy_list(:config) { |i| !File.basename(i).start_with?('test') }
  copy_list(:lang)
  copy_list(:dict)   { |i|  File.basename(i).start_with?('user') }
  copy_list(:sample)
end
do_help(opts = nil) click to toggle source
# File lib/lingo/ctl.rb, line 144
def do_help(opts = nil)
  no_args

  msg = opts ? [opts, 'Commands:'] : []

  aliases = Hash.new { |h, k| h[k] = [] }
  ALIASES.each { |k, v| aliases[v] << k }

  COMMANDS.each { |c, (d, *e)|
    a = aliases[c]
    c = "#{c} (#{a.join(', ')})" unless a.empty?

    if opts
      msg << "    %-#{OPTWIDTH}s %s" % [c, d]
    else
      msg << "#{c}" << "  - #{d}"
      e.each { |i| msg <<  "  + #{i}" }
    end
  }

  abort msg.join("\n")
end
do_path() click to toggle source
# File lib/lingo/ctl.rb, line 139
def do_path
  no_args
  puts path_for_scope || PATH
end
do_usage(msg = nil) click to toggle source
# File lib/lingo/ctl.rb, line 174
def do_usage(msg = nil)
  abort "#{"#{PROGNAME}: #{msg}\n\n" if msg}#{USAGE}"
end
do_version(doit = true) click to toggle source
# File lib/lingo/ctl.rb, line 167
def do_version(doit = true)
  no_args

  msg = "Lingo v#{Lingo::VERSION}"
  doit ? puts(msg) : msg
end
find(what, doit = true) click to toggle source
# File lib/lingo/ctl.rb, line 104
def find(what, doit = true)
  name = ARGV.shift or do_usage('Required argument `name\ missing.')
  no_args

  file = Lingo.find(what, name, path: path_for_scope, &method(:do_usage))
  doit ? puts(file) : file
end
list(what, doit = true) click to toggle source
# File lib/lingo/ctl.rb, line 96
def list(what, doit = true)
  names = Regexp.union(*ARGV.empty? ? '' : ARGV)

  Lingo.list(what, path: path_for_scope).select { |file|
    File.basename(file) =~ names ? doit ? puts(file) : true : false
  }
end
no_args() click to toggle source
# File lib/lingo/ctl.rb, line 218
def no_args
  do_usage('Too many arguments.') unless ARGV.empty?
end
parse_options() click to toggle source
# File lib/lingo/ctl.rb, line 178
def parse_options
  OptionParser.new(USAGE, OPTWIDTH) { |opts|
    opts.separator ''
    opts.separator 'Scope options:'

    opts.on('--system', 'Restrict command to the system-wide Lingo directory') {
      OPTIONS[:scope] = :system
    }

    opts.on('--global', 'Restrict command to the user\s personal Lingo directory') {
      OPTIONS[:scope] = :global
    }

    opts.on('--local', 'Restrict command to the local Lingo directory') {
      OPTIONS[:scope] = :local
    }

    opts.separator ''
    opts.separator 'Generic options:'

    opts.on('-h', '--help', 'Print this help message and exit') {
      do_help(opts)
    }

    opts.on('--version', 'Print program version and exit') {
      abort "#{PROGNAME} v#{VERSION} (#{do_version(false)})"
    }
  }.parse!
end
path_for_scope(scope = OPTIONS[:scope]) click to toggle source
# File lib/lingo/ctl.rb, line 208
def path_for_scope(scope = OPTIONS[:scope])
  case scope
    when :system then [BASE]
    when :global then [HOME]
    when :local  then [OPTIONS[:path] || CURR]
    when nil
    else do_usage("Invalid scope `#{scope.inspect}'.")
  end
end