Class: Vice::Blitter

Inherits:
Object
  • Object
show all
Defined in:
lib/vice/blitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Blitter

Returns a new instance of Blitter.



2
3
4
5
# File 'lib/vice/blitter.rb', line 2

def initialize(window)
  Curses.init_pair 1, Curses::COLOR_BLACK, Curses::COLOR_WHITE
  window.attrset Curses.color_pair(0)
end

Instance Method Details

#drawalert(vice, window) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vice/blitter.rb', line 25

def drawalert(vice, window)
  window.setpos Curses.lines - 1, 0
  window.addstr ' ' * Curses.cols

  return if vice.msg.nil?

  window.setpos Curses.lines - 1, 0
  window.addstr '  ' + vice.msg

  vice.reset_alert
end

#drawbuffer(vice, window) ⇒ Object



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
128
129
130
131
132
# File 'lib/vice/blitter.rb', line 79

def drawbuffer(vice, window)
  buffer = vice.buffers[vice.current_buffer]
  visual_cursor = Vice::Cursor.new buffer.cursor.line, 0

  (0..buffer.cursor.col - 1).each do |i|
    visual_cursor.col += if buffer.currentline[i] == "\t"
               vice.config['tab_width']
             else
               1
             end
  end

  drawtabs vice, buffer, window

  @linenumwidth = buffer.lines.to_s.length + 1

  visual_cursor.line += 1 # space for tabs above buffer
  visual_cursor.line -= buffer.v_scroll
  visual_cursor.col += @linenumwidth + 1 # leave space for line numbers

  while visual_cursor.line >= Curses.lines - 2
    buffer.v_scroll += 1
    visual_cursor.line -= 1
  end

  while visual_cursor.line < 1
    buffer.v_scroll -= 1
    visual_cursor.line += 1
  end

  (1..Curses.lines - 2).each do |r|
    # r: on-screen line
    # i: in-buffer line
    i = r - 1 + buffer.v_scroll
    window.setpos r, 0
    if i < buffer.lines
      line = pad buffer.getline(i).gsub(/(\t)/, ' ' * vice.config['tab_width'])
      window.addstr formatnumber(i + 1) + line
    else
      window.addstr pad(formatnumber('~'))
    end
  end

  drawstatus vice.mode, window, buffer, vice.prompt

  drawalert vice, window

  if vice.mode == :prompt
    window.setpos Curses.lines - 2, vice.prompt.length + 2
  else
    window.setpos visual_cursor.line, visual_cursor.col
  end
  window.refresh
end

#drawstatus(mode, window, buffer, prompt) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vice/blitter.rb', line 37

def drawstatus(mode, window, buffer, prompt)
  window.attrset Curses.color_pair(1)

  # clear
  window.setpos Curses.lines - 2, 0
  window.addstr ' ' * Curses.cols

  # draw mode
  window.setpos Curses.lines - 2, 0
  modestring = if mode == :insert
           ' -- insert --'
         elsif mode == :prompt
           ' :' + prompt
         end
  window.addstr modestring

  # draw cursor position
  location = buffer.cursor.line.to_s + ',' + buffer.cursor.col.to_s
  window.setpos Curses.lines - 2, Curses.cols - location.length - 1
  window.addstr location

  window.attrset Curses.color_pair(0)
end

#drawtabs(vice, buffer, window) ⇒ Object



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

def drawtabs(vice, buffer, window)
  window.attrset Curses.color_pair(1)
  window.setpos 0, 0
  window.addstr ' ' * Curses.cols
  window.setpos 0, 2
  vice.buffers.each do |b|
    window.attrset(Curses.color_pair(0) | Curses::A_BOLD) if b == buffer

    name = b.filename || '[no name]'
    name = b.modified ? '+ ' + name : name

    window.addstr ' ' + name + ' '

    window.attrset Curses.color_pair(1) if b == buffer
  end
  window.attrset Curses.color_pair(0)
end

#formatnumber(number) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vice/blitter.rb', line 61

def formatnumber(number)
  delta = if number == '~'
      @linenumwidth - 1
    else
      @linenumwidth - number.to_s.length
    end
  delta.times do
    number = ' ' + number.to_s
  end
  number += ' '
end

#pad(string) ⇒ Object



73
74
75
76
77
# File 'lib/vice/blitter.rb', line 73

def pad(string)
  delta = Curses.cols - string.length
  delta.times { string += ' ' } if delta > 0
  string
end