Sha256: 0c3632bcc63ffe9be8efb5bfe42218f85c0de4be6b224cea1a88021a98105f36

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'highline'
require 'tty-cursor'

require_relative 'ansi'
require_relative 'painter'

module WhirledPeas
  module UI
    class Screen
      def initialize(print_output=true)
        @print_output = print_output
        @terminal = HighLine.new.terminal
        @cursor = TTY::Cursor
        @strokes = []
        refresh_size!
        Signal.trap('SIGWINCH', proc { self.refresh_size! })
      end

      def paint(template)
        @template = template
        refresh
      end

      def needs_refresh?
        @refreshed_width != width || @refreshed_height != height
      end

      def refresh
        strokes = [cursor.hide, cursor.move_to(0, 0), cursor.clear_screen_down]
        Painter.paint(@template, Canvas.new(0, 0, width, height)) do |stroke|
          unless stroke.chars.nil?
            strokes << cursor.move_to(stroke.left, stroke.top)
            strokes << stroke.chars
          end
        end
        return unless @print_output
        strokes.each(&method(:print))
        STDOUT.flush
        @refreshed_width = width
        @refreshed_height = height
      end

      def finalize
        return unless @print_output
        print UI::Ansi.clear
        print cursor.move_to(0, height - 1)
        print cursor.show
        STDOUT.flush
      end

      protected

      def refresh_size!
        @width, @height = terminal.terminal_size
      end

      private

      attr_reader :cursor, :terminal, :width, :height
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
whirled_peas-0.2.0 lib/whirled_peas/ui/screen.rb
whirled_peas-0.1.1 lib/whirled_peas/ui/screen.rb
whirled_peas-0.1.0 lib/whirled_peas/ui/screen.rb