Class: Cosmos::PryDialog

Inherits:
Qt::Dialog show all
Defined in:
lib/cosmos/gui/dialogs/pry_dialog.rb

Instance Method Summary collapse

Methods inherited from Qt::Dialog

#exec

Constructor Details

#initialize(parent, pry_binding, title = 'Pry Dialog') ⇒ PryDialog

Returns a new instance of PryDialog.



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 30

def initialize(parent, pry_binding, title = 'Pry Dialog')
  super(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
  setMinimumWidth(700)
  setMinimumHeight(400)
  @queue = Queue.new

  setWindowTitle(title)

  layout = Qt::VBoxLayout.new
  @text_edit = Qt::PlainTextEdit.new(self)
  @text_edit.setReadOnly(true)
  if Kernel.is_windows?
    @text_edit.font = Cosmos.getFont('courier', 9)
  else
    @text_edit.font = Cosmos.getFont('courier', 12)
  end
  layout.addWidget(@text_edit)

  @pry_history = []
  @pry_frame = Qt::HBoxLayout.new
  @pry_frame.setContentsMargins(0,0,0,0)
  @pry_frame_label = Qt::Label.new("Pry:")
  @pry_frame.addWidget(@pry_frame_label)
  @pry_text = PryLineEdit.new(self)
  @pry_text.setFocus(Qt::OtherFocusReason)
  @pry_text.keyPressCallback = lambda do |event|
    return_value = true
    case event.key
    when Qt::Key_Return, Qt::Key_Enter
      pry_text = @pry_text.text
      @pry_history.unshift(pry_text)
      @pry_history_index = 0
      if pry_text.strip == 'exit' or pry_text.strip == 'quit'
        return_value = false
        self.close
      else
        sendToPry(pry_text)
        @pry_text.setText('')
      end
    when Qt::Key_Up
      if @pry_history.length > 0
        @pry_text.setText(@pry_history[@pry_history_index])
        @pry_history_index += 1
        if @pry_history_index == @pry_history.length
          @pry_history_index = @pry_history.length-1
        end
      end
    when Qt::Key_Down
      if @pry_history.length > 0
        @pry_text.setText(@pry_history[@pry_history_index])
        @pry_history_index -= 1
        @pry_history_index = 0 if @pry_history_index < 0
      end
    when Qt::Key_Escape
      @pry_text.setText("")
    end
    return_value
  end
  @pry_frame.addWidget(@pry_text)
  layout.addLayout(@pry_frame)

  self.setLayout(layout)
  self.show
  self.raise

  # Attach pry
  @pry_thread = Thread.new do
    Pry.config.pager = false
    Pry.config.color = false
    Pry.config.correct_indent = false
    Pry.start pry_binding, :input => self, :output => self
    @pry_thread = nil
  end
end

Instance Method Details

#closeEvent(event) ⇒ Object



153
154
155
156
157
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 153

def closeEvent(event)
  super(event)
  Cosmos.kill_thread(self, @pry_thread)
  self.dispose
end

#graceful_killObject



159
160
161
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 159

def graceful_kill
  sendToPry("throw :breakout")
end


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 131

def print(*args)
  Qt.execute_in_main_thread(true) do
    if String === args[0]
      @text_edit.appendPlainText(args[0])
    else
      args.each do |string|
        @text_edit.appendPlainText(string)
      end
    end
  end
end

#puts(*args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 115

def puts(*args)
  Qt.execute_in_main_thread(true) do
    if String === args[0]
      @text_edit.appendPlainText(args[0])
    else
      args.each do |string|
        if string[-1..-1] == "\n"
          @text_edit.appendPlainText(string)
        else
          @text_edit.appendPlainText(string + "\n")
        end
      end
    end
  end
end

#readline(sep = nil, limit = nil) ⇒ Object

sep and limit needed to meet the pry API



111
112
113
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 111

def readline(sep = nil, limit = nil)
  @queue.pop
end

#rejectObject



147
148
149
150
151
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 147

def reject
  super()
  Cosmos.kill_thread(self, @pry_thread)
  self.dispose
end

#sendToPry(text) ⇒ Object



105
106
107
108
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 105

def sendToPry(text)
  @text_edit.appendPlainText(text)
  @queue << text
end

#tty?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/cosmos/gui/dialogs/pry_dialog.rb', line 143

def tty?
  false
end