class Vizkit::Console
Constants
- COMMAND_TYPE
- ERROR_TYPE
- RESULT_TYPE
- STDERR_TYPE
- STDOUT_TYPE
- TEXT_TYPE
Public Class Methods
colors()
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 55 def self.colors if !@colors @colors = Hash.new @colors[:black] = Qt::Color.new(0,0,0) @colors[:gray] = Qt::Color.new(120,120,120) @colors[:red] = Qt::Color.new(255,0,0) @colors[:blue] = Qt::Color.new(0,0,255) @colors[31] = Qt::Color.new(255,0,0) @colors[35] = Qt::Color.new(0,255,0) end @colors end
new(parent=nil)
click to toggle source
Calls superclass method
# File lib/vizkit/widgets/console/console.rb, line 68 def initialize(parent=nil) super resize(600,350) @history = [""] @history_index = 0 @console_context = ConsoleContext.new(self) @console_binding = @console_context.instance_eval("binding") @colors = Console.colors @cursor = "> " @font = Qt::Font.new("Monospace",10) setCursorWidth(10) setAcceptRichText(false) setContextMenuPolicy Qt::NoContextMenu append @console_context.help,:gray,Console::RESULT_TYPE new_command deselect end
Public Instance Methods
add_dynamic_obj(obj,port_name)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 119 def add_dynamic_obj(obj,port_name) port_name =~ /.*\.(\w*)$/ name = if $1 $1 else port_name end if !@console_context.respond_to? name insert_text "Added #{name}(#{obj.class.name}). You can access it via '#{name}'.\n"+ "This object will automatically be updated.",:blue @console_context.instance_eval("class << self;self;end").send(:define_method,name) do instance_variable_get("@#{name}") end if command.empty? replace_current_command(name) end end @console_context.instance_variable_set("@#{name}",obj) end
add_obj(obj,options)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 103 def add_obj(obj,options) name = obj.basename.downcase if @console_context.respond_to? name insert_text "Cannot Add #{name}(#{obj.class.name}). #{name} is already defined." else insert_text "Added #{name}(#{obj.class.name}). You can access it via #{name}.",:blue @console_context.instance_eval("class << self;self;end").send(:define_method,name) do instance_variable_get("@#{name}") end @console_context.instance_variable_set("@#{name}",obj) if command.empty? replace_current_command(name) end end end
append(val,color=nil,type=Console::RESULT_TYPE)
click to toggle source
Calls superclass method
# File lib/vizkit/widgets/console/console.rb, line 150 def append(val,color=nil,type=Console::RESULT_TYPE) values = val.to_s.split("\n") values.each do |val| val =~ /\e\[(\d*)m(.*)/ val = $2 if $2 if $1 append(val,$1.to_i,type) else color = if color.is_a?(Qt::Color) color elsif color && @colors.has_key?(color) @colors[color] else @colors[:black] end setTextColor(color) setCurrentFont(@font) if type == COMMAND_TYPE super val else super(" "*@cursor.size+val) end textCursor.block.setUserState(type) end end end
capture_all_output() { || ... }
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 199 def capture_all_output(&block) real_stdout, $stdout = $stdout, StringIO.new real_stderr, $stderr = $stderr, StringIO.new logdev = Vizkit.logger.instance_variable_get(:@logdev) real_dev = logdev.instance_variable_get(:@dev) logdev.instance_variable_set(:@dev,$stdout) yield [$stdout.string,$stderr.string] ensure $stdout = real_stdout $stderr = real_stderr logdev.instance_variable_set(:@dev,real_dev) end
clear()
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 90 def clear document().clear deselect end
command()
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 95 def command textCursor.block.text[@cursor.size..-1] end
deselect()
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 213 def deselect #we have to set the cursor otherwise there will be no effect cursor = textCursor cursor.movePosition(Qt::TextCursor::End,Qt::TextCursor::MoveAnchor) setTextCursor(cursor) ensureCursorVisible end
insert_text(text,color=:blue)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 139 def insert_text(text,color=:blue) deselect cursor = textCursor cursor.select(Qt::TextCursor::LineUnderCursor) old_text = cursor.selectedText cursor.removeSelectedText append text,color,Console::TEXT_TYPE new_command(old_text[@cursor.size..-1]) nil end
interprete_command(cmd)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 177 def interprete_command(cmd) return unless cmd @history << cmd @history_index = @history.size result = nil error = nil out = capture_all_output do begin result = eval(cmd,@console_binding,__FILE__,__LINE__).to_s rescue Exception => e error = e.message end end if out append out.first,:gray, Console::STDOUT_TYPE if !out.first.empty? append out[1],:red,Console::STDERR_TYPE if !out[1].empty? end append error,:red,Console::ERROR_TYPE if error && !error.empty? append result,:gray,Console::RESULT_TYPE if result && !result.empty? new_command end
keyPressEvent(event)
click to toggle source
Calls superclass method
# File lib/vizkit/widgets/console/console.rb, line 253 def keyPressEvent(event) #block paste outside the last line if event.modifiers == Qt::ControlModifier case event.key when 86 # ctrl-v deselect return super when 67 #ctrl-c return super end end case event.key when 16777234 #left if textCursor.positionInBlock > @cursor.size super else if textCursor.blockNumber > 0 cursor = textCursor cursor.movePosition(Qt::TextCursor::Up,Qt::TextCursor::MoveAnchor) cursor.movePosition(Qt::TextCursor::EndOfLine,Qt::TextCursor::MoveAnchor) setTextCursor(cursor) end end when 16777236 #right if textCursor.positionInBlock < textCursor.block.length-1 super else if textCursor.blockNumber < document.blockCount-1 cursor = textCursor cursor.movePosition(Qt::TextCursor::Down,Qt::TextCursor::MoveAnchor) cursor.movePosition(Qt::TextCursor::StartOfLine,Qt::TextCursor::MoveAnchor) cursor.movePosition(Qt::TextCursor::NextCharacter,Qt::TextCursor::MoveAnchor,@cursor.size) setTextCursor(cursor) else super end end when 16777235 #up if textCursor.blockNumber == document.blockCount-1 prev_command(-1) else super end when 16777237 #down if textCursor.blockNumber == document.blockCount-1 prev_command(1) else super end when 16777220 #return deselect interprete_command(command) when 16777219 #backspace if textCursor.hasSelection deselect else super if textCursor.positionInBlock > @cursor.size end when 16777232 cursor = textCursor cursor.movePosition(Qt::TextCursor::StartOfLine,Qt::TextCursor::MoveAnchor) cursor.movePosition(Qt::TextCursor::NextCharacter,Qt::TextCursor::MoveAnchor,@cursor.size) setTextCursor(cursor) ensureCursorVisible when 16777233 super when 16777217 #tab #super # TODO auto completion else #puts event.key if textCursor.blockNumber != document.blockCount-1 && event.key < 1000 deselect else deselect if textCursor.positionInBlock < @cursor.size super end end end
mouseReleaseEvent(event)
click to toggle source
Calls superclass method
# File lib/vizkit/widgets/console/console.rb, line 241 def mouseReleaseEvent(event) if event.button == Qt::MidButton if textCursor.hasSelection copy end deselect paste else super end end
multi_value?()
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 86 def multi_value? true end
new_command(new_command="")
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 99 def new_command(new_command="") append @cursor+new_command,:black,Console::COMMAND_TYPE end
prev_command(direction=-1)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 229 def prev_command(direction=-1) @history_index += direction @history_index = 0 if @history_index < 0 cmd = if @history_index >= @history.size @history_index = @history.size "" else @history[@history_index] end replace_current_command(cmd) end
replace_current_command(val)
click to toggle source
# File lib/vizkit/widgets/console/console.rb, line 221 def replace_current_command(val) cursor = textCursor cursor.select(Qt::TextCursor::LineUnderCursor) cursor.removeSelectedText cursor.insertText(@cursor+val) ensureCursorVisible end