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



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

  @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.



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

def addons
  @addons
end

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#editorObject

Returns the value of attribute editor.



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

def editor
  @editor
end

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#last_resultObject

Returns the value of attribute last_result.



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

def last_result
  @last_result
end

#promptObject

Returns the value of attribute prompt.



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

def prompt
  @prompt
end

#replObject

Returns the value of attribute repl.



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

def repl
  @repl
end

#secondary_promptObject

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



76
77
78
# File 'lib/yap/world.rb', line 76

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

#aliasesObject



80
81
82
# File 'lib/yap/world.rb', line 80

def aliases
  Yap::Shell::Aliases.instance
end

#bind(key, &blk) ⇒ Object



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

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

#build_editor_domObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
# File 'lib/yap/world.rb', line 189

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

#builtinsObject



84
85
86
# File 'lib/yap/world.rb', line 84

def builtins
  Yap::Shell::BuiltinCommand.builtins.keys.map(&:to_s)
end

#configurationObject



72
73
74
# File 'lib/yap/world.rb', line 72

def configuration
  ::Yap.configuration
end

#eventsObject



92
93
94
# File 'lib/yap/world.rb', line 92

def events
  @editor.events
end

#foreground?Boolean



139
140
141
# File 'lib/yap/world.rb', line 139

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

#func(name, &blk) ⇒ Object



110
111
112
# File 'lib/yap/world.rb', line 110

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

#historyObject



143
144
145
# File 'lib/yap/world.rb', line 143

def history
  @editor.history
end

#interactive!Object



147
148
149
150
# File 'lib/yap/world.rb', line 147

def interactive!
  refresh_prompt
  @editor.start
end

#refresh_promptObject



176
177
178
# File 'lib/yap/world.rb', line 176

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

#reload!Object



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

def reload!
  exec File.expand_path($0)
end

#right_prompt_text=(str) ⇒ Object



180
181
182
183
# File 'lib/yap/world.rb', line 180

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

#shell(statement) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/yap/world.rb', line 114

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

#shell_commandsObject



88
89
90
# File 'lib/yap/world.rb', line 88

def shell_commands
  Yap::Shell::ShellCommand.registered_functions.keys.map(&:to_s)
end

#subscribe(*args, &blk) ⇒ Object



185
186
187
# File 'lib/yap/world.rb', line 185

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

#unbind(key) ⇒ Object



102
103
104
# File 'lib/yap/world.rb', line 102

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