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/widgets/base.rb

48 lines
972 B
Ruby
Raw Normal View History

2017-07-22 05:00:28 -04:00
# frozen_string_literal: true
module Widgets
class Base
2017-07-25 10:29:01 -04:00
def initialize(parent)
2017-07-24 07:54:00 -04:00
@parent = parent
2017-07-24 18:34:50 -04:00
@props = {}.freeze
2017-07-22 05:00:28 -04:00
end
2017-07-24 18:34:50 -04:00
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?
2017-07-25 10:29:01 -04:00
@window = nil if props[:x] != value[:x] || props[:y] != value[:y] ||
props[:width] != value[:width] || props[:height] != value[:height]
2017-07-24 18:34:50 -04:00
@props = value
end
2017-07-25 11:12:25 -04:00
def trigger(event); end
2017-07-22 05:00:28 -04:00
def render
2017-07-22 09:03:10 -04:00
draw
2017-07-24 06:08:53 -04:00
window.refresh
2017-07-22 09:03:10 -04:00
end
2017-07-25 10:48:37 -04:00
private
attr_reader :parent, :props
def window
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
end
2017-07-22 09:03:10 -04:00
def draw
raise NotImplementedError, "#{self.class}#draw"
2017-07-22 05:00:28 -04:00
end
2017-07-22 10:27:14 -04:00
def setpos(x, y)
2017-07-24 06:08:53 -04:00
window.setpos y, x
2017-07-22 10:27:14 -04:00
end
2017-07-22 14:46:50 -04:00
def addstr(s)
2017-07-24 05:52:42 -04:00
window.addstr s
2017-07-22 14:46:50 -04:00
end
2017-07-22 05:00:28 -04:00
end
end