Class: Yap::World

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addons:) ⇒ World

Returns a new instance of World.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yap/world.rb', line 29

def initialize(addons:)
  @env = ENV.to_h.dup
  dom = build_editor_dom

  @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

#addonsObject (readonly)

Returns the value of attribute addons.



21
22
23
# File 'lib/yap/world.rb', line 21

def addons
  @addons
end

#contentsObject

Returns the value of attribute contents.



20
21
22
# File 'lib/yap/world.rb', line 20

def contents
  @contents
end

#editorObject

Returns the value of attribute editor.



20
21
22
# File 'lib/yap/world.rb', line 20

def editor
  @editor
end

#envObject

Returns the value of attribute env.



20
21
22
# File 'lib/yap/world.rb', line 20

def env
  @env
end

#last_resultObject

Returns the value of attribute last_result.



23
24
25
# File 'lib/yap/world.rb', line 23

def last_result
  @last_result
end

#promptObject

Returns the value of attribute prompt.



20
21
22
# File 'lib/yap/world.rb', line 20

def prompt
  @prompt
end

#replObject

Returns the value of attribute repl.



20
21
22
# File 'lib/yap/world.rb', line 20

def repl
  @repl
end

#secondary_promptObject

Returns the value of attribute secondary_prompt.



20
21
22
# File 'lib/yap/world.rb', line 20

def secondary_prompt
  @secondary_prompt
end

Class Method Details

.instance(*args) ⇒ Object



25
26
27
# File 'lib/yap/world.rb', line 25

def self.instance(*args)
  @instance ||= new(*args)
end

Instance Method Details

#[](addon_name) ⇒ Object



49
50
51
# File 'lib/yap/world.rb', line 49

def [](addon_name)
  @addons.fetch(addon_name){ raise(ArgumentError, "No addon loaded registered as #{addon_name}") }
end

#bind(key, &blk) ⇒ Object



57
58
59
60
61
# File 'lib/yap/world.rb', line 57

def bind(key, &blk)
  @editor.bind(key) do
    blk.call self
  end
end

#build_editor_domObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/yap/world.rb', line 137

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
  end
end

#eventsObject



53
54
55
# File 'lib/yap/world.rb', line 53

def events
  @editor.events
end

#foreground?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/yap/world.rb', line 96

def foreground?
  Process.getpgrp == Termios.tcgetpgrp($stdout)
end

#func(name, &blk) ⇒ Object



67
68
69
# File 'lib/yap/world.rb', line 67

def func(name, &blk)
  Yap::Shell::ShellCommand.define_shell_function(name, &blk)
end

#historyObject



100
101
102
# File 'lib/yap/world.rb', line 100

def history
  @editor.history
end

#interactive!Object



104
105
106
107
# File 'lib/yap/world.rb', line 104

def interactive!
  refresh_prompt
  @editor.start
end

#refresh_promptObject



124
125
126
# File 'lib/yap/world.rb', line 124

def refresh_prompt
  @editor.prompt = @prompt.update.text
end

#right_prompt_text=(str) ⇒ Object



128
129
130
131
# File 'lib/yap/world.rb', line 128

def right_prompt_text=(str)
  @right_status_float.width = str.length
  @right_status_box.content = str
end

#shell(statement) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/yap/world.rb', line 71

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)
    end
  end

  @last_result
end

#subscribe(*args, &blk) ⇒ Object



133
134
135
# File 'lib/yap/world.rb', line 133

def subscribe(*args, &blk)
  @editor.subscribe(*args, &blk)
end

#unbind(key) ⇒ Object



63
64
65
# File 'lib/yap/world.rb', line 63

def unbind(key)
  @editor.unbind(key)
end