Class: Termular::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/termular/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUI

Returns a new instance of UI.



161
162
163
164
# File 'lib/termular/ui.rb', line 161

def initialize
  @cursor_offset = 0
  @expression = ""
end

Instance Attribute Details

#current_commandObject

Returns the value of attribute current_command.



3
4
5
# File 'lib/termular/ui.rb', line 3

def current_command
  @current_command
end

#current_graphObject

Returns the value of attribute current_graph.



3
4
5
# File 'lib/termular/ui.rb', line 3

def current_graph
  @current_graph
end

#cursor_offsetObject

Returns the value of attribute cursor_offset.



3
4
5
# File 'lib/termular/ui.rb', line 3

def cursor_offset
  @cursor_offset
end

#expressionObject

Returns the value of attribute expression.



3
4
5
# File 'lib/termular/ui.rb', line 3

def expression
  @expression
end

#insert_modeObject

Returns the value of attribute insert_mode.



3
4
5
# File 'lib/termular/ui.rb', line 3

def insert_mode
  @insert_mode
end

#invalidate_nextObject

Returns the value of attribute invalidate_next.



3
4
5
# File 'lib/termular/ui.rb', line 3

def invalidate_next
  @invalidate_next
end

Instance Method Details

#dispatch_insert_keypress(k) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/termular/ui.rb', line 74

def dispatch_insert_keypress(k)
  case k[0].ord
  when 127 # backspace
    unless cursor_offset.zero?
      @cursor_offset -= 1
      expression[cursor_offset..cursor_offset] = ""
    end
  when 10 # enter
    begin
      @current_command = Parser.parse! expression
      if current_command.is_a? AST::CartesianCommand
        @current_graph = Graph::Cartesian.new current_command.expression
        @needs_tick = expression =~ /time/
      elsif current_command.is_a? AST::PolarCommand
        @current_graph = Graph::Polar.new current_command.expression
        @needs_tick = expression =~ /time/
      elsif current_command.is_a? AST::OptionCommand
        if current_graph
          current_graph.options[current_command.option] = current_command.expression.eval(Context.global)
          current_graph.invalidate
        end
      elsif current_command.is_a? AST::QuitCommand
        exit
      end
    rescue => e
      show_exception "#{e.class.name} #{e.message}"
    end
  when 27 # special key
    if k == "\e"
      # legit escape
      @insert_mode = false
      return
    end
    case k[1..-1]
    when "OH" # home
      @cursor_offset = 0
    when "OF" # end
      @cursor_offset = expression.length
    when "OD" # arrow left
      @cursor_offset -= 1 unless cursor_offset.zero?
    when "OC" # arrow right
      @cursor_offset += 1 unless cursor_offset == expression.length
    when "[3~"  
      expression[cursor_offset..cursor_offset] = ""
    else
#          print k.each_byte.to_a.join ","
#          raise k[1..-1]#k.each_byte.to_a.join ","
    end
  when 0x20..0x7E
#        expression << k.ord.to_s << " "
    expression[cursor_offset, 0] = k
    @cursor_offset += 1
  end
end

#dispatch_keypress(k) ⇒ Object



25
26
27
28
29
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
# File 'lib/termular/ui.rb', line 25

def dispatch_keypress(k)
  if invalidate_next
    current_graph and current_graph.invalidate
    @invalidate_next = false
  end
  return dispatch_insert_keypress k if insert_mode
  case k[0]
  when "h" # move left
    current_graph.pan -0.2, 0
  when "H"
    current_graph.pan -0.05, 0
  when "l" # move right
    current_graph.pan 0.2, 0
  when "L"
    current_graph.pan 0.05, 0
  when "j" # move down
    current_graph.pan 0, -0.2
  when "J"
    current_graph.pan 0, -0.05
  when "k" # move up
    current_graph.pan 0, 0.2
  when "K"
    current_graph.pan 0, 0.05
  when " " # center
    current_graph.center
  when "]" # zoom in
    current_graph.zoom 2
  when "[" # zoom out
    current_graph.zoom 0.5
  when "i"
    @insert_mode = true
  when ":"
    @insert_mode = true
    @expression = ":"
    @cursor_offset = 1
  when "o"
    @insert_mode = true
    @expression = ""
    @cursor_offset = 0
  else
    print Console.move 0, 0
    print k.ord
  end

  #print Termular::Console.move 0, Termular::Console.rows
  #print c.ord
  exit if k[0] == "\3"
end

#draw_status_lineObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/termular/ui.rb', line 5

def draw_status_line
  Console.buffered_print do |s|
    s <<  Console.move(0, Console.rows) <<
          Console.clear_line <<
          Console.color(:black, :white) <<
          (" " * Console.cols) <<
          Console.move(0, Console.rows) <<
          expression
    if insert_mode
      msg = "--INSERT--"
      s <<  Console.move(Console.cols - msg.length + 1, Console.rows) << 
            Console.color(:black, :white) << msg <<
            Console.move(cursor_offset + 1, Console.rows)
    else
      s << Console.move(Console.cols, Console.rows)
    end
    s << Console.reset
  end
end

#needs_tick?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/termular/ui.rb', line 157

def needs_tick?
  @needs_tick
end

#renderObject



142
143
144
145
146
147
148
149
150
# File 'lib/termular/ui.rb', line 142

def render
  Console.clear
  begin
    current_graph.render if current_graph and current_graph.needs_redraw
  rescue => e
    show_exception e.message
  end
  draw_status_line
end

#show_exception(msg) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/termular/ui.rb', line 129

def show_exception(msg)
  Console.buffered_print do |s|
    s <<  Console.move(0, Console.rows - 1) <<
          Console.color(:bright, :white, :red) <<
          msg <<
          Console.reset
  end
  # this writes over where the graph goes, so invalidate it
  # however if we invalidate it now, it gets immediately dismissed, so set
  # a special flag
  @invalidate_next = true
end

#tickObject



152
153
154
155
# File 'lib/termular/ui.rb', line 152

def tick
  current_graph and current_graph.invalidate
  render
end