class Rurses::Window

Constants

ATTRIBUTES
MODE_NAMES

Attributes

curses_ref[R]
subwindows[R]

Public Class Methods

new(**details) click to toggle source
# 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

Public Instance Methods

change_modes(modes) click to toggle source
# 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) click to toggle source
# 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
columns() click to toggle source
# 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 ) click to toggle source
# 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_x() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 33
def cursor_x
  Rurses.curses.getcurx(curses_ref)
end
cursor_xy() click to toggle source
# 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_y() click to toggle source
# 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 ) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
lines() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 50
def lines
  Rurses.curses.getmaxy(curses_ref)
end
move_cursor(x: , y: ) click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 89
def move_cursor(x: , y: )
  Rurses.curses.wmove(curses_ref, y, x)
end
refresh_in_memory() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 124
def refresh_in_memory
  Rurses.curses.wnoutrefresh(curses_ref)
end
resize(lines: , columns: ) click to toggle source
# 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
size() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 54
def size
  {columns: columns, lines: lines}
end
skip_line() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 104
def skip_line
  move_cursor(x: 0, y: cursor_y + 1)
end
standard_screen?() click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 29
def standard_screen?
  @standard_screen
end
style(*attributes) { || ... } click to toggle source
# 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) click to toggle source
# File lib/terminal-notes/rurses/window.rb, line 144
def subwindow(name)
  subwindows[name]
end