Module: Dopairb::CLI

Defined in:
lib/dopairb/cli.rb

Overview

dopairb [dopairb options] [--] [irb options]

Class Method Summary collapse

Class Method Details

.ours?(arg) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/dopairb/cli.rb', line 49

def ours?(arg)
  return true if %w[-h --help --version].include?(arg)
  return false unless arg.start_with?("--")
  parser([]).parse([arg])
  true
rescue OptionParser::InvalidOption, OptionParser::AmbiguousOption
  false
rescue OptionParser::ParseError
  true
end

.parser(settings) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dopairb/cli.rb', line 11

def parser(settings)
  OptionParser.new do |o|
    o.require_exact = true
    o.banner = "Usage: dopairb [options] [-- irb options]"
    o.separator ""
    o.separator "IRB with fireworks. Every other option is handed to IRB."
    o.separator ""
    o.on("--intensity=LEVEL", Config::INTENSITIES.map(&:to_s), "off / low / normal (default) / max") { |v| settings << [:intensity, v] }
    o.on("--calm", "low intensity, no flash, shorter effects") { settings.concat([[:intensity, :low], [:flash, :off], [:duration, 0.7]]) }
    o.on("--max", "everything at maximum") { settings << [:intensity, :max] }
    o.on("--party", "max + full-screen flash + sound effects") { settings.concat([[:intensity, :max], [:flash, :full], [:sound, :sfx]]) }
    o.on("--[no-]flash[=MODE]", "flash: off / soft (default) / full") { |v| settings << [:flash, v.nil? ? :soft : (v == false ? :off : v)] }
    o.on("--[no-]motion", "animations (default on)") { |v| settings << [:motion, v] }
    o.on("--[no-]sound[=MODE]", "sound: off (default) / bell / sfx") { |v| settings << [:sound, v.nil? ? :sfx : (v == false ? :off : v)] }
    o.on("--[no-]hud", "COMBO / SCORE next to the prompt (default on)") { |v| settings << [:hud, v] }
    o.on("--[no-]keys", "per-keystroke effects (default on)") { |v| settings << [:keys, v] }
    o.on("--[no-]intro", "title animation (default on)") { |v| settings << [:intro, v] }
    o.on("--duration=X", Float, "effect length multiplier") { |v| settings << [:duration, v] }
    o.on("--set=K=V,...", "any setting, same format as $DOPAIRB") { |v| settings << [:string, v] }
    o.on("--version", "print version") do
      puts "dopairb #{VERSION} (irb #{IRB::VERSION}, reline #{Reline::VERSION})"
      exit
    end
    o.on("-h", "--help", "this help") do
      puts o
      puts
      puts "Settings (also via DOPAIRB=\"key=value,...\" or `dopa key=value` inside the session):"
      puts Config.new.describe
      exit
    end
  end
end

.run_irb(irb_args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dopairb/cli.rb', line 73

def run_irb(irb_args)
  STDOUT.sync = true
  $0 = "dopairb"
  IRB.setup(nil, argv: irb_args)
  Dopairb.enable
  IRB.conf[:PROMPT][:DOPAIRB] = {
    PROMPT_I: "dopairb(%m):%03n> ",
    PROMPT_S: "dopairb(%m):%03n%l ",
    PROMPT_C: "dopairb(%m):%03n* ",
    RETURN: "=> %s\n",
  }
  IRB.conf[:PROMPT_MODE] = :DOPAIRB if IRB.conf[:PROMPT_MODE] == :DEFAULT
  irb = IRB::Irb.new
  Dopairb.session.intro
  irb.run(IRB.conf)
end

.split_args(argv) ⇒ Object



44
45
46
47
# File 'lib/dopairb/cli.rb', line 44

def split_args(argv)
  return [argv[0...argv.index("--")], argv[(argv.index("--") + 1)..]] if argv.include?("--")
  argv.partition { |a| ours?(a) }
end

.start(argv = ARGV) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dopairb/cli.rb', line 60

def start(argv = ARGV)
  ours, irb_args = split_args(argv.dup)
  settings = []
  parser(settings).parse!(ours)
  settings.each do |k, v|
    k == :string ? Dopairb.config.apply_string(v) : Dopairb.config.set(k, v)
  end
  run_irb(irb_args)
rescue OptionParser::ParseError, ArgumentError => e
  warn "dopairb: #{e.message}"
  exit 2
end