Class: Rumacs::Screen::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/rumacs/screen/curses.rb

Constant Summary collapse

CHARACTER_MAP =
{
  Curses::Key::PPAGE => :pgup,
  Curses::Key::NPAGE => :pgdn,
  Curses::Key::UP => :up,
  Curses::Key::DOWN => :down,
  Curses::Key::LEFT => :left,
  Curses::Key::RIGHT => :right,
  Curses::Key::HOME => :home,
  Curses::Key::END => :end,
  Curses::Key::IC => :insert,
  Curses::Key::BACKSPACE => :backspace,
  127 => :backspace,
  Curses::Key::DC => :delete,
  Curses::Key::BTAB => :backtab,

  265 => :f_1,
  266 => :f_2,
  267 => :f_3,
  268 => :f_4,
  269 => :f_5,
  270 => :f_6,
  271 => :f_7,
  272 => :f_8,
  273 => :f_9,
  274 => :f_10,
  275 => :f_11,
  276 => :f_12,

  1   => :c_a,
  2   => :c_b,
  3   => :c_c,
  4   => :c_d,
  5   => :c_e,
  6   => :c_f,
  7   => :c_g,
  8   => :c_h,
  9   => :c_i,
  10  => :c_j,
  11  => :c_k,
  12  => :c_l,
  13  => :c_m,
  14  => :c_n,
  15  => :c_o,
  16  => :c_p,
  17  => :c_q,
  18  => :c_r,
  19  => :c_s,
  20  => :c_t,
  21  => :c_u,
  22  => :c_v,
  23  => :c_w,
  24  => :c_x,
  25  => :c_y,
  26  => :c_z,

  27  => :c_3,
  28  => :c_4,
  29  => :c_5,
  30  => :c_6,
  31  => :c_7,
  34  => :c_0,
}

Instance Method Summary collapse

Constructor Details

#initialize(screen) ⇒ Backend

Returns a new instance of Backend.



11
12
13
14
15
16
# File 'lib/rumacs/screen/curses.rb', line 11

def initialize(screen)
  @screen = screen
  rows, cols = $stdin.winsize
  @screen.rows = rows
  @screen.cols = cols
end

Instance Method Details

#clean_exitObject



42
43
44
# File 'lib/rumacs/screen/curses.rb', line 42

def clean_exit()
  Curses.close_screen
end

#draw_line(row, col, text) ⇒ Object



127
128
129
130
131
# File 'lib/rumacs/screen/curses.rb', line 127

def draw_line(row, col, text)
  Curses.setpos(row, col)
  Curses.addstr((text || ''))
  Curses.refresh
end

#draw_line_mode(row, col, text, mode) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/rumacs/screen/curses.rb', line 137

def draw_line_mode(row, col, text, mode)
  curses_modes = {
    :statusbar => Curses::A_REVERSE,
    :minibuf => Curses::A_NORMAL,
  }

  Curses.attron(curses_modes[mode])
  draw_line(row, col, text)
  Curses.attroff(curses_modes[mode])
end

#init_screenObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rumacs/screen/curses.rb', line 18

def init_screen()
  Curses.init_screen
  Curses.raw
  Curses.noecho # do not show typed keys
  Curses.stdscr.keypad(true) # enable arrow keys

  # trap signals from kill; key strokes don't trigger anymore
  for i in 1 .. 15  # SIGHUP .. SIGTERM
    if trap(i, "SIG_IGN") != 0 then  # 0 for SIG_IGN
      trap(i) do |sig|
        Curses.close_screen
        puts "rumacs received an unexpected signal #{sig}."
        exit sig
      end
    end
  end

  begin
    yield
  ensure
    clean_exit
  end
end

#jump_to_pointObject



123
124
125
# File 'lib/rumacs/screen/curses.rb', line 123

def jump_to_point()
  Curses.setpos(@screen.point_row, @screen.point_col)
end

#next_characterObject



109
110
111
112
113
114
115
116
117
# File 'lib/rumacs/screen/curses.rb', line 109

def next_character
  c = Curses.getch

  if CHARACTER_MAP.include? c then
    c = CHARACTER_MAP[c]
  end

  c
end

#notifyObject



133
134
135
# File 'lib/rumacs/screen/curses.rb', line 133

def notify()
  Curses.beep
end

#redrawObject



119
120
121
# File 'lib/rumacs/screen/curses.rb', line 119

def redraw()
  Curses.refresh
end