Class: AimsProject::GeometryConsole

Inherits:
Wx::ScrolledWindow
  • Object
show all
Includes:
Aims, Wx
Defined in:
lib/aims_project/geometry_console.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, window) ⇒ GeometryConsole

Returns a new instance of GeometryConsole.



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
104
105
106
# File 'lib/aims_project/geometry_console.rb', line 37

def initialize(app, window)
  super(window)
  @app = app
  @text_ctrl = RichTextCtrl.new(self, :style => Wx::WANTS_CHARS)
  @history = CommandHistory.new
  sizer = BoxSizer.new(VERTICAL)
  sizer.add_item(@text_ctrl, :proportion => 1, :flag => EXPAND | ALL, :border => 5)

  set_auto_layout(true)
  set_sizer(sizer)
  prompt
  
  @text_ctrl.evt_key_down do |evt|
    k = evt.get_key_code
    caret_pos = @text_ctrl.get_insertion_point
    
    case k
    when K_UP
      prev = @history.prev
      @text_ctrl.remove(@line_start, @text_ctrl.get_last_position)
      @text_ctrl.append_text(prev) if prev
    when K_DOWN
      succ = @history.succ
      @text_ctrl.remove(@line_start,@text_ctrl.get_last_position)
      @text_ctrl.append_text(succ) if succ
    when K_LEFT
      if caret_pos > @line_start
        @text_ctrl.set_insertion_point(caret_pos-1)
      end
    when K_RIGHT
      if caret_pos < @text_ctrl.get_last_position
        @text_ctrl.set_insertion_point(caret_pos+1)
      end          
    else
      if caret_pos < @line_start
        @text_ctrl.set_insertion_point(@line_start+1)
      end
      evt.skip
    end
  end
  
  @text_ctrl.evt_char do |evt|
    k = evt.get_key_code
    case k
    when K_RETURN
      cmd = @text_ctrl.get_range(@line_start, @text_ctrl.get_caret_position+1).strip
      @history.save(cmd)
      print "\n"
      # evaluate
      begin
        result = get_binding.eval(cmd)
        if result
          result_str = result.to_s
          print(result_str+"\n") 
        end
      rescue Exception => e
        print(e.message + "\n", Wx::RED)
      end

      prompt
    when K_HOME # For some reason, ctrl-a maps to this
      @text_ctrl.set_insertion_point(@line_start)
    when K_HELP # For some reason, ctrl-e maps to this
      @text_ctrl.set_insertion_point_end
    else
      evt.skip()
    end
  end
  
end

Instance Method Details

#echo(str, color = Wx::BLACK) ⇒ Object



130
131
132
133
# File 'lib/aims_project/geometry_console.rb', line 130

def echo(str, color=Wx::BLACK)
  print(str+"\n", color)
  return nil
end

#geometryObject



141
142
143
# File 'lib/aims_project/geometry_console.rb', line 141

def geometry
  @app.geometry
end

#get_bindingObject



108
109
110
111
112
113
# File 'lib/aims_project/geometry_console.rb', line 108

def get_binding
  unless @binding
    @binding = binding()
  end
  @binding
end

#ls(pattern = "*") ⇒ Object



135
136
137
138
139
# File 'lib/aims_project/geometry_console.rb', line 135

def ls(pattern = "*")
  match = Dir[pattern]
  print(match.join("\n") + "\n")
  return nil
end


122
123
124
125
126
127
128
# File 'lib/aims_project/geometry_console.rb', line 122

def print(str, color=Wx::BLACK)
  start = @text_ctrl.get_caret_position
  @text_ctrl.append_text(str)
  stop = @text_ctrl.get_caret_position
  @text_ctrl.set_style(start..stop, RichTextAttr.new(color))
  
end

#promptObject



115
116
117
118
119
120
# File 'lib/aims_project/geometry_console.rb', line 115

def prompt
  print(">> ")
  @text_ctrl.move_end
  @line_start = @text_ctrl.get_caret_position + 1
  @text_ctrl.show_position(@line_start)
end

#set_geometry(geom) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/aims_project/geometry_console.rb', line 145

def set_geometry(geom)
  if geom.is_a? Aims::Geometry
    puts "All Good"
    @app.show_geometry(GeometryFile.new(geom))
  else
    echo("Not a valid geometry object")
  end
end