Class: Mrubyc::Debugger::Console

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/mrubyc/debugger/console.rb

Constant Summary collapse

COL_A =
[
  COLOR_BLACK,
  COLOR_BLUE,
  COLOR_CYAN,
  COLOR_GREEN,
  COLOR_MAGENTA,
  COLOR_RED,
  COLOR_WHITE,
  COLOR_YELLOW
]
ESCDELAY =
25

Instance Method Summary collapse

Constructor Details

#initialize(loops) ⇒ Console

Returns a new instance of Console.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mrubyc/debugger/console.rb', line 24

def initialize(loops)
  @srcs = []
  loops.each do |loop|
    src = []
    File.open(loop, 'r') do |f|
      f.each_line do |line|
        src << line
      end
    end
    @srcs << src
  end
  cbreak # raw?
  noecho # stop echo back
  set_escdelay ESCDELAY # response speed. default 1000
  @cursor_pos = { x: 0, y: 1}
  @sleepers = Array.new(@srcs.size)
  @events = Array.new(@srcs.size)
end

Instance Method Details

#col_sampleObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mrubyc/debugger/console.rb', line 59

def col_sample
  max = COL_A.size
  no = 0
  max.times do |y|
    max.times do |x|
      setpos(y, x * 6)
      s = " %03d "%no
      attron(color_pair(no))
      addstr(s)
      attroff(color_pair(no))
      no += 1
    end
  end
  s = "colors: %d"%(colors)
  setpos(12, 0)
  addstr(s)
  s = "color_pairs: %d"%(color_pairs)
  setpos(13, 0)
  addstr(s)
end

#color_num_by(level) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mrubyc/debugger/console.rb', line 86

def color_num_by(level)
  case level
  when :info
    24
  when :debug
    32
  when :warn
    56
  when :error
    40
  end
end

#make_pairObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/mrubyc/debugger/console.rb', line 48

def make_pair
  # 0 can't be changed
  no = 0
  COL_A.each do |c0|
    COL_A.each do |c1|
      init_pair(no, c0, c1)
      no += 1
    end
  end
end

#run(show_colors_at_start = false) ⇒ Object



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/mrubyc/debugger/console.rb', line 99

def run(show_colors_at_start = false)
  mainwin = init_screen
  mainwin.keypad true
  mainwin.timeout = 0
  curs_set(0)
  start_color
  make_pair
  show_colors if show_colors_at_start

  begin
    wins = []
    num = @srcs.size
    num.times do |i|
      win = {}
      win[:src] = mainwin.subwin(lines - 6, cols / num, 0, i * cols / num)
      win[:out] = mainwin.subwin(1, cols / num, lines - 6, i * cols / num)
      win[:var] = mainwin.subwin(5, cols / num, lines - 5, i * cols / num)
      wins << win
    end
    while true
      @key = mainwin.getch
      handle_key
      num.times do |i|
        wins[i][:src].resize(lines - 6, cols / num)
        wins[i][:out].resize(1, cols / num)
        wins[i][:var].resize(5, cols / num)
        unless $sleep_queues[i].empty?
          @sleepers[i] = $sleep_queues[i].pop
        end
        if @sleepers[i] && @sleepers[i] < Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW, :millisecond)
          @sleepers[i] = nil
          $threads[i].run
        end
        unless $debug_queues[i].empty?
          message = $debug_queues[i].pop
          wins[i][:out].setpos(0, 0)
          color_num = color_num_by(message[:level])
          wins[i][:out].addstr '|'
          wins[i][:out].attron(color_pair color_num)
          wins[i][:out].addstr " #{message[:level].to_s[0].upcase}) " + message[:body].ljust(wins[i][:out].maxx-6)
          wins[i][:out].attroff(color_pair color_num)
          wins[i][:out].addstr '|'
          wins[i][:out].refresh
        end
        unless $event_queues[i].empty?
          @events[i] = $event_queues[i].pop
        end
        # needs this condition in spite of being used at the first time
        # TODO refactor
        if @events[i]
          threads_debug_print(i)
          (1..(wins[i][:src].maxy - 2)).each do |y|
            wins[i][:src].setpos(y, 1)
            if !@srcs[i][y]
              wins[i][:src].addstr ' ' * wins[i][:src].maxx
            else
              lineno = @events[i][:lineno] - 1 # hide `using DebugQueue` line
              wins[i][:src].attron(A_UNDERLINE) if y == @cursor_pos[:y] && i == @cursor_pos[:x]
              wins[i][:src].attron(A_REVERSE) if y == lineno
              lineno_color = if $breakpoints.any? {|bp| bp == [i, y] }
                21
              else
                16
              end
              wins[i][:src].attron(color_pair lineno_color)
              wins[i][:src].attron(A_BOLD)
              wins[i][:src].addstr y.to_s.rjust(2).to_s
              wins[i][:src].attroff(A_BOLD)
              wins[i][:src].attroff(color_pair lineno_color)
              wins[i][:src].addstr ' ' + @srcs[i][y]
              wins[i][:src].attroff(A_REVERSE) if y == lineno
              wins[i][:src].attroff(A_UNDERLINE) if y == @cursor_pos[:y] && i == @cursor_pos[:x]
            end
          end
          wins[i][:src].box(?|,?-,?+)
          wins[i][:src].refresh
          if @events[i][:breakpoint]
            command_line(i, wins[i][:var], @events[i][:tp_binding])
            @events[i][:breakpoint] = nil # to avoid #sleep line remains Thread.stop
          else
            vars = {}
            @events[i][:tp_binding].local_variables.each do |var|
              vars[var] = @events[i][:tp_binding].local_variable_get(var).inspect
            end
            vars.each_with_index do |(k,v),j|
              wins[i][:var].setpos(j+1, 2)
              wins[i][:var].addstr (k.to_s + ' => ' + v).ljust(wins[i][:var].maxx)
            end
            box_var_win(wins[i][:var], 16)
          end
        end
      end
      refresh
    end
  rescue => e
    sleep 5
  ensure
    finish
  end
end

#set_escdelay(ms) ⇒ Object



43
44
45
46
# File 'lib/mrubyc/debugger/console.rb', line 43

def set_escdelay(ms)
  Curses.ESCDELAY = ms
rescue NotImplementedError
end

#show_colorsObject



80
81
82
83
84
# File 'lib/mrubyc/debugger/console.rb', line 80

def show_colors
  col_sample
  refresh
  getch
end