Class: Ankit::Runtime
- Inherits:
-
Object
- Object
- Ankit::Runtime
- Defined in:
- lib/ankit/runtime.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
- #line ⇒ Object
- #stderr ⇒ Object
- #stdin ⇒ Object
- #stdout ⇒ Object
Class Method Summary collapse
- .parse_options(args) ⇒ Object
- .run(args) ⇒ Object
- .setup(args) ⇒ Object
- .split_subcommand(args) ⇒ Object
Instance Method Summary collapse
- #clear_screen ⇒ Object
- #dispatch(args) ⇒ Object
-
#dispatch_then(args) ⇒ Object
To encourage one-liner.
-
#initialize(config) ⇒ Runtime
constructor
A new instance of Runtime.
- #make_command(args) ⇒ Object
- #supress_io ⇒ Object
- #unsupress_io(saved) ⇒ Object
- #with_supressing_io(&block) ⇒ Object
Constructor Details
#initialize(config) ⇒ Runtime
Returns a new instance of Runtime.
120 121 122 |
# File 'lib/ankit/runtime.rb', line 120 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
73 74 75 |
# File 'lib/ankit/runtime.rb', line 73 def config @config end |
#line ⇒ Object
75 |
# File 'lib/ankit/runtime.rb', line 75 def line; @line ||= HighLine.new; end |
#stderr ⇒ Object
78 |
# File 'lib/ankit/runtime.rb', line 78 def stderr; @stderr ||= STDERR; end |
#stdin ⇒ Object
76 |
# File 'lib/ankit/runtime.rb', line 76 def stdin; @stdin ||= STDIN; end |
#stdout ⇒ Object
77 |
# File 'lib/ankit/runtime.rb', line 77 def stdout; @stdout ||= STDOUT; end |
Class Method Details
.parse_options(args) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ankit/runtime.rb', line 90 def self.(args) = {} OptionParser.new do |spec| spec.on("-c", "--config FILE", "Specifies config file") { |file| [:config] = file } end.parse(args) [:noconf] = [:config].nil? [:config] ||= Config::DEFAULT_PATH end |
.run(args) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ankit/runtime.rb', line 107 def self.run(args) splitted = self.split_subcommand(args) r = self.setup(splitted[:global]) if splitted[:subcommand].empty? r.dispatch([ChallengeCommand.command_name]) else r.dispatch(splitted[:subcommand]) end rescue ExpectedFatalError print("Error:", $!., "\n") exit(1) end |
.setup(args) ⇒ Object
101 102 103 104 105 |
# File 'lib/ankit/runtime.rb', line 101 def self.setup(args) = self.(args) Config.prepare_default if [:noconf] and not File.exist?(Config::DEFAULT_PATH) r = self.new(Config.open([:config])) end |
.split_subcommand(args) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/ankit/runtime.rb', line 80 def self.split_subcommand(args) names = Command.by_name.keys i = args.find_index { |a| names.include?(a) } unless i.nil? { global: args[0 ... i], subcommand: args[i .. -1] } else { global: [], subcommand: [] } end end |
Instance Method Details
#clear_screen ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/ankit/runtime.rb', line 147 def clear_screen w = HighLine::SystemExtensions.terminal_size[1] stdout.print("\033[#{w}D") stdout.print("\033[2J") h = HighLine::SystemExtensions.terminal_size[0] stdout.print("\033[#{h}A") end |
#dispatch(args) ⇒ Object
129 130 131 132 133 |
# File 'lib/ankit/runtime.rb', line 129 def dispatch(args) command = make_command(args) command.execute() command end |
#dispatch_then(args) ⇒ Object
To encourage one-liner
136 137 138 139 |
# File 'lib/ankit/runtime.rb', line 136 def dispatch_then(args) dispatch(args) self end |
#make_command(args) ⇒ Object
124 125 126 127 |
# File 'lib/ankit/runtime.rb', line 124 def make_command(args) name = args.shift Command.by_name[name].new(self, args) end |
#supress_io ⇒ Object
141 142 143 144 145 |
# File 'lib/ankit/runtime.rb', line 141 def supress_io saved = [@stdin, @stdout, @stderr] ["stdin=", "stdout=", "stderr="].each { |m| self.send(m, StringIO.new) } saved end |
#unsupress_io(saved) ⇒ Object
155 156 157 |
# File 'lib/ankit/runtime.rb', line 155 def unsupress_io(saved) @stdin, @stdout, @stderr = saved end |
#with_supressing_io(&block) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/ankit/runtime.rb', line 159 def with_supressing_io(&block) saved = supress_io begin block.call ensure unsupress_io(saved) end end |