Sha256: f1f4817748b2d8bf0b36067b8a32757a473653363e10ed1357996c069f8bbcd3

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: false

module Prompts
  class Content
    attr_reader :slots

    def initialize(width: MAX_WIDTH)
      @slots = []
      @frame_stack = []
      @width = width
    end

    def paragraph(text)
      paragraph = Paragraph.new(text, width: @width)
      @slots.concat paragraph.lines
      self
    end

    def gap
      @slots << SPACE
      self
    end

    def box(padded: false, border_color: nil, &block)
      box = Box.new(width: @width, padded: padded, border_color: border_color)
      yield(box)
      @slots.concat box.lines
      self
    end

    def render
      clear_screen
      render_frame
    end

    def reset!
      @slots = @frame_stack.first.dup
    end

    def prepend(*lines)
      @slots.unshift(*lines)
    end

    private

    def clear_screen
      jump_cursor_to_top
      erase_down
    end

    def render_frame
      @frame_stack << @slots.dup
      OUTPUT.puts SPACE

      return if @slots.empty?

      OUTPUT.puts @slots.join("\n")
      OUTPUT.puts SPACE
      @slots.clear
    end

    def jump_cursor_to_top
      OUTPUT.print "\033[H"
    end

    def erase_down
      OUTPUT.print "\e[J"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prompts-0.3.0 lib/prompts/content.rb
prompts-0.2.1 lib/prompts/content.rb