Class: Yap::World
- Inherits:
-
Object
- Object
- Yap::World
- Extended by:
- Forwardable
- Includes:
- Term::ANSIColor
- Defined in:
- lib/yap/world.rb,
lib/yap/world/addons.rb
Defined Under Namespace
Modules: AddonMethods, Addons, Namespace, UserAddons Classes: Addon
Constant Summary collapse
- DEFAULTS =
{ primary_prompt_text: "yap> ", secondary_prompt_text: "> " }
Instance Attribute Summary collapse
-
#addons ⇒ Object
readonly
Returns the value of attribute addons.
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#editor ⇒ Object
Returns the value of attribute editor.
-
#env ⇒ Object
Returns the value of attribute env.
-
#last_result ⇒ Object
Returns the value of attribute last_result.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#repl ⇒ Object
Returns the value of attribute repl.
-
#secondary_prompt ⇒ Object
Returns the value of attribute secondary_prompt.
Class Method Summary collapse
Instance Method Summary collapse
- #[](addon_name) ⇒ Object
- #aliases ⇒ Object
- #bind(key, &blk) ⇒ Object
- #build_editor_dom ⇒ Object
- #builtins ⇒ Object
- #configuration ⇒ Object
- #events ⇒ Object
- #foreground? ⇒ Boolean
- #func(name, &blk) ⇒ Object
- #history ⇒ Object
-
#initialize(addons:) ⇒ World
constructor
A new instance of World.
- #interactive! ⇒ Object
- #refresh_prompt ⇒ Object
- #reload! ⇒ Object
- #right_prompt_text=(str) ⇒ Object
- #shell(statement) ⇒ Object
- #shell_commands ⇒ Object
- #subscribe(*args, &blk) ⇒ Object
- #unbind(key) ⇒ Object
Constructor Details
#initialize(addons:) ⇒ World
Returns a new instance of World.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/yap/world.rb', line 30 def initialize(addons:) @env = ENV.to_h.dup dom = build_editor_dom # ensure yap directory exists if !File.exists?(configuration.yap_path) if configuration.skip_first_time? # skipping first time else puts puts yellow("Yap directory not found: #{configuration.yap_path}") puts puts "Initializing yap for the first time:" puts print " Creating #{configuration.yap_path} " FileUtils.mkdir_p configuration.yap_path puts green("done") print " Creating default #{configuration.preferred_yaprc_path} " FileUtils.cp configuration.yaprc_template_path, configuration.yap_path puts green("done") puts puts "To tweak yap take a look at #{configuration.preferred_yaprc_path}." puts puts "Reloading shell" reload! end end @editor = RawLine::Editor.create(dom: dom) self.prompt = Yap::Shell::Prompt.new(text: DEFAULTS[:primary_prompt_text]) self.secondary_prompt = Yap::Shell::Prompt.new(text: DEFAULTS[:secondary_prompt_text]) @repl = Yap::Shell::Repl.new(world:self) @addons = addons.reduce(Hash.new) do |hsh, addon| hsh[addon.addon_name] = addon hsh end # initialize after they are all loaded in case they reference each other. addons.each { |addon| addon.initialize_world(self) } end |
Instance Attribute Details
#addons ⇒ Object (readonly)
Returns the value of attribute addons.
22 23 24 |
# File 'lib/yap/world.rb', line 22 def addons @addons end |
#contents ⇒ Object
Returns the value of attribute contents.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def contents @contents end |
#editor ⇒ Object
Returns the value of attribute editor.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def editor @editor end |
#env ⇒ Object
Returns the value of attribute env.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def env @env end |
#last_result ⇒ Object
Returns the value of attribute last_result.
24 25 26 |
# File 'lib/yap/world.rb', line 24 def last_result @last_result end |
#prompt ⇒ Object
Returns the value of attribute prompt.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def prompt @prompt end |
#repl ⇒ Object
Returns the value of attribute repl.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def repl @repl end |
#secondary_prompt ⇒ Object
Returns the value of attribute secondary_prompt.
21 22 23 |
# File 'lib/yap/world.rb', line 21 def secondary_prompt @secondary_prompt end |
Class Method Details
.instance(*args) ⇒ Object
26 27 28 |
# File 'lib/yap/world.rb', line 26 def self.instance(*args) @instance ||= new(*args) end |
Instance Method Details
#[](addon_name) ⇒ Object
80 81 82 |
# File 'lib/yap/world.rb', line 80 def [](addon_name) @addons.fetch(addon_name){ raise(ArgumentError, "No addon loaded registered as #{addon_name}") } end |
#aliases ⇒ Object
84 85 86 |
# File 'lib/yap/world.rb', line 84 def aliases Yap::Shell::Aliases.instance end |
#bind(key, &blk) ⇒ Object
100 101 102 103 104 |
# File 'lib/yap/world.rb', line 100 def bind(key, &blk) @editor.bind(key) do blk.call self end end |
#build_editor_dom ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/yap/world.rb', line 204 def build_editor_dom @left_status_box = TerminalLayout::Box.new(content: "", style: {display: :inline}) @right_status_box = TerminalLayout::Box.new(content: "", style: {display: :inline}) @prompt_box = TerminalLayout::Box.new(content: "yap>", style: {display: :inline}) @input_box = TerminalLayout::InputBox.new(content: "", style: {display: :inline}) @content_box = TerminalLayout::Box.new(content: "", style: {display: :block}) @bottom_left_status_box = TerminalLayout::Box.new(content: "", style: {display: :inline}) @bottom_right_status_box = TerminalLayout::Box.new(content: "", style: {display: :inline}) @right_status_float = TerminalLayout::Box.new(style: {display: :float, float: :right, width: @right_status_box.content.length}, children: [ @right_status_box ] ) RawLine::DomTree.new( children:[ @right_status_float, @left_status_box, @prompt_box, @input_box, @content_box, TerminalLayout::Box.new(style: {display: :float, float: :left, width: @bottom_left_status_box.content.length}, children: [ @bottom_left_status_box ] ), TerminalLayout::Box.new(style: {display: :float, float: :right, width: @bottom_right_status_box.content.length}, children: [ @bottom_right_status_box ] ) ] ).tap do |dom| dom.prompt_box = @prompt_box dom.input_box = @input_box dom.content_box = @content_box @prompt_box.name = "prompt-box" @input_box.name = "input-box" @content_box.name = "content-box" end end |
#builtins ⇒ Object
88 89 90 |
# File 'lib/yap/world.rb', line 88 def builtins Yap::Shell::BuiltinCommand.builtins.keys.map(&:to_s) end |
#configuration ⇒ Object
76 77 78 |
# File 'lib/yap/world.rb', line 76 def configuration ::Yap.configuration end |
#events ⇒ Object
96 97 98 |
# File 'lib/yap/world.rb', line 96 def events @editor.events end |
#foreground? ⇒ Boolean
153 154 155 156 |
# File 'lib/yap/world.rb', line 153 def foreground? return unless STDIN.isatty Process.getpgrp == Termios.tcgetpgrp($stdout) end |
#func(name, &blk) ⇒ Object
114 115 116 |
# File 'lib/yap/world.rb', line 114 def func(name, &blk) Yap::Shell::ShellCommand.define_shell_function(name, &blk) end |
#history ⇒ Object
158 159 160 |
# File 'lib/yap/world.rb', line 158 def history @editor.history end |
#interactive! ⇒ Object
162 163 164 165 |
# File 'lib/yap/world.rb', line 162 def interactive! refresh_prompt @editor.start end |
#refresh_prompt ⇒ Object
191 192 193 |
# File 'lib/yap/world.rb', line 191 def refresh_prompt @editor.prompt = @prompt.update.text end |
#reload! ⇒ Object
110 111 112 |
# File 'lib/yap/world.rb', line 110 def reload! exec File.($0) end |
#right_prompt_text=(str) ⇒ Object
195 196 197 198 |
# File 'lib/yap/world.rb', line 195 def right_prompt_text=(str) @right_status_float.width = str.length @right_status_box.content = str end |
#shell(statement) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/yap/world.rb', line 118 def shell(statement) context = Yap::Shell::Execution::Context.new( stdin: $stdin, stdout: $stdout, stderr: $stderr ) if statement.nil? @last_result = Yap::Shell::Execution::Result.new( status_code: 1, directory: Dir.pwd, n: 1, of: 1 ) else evaluation = Yap::Shell::Evaluation.new(stdin:$stdin, stdout:$stdout, stderr:$stderr, world:self) evaluation.evaluate(statement) do |command, stdin, stdout, stderr, wait| context.clear_commands context.add_command_to_run command, stdin:stdin, stdout:stdout, stderr:stderr, wait:wait @last_result = context.execute(world:self) || 0 if @last_result.is_a?(Integer) Yap::Shell::Execution::Result.new( status_code: @last_result, directory: Dir.pwd, n: 1, of: 1 ) else @last_result end end end @last_result end |
#shell_commands ⇒ Object
92 93 94 |
# File 'lib/yap/world.rb', line 92 def shell_commands Yap::Shell::ShellCommand.registered_functions.keys.map(&:to_s) end |
#subscribe(*args, &blk) ⇒ Object
200 201 202 |
# File 'lib/yap/world.rb', line 200 def subscribe(*args, &blk) @editor.subscribe(*args, &blk) end |
#unbind(key) ⇒ Object
106 107 108 |
# File 'lib/yap/world.rb', line 106 def unbind(key) @editor.unbind(key) end |