Class: Alchemist::Curses::GlyphWindow

Inherits:
Object
  • Object
show all
Includes:
FFI::NCurses
Defined in:
lib/alchemist-server/curses/glyph_window.rb

Constant Summary collapse

ROWS =
21
COLS =
48

Instance Method Summary collapse

Constructor Details

#initialize(line, col) ⇒ GlyphWindow

Returns a new instance of GlyphWindow.



9
10
11
12
13
14
# File 'lib/alchemist-server/curses/glyph_window.rb', line 9

def initialize(line,col)
  @width = (COLS + 2)*2
  @win = newwin ROWS, @width, line, col
  @starting_char = 0
  @cursor_offset = 0
end

Instance Method Details

#charactersObject



29
30
31
# File 'lib/alchemist-server/curses/glyph_window.rb', line 29

def characters
  Glyphs.strings[range_start..range_end] || []
end

#drawObject



16
17
18
19
20
21
# File 'lib/alchemist-server/curses/glyph_window.rb', line 16

def draw
  wmove @win, 0, 0
  wclear @win
  wprintw @win, rows.pad_to_unicode_monospace
  reset_cursor
end

#have_user_selectObject



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
79
80
81
82
83
84
85
# File 'lib/alchemist-server/curses/glyph_window.rb', line 49

def have_user_select
  draw
  wmove @win, 0, 0
  wrefresh @win

  while (c = getch) != KEY_RETURN
    y,x = getyx @win

    case c
    when KEY_ESCAPE
      return nil
    when KEY_LEFT
      @cursor_offset = [@cursor_offset - 1, 0].max
      reset_cursor
    when KEY_RIGHT
      @cursor_offset = [@cursor_offset + 1, (ROWS*COLS)].min
      reset_cursor
    when KEY_UP
      @cursor_offset = [@cursor_offset - COLS, 0].max
      reset_cursor
    when KEY_DOWN
      @cursor_offset = [@cursor_offset + COLS, (ROWS*COLS)].min
      reset_cursor
    when '0'.ord
      @starting_char += ROWS*COLS
      draw
    when '9'.ord
      @starting_char = [@starting_char-ROWS*COLS,0].max
      draw
    end
  end

  characters[@cursor_offset]
ensure
  wclear @win
  wrefresh @win
end

#range_endObject



45
46
47
# File 'lib/alchemist-server/curses/glyph_window.rb', line 45

def range_end
  @starting_char + ROWS*COLS
end

#range_startObject



41
42
43
# File 'lib/alchemist-server/curses/glyph_window.rb', line 41

def range_start
  @starting_char
end

#reset_cursorObject



23
24
25
26
27
# File 'lib/alchemist-server/curses/glyph_window.rb', line 23

def reset_cursor
  y,x = @cursor_offset.divmod COLS
  wmove @win, y, x*2
  wrefresh @win
end

#rowsObject



33
34
35
36
37
38
39
# File 'lib/alchemist-server/curses/glyph_window.rb', line 33

def rows
  characters
  .each_slice(COLS)
  .map { |slice| slice.join('') }
  .join("\n")
  .force_encoding(Encoding::UTF_8)
end