Class: Yap::Shell::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/yap/shell.rb

Instance Method Summary collapse

Constructor Details

#initialize(addons:) ⇒ Impl

Returns a new instance of Impl.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yap/shell.rb', line 25

def initialize(addons:)
  @original_file_descriptor_flags = {
    stdin: $stdin.fcntl(Fcntl::F_GETFL, 0),
    stdout: $stdout.fcntl(Fcntl::F_GETFL, 0),
    stderr: $stderr.fcntl(Fcntl::F_GETFL, 0)
  }

  @stdin = $stdin
  @stdout = $stdout
  @stderr = $stderr

  @stdout.sync = true
  @stderr.sync = true

  @world = Yap::World.instance(addons:addons)
end

Instance Method Details

#replObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yap/shell.rb', line 63

def repl
  context = Yap::Shell::Execution::Context.new(
    stdin:  @stdin,
    stdout: @stdout,
    stderr: @stderr
  )

  @world.repl.on_input do |input|
    evaluation = Yap::Shell::Evaluation.new(stdin:@stdin, stdout:@stdout, stderr:@stderr, world:@world)
    evaluation.evaluate(input) do |command, stdin, stdout, stderr, wait|
      context.clear_commands
      context.add_command_to_run command, stdin:stdin, stdout:stdout, stderr:stderr, wait:wait

      with_original_file_descriptor_flags do
        context.execute(world:@world)
      end
    end
  end

  begin
    @world.interactive!
  # rescue Errno::EIO => ex
  #   # This happens when yap is no longer the foreground process
  #   # but it tries to receive input/output from the tty. I believe it
  #   # is a race condition when launching a child process.
  rescue Interrupt
    @world.editor.puts "^C"
    retry
  rescue Exception => ex
    require 'pry'
    binding.pry unless ex.is_a?(SystemExit)
  end
end

#with_original_file_descriptor_flags(&block) ⇒ Object

Yields to the passed in block after restoring the file descriptor flags that Yap started in. This ensures that any changes Yap has made to run the shell don’t interfere with child processes.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yap/shell.rb', line 45

def with_original_file_descriptor_flags(&block)
  current_file_descriptor_flags = {
    stdin: $stdin.fcntl(Fcntl::F_GETFL, 0),
    stdout: $stdout.fcntl(Fcntl::F_GETFL, 0),
    stderr: $stderr.fcntl(Fcntl::F_GETFL, 0)
  }

  $stdin.fcntl(Fcntl::F_SETFL, @original_file_descriptor_flags[:stdin])
  $stdout.fcntl(Fcntl::F_SETFL, @original_file_descriptor_flags[:stdout])
  $stderr.fcntl(Fcntl::F_SETFL, @original_file_descriptor_flags[:stderr])

  yield
ensure
  $stdin.fcntl(Fcntl::F_SETFL, current_file_descriptor_flags[:stdin])
  $stdout.fcntl(Fcntl::F_SETFL, current_file_descriptor_flags[:stdout])
  $stderr.fcntl(Fcntl::F_SETFL, current_file_descriptor_flags[:stderr])
end