Class: IRB2025Mikan::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/theme/rk2025.rb

Constant Summary collapse

CHARS =
' ▘▝▀▖▌▞▛▗▚▐▜▄▙▟█'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ Canvas

Returns a new instance of Canvas.



7
8
9
10
11
12
13
14
15
16
# File 'lib/irb/theme/rk2025.rb', line 7

def initialize(w, h)
  @w = w
  @h = h
  clear
  # TODO: delete this
  @lines = (@h/2).times.map do
    rand(2..40).times.map{rand(33..126).chr}.join
  end
  @focus_line = rand(@lines.size)
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



5
6
7
# File 'lib/irb/theme/rk2025.rb', line 5

def colors
  @colors
end

#focus_lineObject

Returns the value of attribute focus_line.



6
7
8
# File 'lib/irb/theme/rk2025.rb', line 6

def focus_line
  @focus_line
end

#hObject (readonly)

Returns the value of attribute h.



5
6
7
# File 'lib/irb/theme/rk2025.rb', line 5

def h
  @h
end

#linesObject

Returns the value of attribute lines.



6
7
8
# File 'lib/irb/theme/rk2025.rb', line 6

def lines
  @lines
end

#masksObject (readonly)

Returns the value of attribute masks.



5
6
7
# File 'lib/irb/theme/rk2025.rb', line 5

def masks
  @masks
end

#wObject (readonly)

Returns the value of attribute w.



5
6
7
# File 'lib/irb/theme/rk2025.rb', line 5

def w
  @w
end

Instance Method Details

#clearObject



18
19
20
21
# File 'lib/irb/theme/rk2025.rb', line 18

def clear
  @masks = @h.times.map{[]}
  @colors = @h.times.map{[]}
end

#draw(cx, cy, r, color) ⇒ Object



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/irb/theme/rk2025.rb', line 80

def draw(cx, cy, r, color)
  offset = 2
  plot = ->x, y, v, color {
    val = @masks[y/2][x/2] || 0
    bit = 1 << (y % 2 * 2 + x % 2)
    @masks[y/2][x/2] = (val & ~bit) | (v * bit)
    @colors[y/2][x/2] = color if color
  }
  xrange = ([2 * (cx - r - offset), 0].max.ceil..[2 * (cx + r + offset), 2 * @w - 1].min)
  yrange = ([2 * cy - r - offset, 0].max.ceil..[2 * cy + r + offset, 2 * @h - 1].min)
  yrange.each do |iy|
    xrange.each do |ix|
      x = ix - 2 * cx
      y = (iy - 2 * cy) * 2.0
      r2 = x ** 2 + y ** 2
      next if r2 > 4 * (r + offset)**2
      if r2 > 4 * (r + 0.5) ** 2
        plot[ix, iy, 0, nil]
      else
        plot[ix, iy, yield(x / 2.0 / r, y / 2.0 / r) ? 1 : 0, color]
      end
    end
  end
end

#render_to_linesObject



23
24
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
73
74
75
76
77
78
# File 'lib/irb/theme/rk2025.rb', line 23

def render_to_lines
  output_lines = []
  @h.times do |y|
    color = nil
    default_background = 16+36*5+6*5+4
    text_color = 16+36*0+6*2+0
    line = @lines[y]
    if y == @focus_line
      default_background = 16+36*5+6*5+3
      line = line.sub('  ', '🍊')
      text_color = "#{16+36*0+6*1+0};1"
    end 
    output = +''
    background = color = nil
    x = 0
    chars = line ? line.grapheme_clusters.flat_map { |c|
      case Reline::Unicode.get_mbchar_width(c)
      when 2
        [[c, 2], :skip]
      when 1
        [[c, 1]]
      else
        []
      end
    } : []
    while x < @w do
      m = @masks[y][x] || 0
      c, cw = chars[x]
      if c == :skip
        x += 1
        next
      end
      c = nil if c == ' '

      col = @colors[y][x]
      bg = default_background
      if c && x + cw <= @w
        bg = m == 0 ? default_background : col || default_background
        col = text_color
        x += cw
      else
        x += 1
      end
      if color != col
        output << "\e[38;5;#{color = col}m"
      end
      if background != bg
        output << "\e[48;5;#{background = bg}m"
      end
      output << (c || CHARS[m])
    end
    output << "\e[m"
    output_lines << output
  end
  output_lines
end