Class: Rurses::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-notes/rurses/window.rb

Constant Summary collapse

MODE_NAMES =
{
  c_break:      :cbreak,
  no_echo:      :noecho,
  keypad:       [:keypad, :window, true],
  hide_cursor:  [:curs_set, 0],
  non_blocking: [:timeout, 0]
}
ATTRIBUTES =
Hash.new { |all, name|
  all[name] = Rurses.curses.const_get("A_#{name.upcase}")
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**details) ⇒ Window

Returns a new instance of Window.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/terminal-notes/rurses/window.rb', line 14

def initialize(**details)
  @curses_ref      = details.fetch(:curses_ref) {
    Rurses.curses.newwin(
      details.fetch(:lines),
      details.fetch(:columns),
      details.fetch(:y),
      details.fetch(:x)
    )
  }
  @standard_screen = details.fetch(:standard_screen) { false }
  @subwindows      = { }
end

Instance Attribute Details

#curses_refObject (readonly)

Returns the value of attribute curses_ref.



27
28
29
# File 'lib/terminal-notes/rurses/window.rb', line 27

def curses_ref
  @curses_ref
end

#subwindowsObject (readonly)

Returns the value of attribute subwindows.



27
28
29
# File 'lib/terminal-notes/rurses/window.rb', line 27

def subwindows
  @subwindows
end

Instance Method Details

#change_modes(modes) ⇒ Object



63
64
65
66
67
68
# File 'lib/terminal-notes/rurses/window.rb', line 63

def change_modes(modes)
  modes.each do |name|
    mode = Array(MODE_NAMES[name] || name)
    Rurses.curses.send(*mode.map { |arg| arg == :window ? curses_ref : arg })
  end
end

#clear(reset_cursor: true) ⇒ Object



119
120
121
122
# File 'lib/terminal-notes/rurses/window.rb', line 119

def clear(reset_cursor: true)
  Rurses.curses.wclear(curses_ref)
  move_cursor(x: 0, y: 0) if reset_cursor
end

#columnsObject



46
47
48
# File 'lib/terminal-notes/rurses/window.rb', line 46

def columns
  Rurses.curses.getmaxx(curses_ref)
end

#create_subwindow(name:, top_padding: 0, left_padding: 0, right_padding: 0, bottom_padding: 0) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/terminal-notes/rurses/window.rb', line 128

def create_subwindow( name: , top_padding:   0, left_padding:   0,
                              right_padding: 0, bottom_padding: 0 )
  s                = size
  xy               = cursor_xy
  subwindows[name] =
    self.class.new(
      curses_ref: Rurses.curses.derwin(
        curses_ref,
        s[:lines]   - (top_padding  + bottom_padding),
        s[:columns] - (left_padding + right_padding),
        xy[:y]      + top_padding,
        xy[:x]      + left_padding
      )
    )
end

#cursor_xObject



33
34
35
# File 'lib/terminal-notes/rurses/window.rb', line 33

def cursor_x
  Rurses.curses.getcurx(curses_ref)
end

#cursor_xyObject



41
42
43
44
# File 'lib/terminal-notes/rurses/window.rb', line 41

def cursor_xy
  y, x = Rurses.curses.getyx(curses_ref)
  {x: x, y: y}
end

#cursor_yObject



37
38
39
# File 'lib/terminal-notes/rurses/window.rb', line 37

def cursor_y
  Rurses.curses.getcury(curses_ref)
end

#draw_border(left: 0, right: 0, top: 0, bottom: 0, top_left: 0, top_right: 0, bottom_left: 0, bottom_right: 0) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/terminal-notes/rurses/window.rb', line 70

def draw_border( left:     0, right:     0, top:         0, bottom:       0,
                 top_left: 0, top_right: 0, bottom_left: 0, bottom_right: 0 )
  args = [
    left,     right,     top,         bottom,
    top_left, top_right, bottom_left, bottom_right
  ].map { |c| c.is_a?(String) ? c.encode("UTF-8").codepoints.first : c }
  if args.any? { |c| c >= 128 }
    Rurses.curses.wborder_set(
      curses_ref,
      *args.map { |c|
        char            = Rurses.curses::WinStruct::CCharT.new
        char[:chars][0] = c
      }
    )
  else
    Rurses.curses.wborder(curses_ref, *args)
  end
end

#draw_string(content) ⇒ Object



93
94
95
# File 'lib/terminal-notes/rurses/window.rb', line 93

def draw_string(content)
  Rurses.curses.waddstr(curses_ref, content)
end

#draw_string_on_a_line(content) ⇒ Object



97
98
99
100
101
102
# File 'lib/terminal-notes/rurses/window.rb', line 97

def draw_string_on_a_line(content)
  old_y = cursor_y
  draw_string(content)
  new_y = cursor_y
  move_cursor(x: 0, y: new_y + 1) if new_y == old_y
end

#linesObject



50
51
52
# File 'lib/terminal-notes/rurses/window.rb', line 50

def lines
  Rurses.curses.getmaxy(curses_ref)
end

#move_cursor(x:, y:) ⇒ Object



89
90
91
# File 'lib/terminal-notes/rurses/window.rb', line 89

def move_cursor(x: , y: )
  Rurses.curses.wmove(curses_ref, y, x)
end

#refresh_in_memoryObject



124
125
126
# File 'lib/terminal-notes/rurses/window.rb', line 124

def refresh_in_memory
  Rurses.curses.wnoutrefresh(curses_ref)
end

#resize(lines:, columns:) ⇒ Object



58
59
60
61
# File 'lib/terminal-notes/rurses/window.rb', line 58

def resize(lines: , columns: )
  Rurses.curses.resizeterm(lines, columns) if standard_screen?
  Rurses.curses.wresize(curses_ref, lines, columns)
end

#sizeObject



54
55
56
# File 'lib/terminal-notes/rurses/window.rb', line 54

def size
  {columns: columns, lines: lines}
end

#skip_lineObject



104
105
106
# File 'lib/terminal-notes/rurses/window.rb', line 104

def skip_line
  move_cursor(x: 0, y: cursor_y + 1)
end

#standard_screen?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/terminal-notes/rurses/window.rb', line 29

def standard_screen?
  @standard_screen
end

#style(*attributes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/terminal-notes/rurses/window.rb', line 108

def style(*attributes)
  attributes.each do |attribute|
    Rurses.curses.wattron(curses_ref, ATTRIBUTES[attribute])
  end
  yield
ensure
  attributes.each do |attribute|
    Rurses.curses.wattroff(curses_ref, ATTRIBUTES[attribute])
  end
end

#subwindow(name) ⇒ Object



144
145
146
# File 'lib/terminal-notes/rurses/window.rb', line 144

def subwindow(name)
  subwindows[name]
end