Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/curses/react/component.rb
Braiden Vasco 896aae87cb Rename Curses::React::Component#draw to #render, #render to #draw
To make components similar to React components
2017-07-28 00:02:59 +00:00

55 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Curses
module React
class Component
def initialize(parent)
self.parent = parent
@props = {}.freeze
end
def props=(value)
raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash
raise ArgumentError, 'expected props to be frozen' unless value.frozen?
@window = nil if props[:x] != value[:x] || props[:y] != value[:y] ||
props[:width] != value[:width] || props[:height] != value[:height]
@props = value
end
def trigger(event); end
def draw
render
window.refresh
end
private
attr_reader :parent, :props
def parent=(value)
return if value.nil?
raise TypeError, "expected #parent to be a #{Component}" unless value.is_a? Component
@parent = value
end
def window
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
end
def render
raise NotImplementedError, "#{self.class}#render"
end
def setpos(x, y)
window.setpos y, x
end
def addstr(s)
window.addstr s
end
end
end
end