Sha256: 17e5e85b6c966939ee586028687692d1ac4b5b800ebcc19be7d70249600233ac

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require 'virtus'

require_relative 'presentation'
require_relative 'line_collection'
require_relative '../output/interface_renderer'
require_relative '../support/esc'
require_relative '../support/queue'
require_relative '../support/terminal'

module Vedeu
  class Interface
    include Virtus.model
    include Presentation
    include Vedeu::Queue

    attribute :name,    String
    attribute :lines,   LineCollection
    attribute :y,       Integer, default: 1
    attribute :x,       Integer, default: 1
    attribute :z,       Integer, default: 1
    attribute :width,   Integer, default: Terminal.width
    attribute :height,  Integer, default: Terminal.height
    attribute :current, String,  default: ''
    attribute :cursor,  Boolean, default: true

    def geometry
      @geometry ||= Geometry.new(self)
    end

    def refresh
      if enqueued?
        self.current = dequeue

      elsif no_content?
        self.current = clear_interface

      else
        self.current

      end
    end

    def to_json
      Oj.dump(json_attributes, mode: :compat)
    end

    def to_s
      clear_interface + render_content
    end

    private

    def no_content?
      self.current.nil? || self.current.empty?
    end

    def render_content
      InterfaceRenderer.render(self)
    end

    def clear_interface
      InterfaceRenderer.clear(self)
    end

    def json_attributes
      {
        colour: colour, # TODO: translate back?
        style:  style,
        name:   name,
        lines:  lines,
        y:      y,
        x:      x,
        z:      z,
        width:  width,
        height: height,
        cursor: cursor
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.0.32 lib/vedeu/models/interface.rb
vedeu-0.0.31 lib/vedeu/models/interface.rb